Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

1.9. Docker Example

	

1.9.1. Example Java - Spring boot with Docker

1.9.1.1. 獲取 CentOS 7 鏡像

docker pull centos:7

		
# docker pull centos:7
7: Pulling from library/centos
343b09361036: Pull complete 
Digest: sha256:bba1de7c9d900a898e3cadbae040dfe8a633c06bc104a0df76ae24483e03c077
Status: Downloaded newer image for centos:7
		
		

基于 CentOS 7 運行一個容器

docker run -it --name mycentos docker.io/centos:7 /bin/bash

		
# docker run -it --name mycentos docker.io/centos:7 /bin/bash
		
		

運行後直接進入了容器的shell控制台預設是bash

1.9.1.2. 安裝 openjdk

		
# yum install -y java-1.8.0-openjdk

# cat >> /etc/profile.d/java.sh <<'EOF'
export JAVA_HOME=/usr/java/default
export JAVA_OPTS="-server -Xms2048m -Xmx4096m -Djava.io.tmpdir=/tmp -Djava.security.egd=file:/dev/./urandom -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:.
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:
EOF

# source /etc/profile.d/java.sh
		
		

檢查Java是否安裝成功

		
# whereis java
java: /usr/bin/java /usr/lib/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz

# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
		
		

創建應用程序目錄

# mkdir -p /www/netkiller.cn/www.netkiller.cn/		
		

推出當前容器

# exit
		

1.9.1.3. Spring boot 包

複製 jar 檔案到Docker容器

		
docker cp /www/netkiller.cn/www.netkiller.cn/www.netkiller.cn-0.0.1.war mycentos:/usr/local/libexec
		
		

1.9.1.4. 啟動 Spring boot 項目

啟動容器

		
# docker start mycentos
mycentos		
		
		

進入容器

		
# docker exec -it mycentos /bin/bash
		
		

如果僅僅是測試可以手動啟動 Srping boot 項目

		
# cat >> /root/run.sh <<EOF
java -server -Xms2048m -Xmx8192m -jar /usr/local/libexec/www.netkiller.cn-0.0.1.war
EOF

chmod u+x /root/run.sh
		
		

生產環境請使用啟動腳本

				
# curl -s https://raw.githubusercontent.com/oscm/build/master/Application/Spring/service/springbootd -o /etc/init.d/springbootd
# chmod +x /etc/init.d/springbootd
		
		

編輯啟動腳本 /etc/init.d/springbootd 修改下面配置項

		
##############################################
BASEDIR="/www/netkiller.cn/api.netkiller.cn"
JAVA_HOME=/srv/java
JAVA_OPTS="-server -Xms2048m -Xmx8192m -Djava.security.egd=file:/dev/./urandom"
PACKAGE="api.netkiller.cn-0.0.2-release.jar"
CONFIG="--spring.config.location=$BASEDIR/application.properties"
USER=www
##############################################
NAME=springbootd
PROG="$JAVA_HOME/bin/java $JAVA_OPTS -jar $BASEDIR/$PACKAGE $CONFIG"
LOGFILE=/var/tmp/$NAME.log
PIDFILE=/var/tmp/$NAME.pid
ACCESS_LOG=/var/tmp/$NAME.access.log
##############################################
		
		

你也可以使用 systemd 啟動腳本,詳見《Netkiller Java 手札》

1.9.1.5. 基于 CentOS 7 製作 spring 鏡像

docker commit mycentos springboot:1

		
# docker commit mycentos springboot:1
sha256:757d92d642d1b5a7b244f6ddf89f24a8d463d154438651c83ba51a644b401782		
		
		

啟動 spring boot 容器

		
# docker run -d --name springboot -p 80:8080 springboot:1 /root/run.sh
		
		

		
-d: 以守護進程方式啟動 
--name:指定容器的名稱 
-p:映射容器8080連接埠到宿主機的80連接埠 
springboot:1 :上一步製作好的springboot鏡像,版本號為1	
		
		

啟動容器

		
# docker start springboot		
		
		

停止容器

		
# docker stop springboot		
		
		

1.9.2. Redis

1.9.2.1. 獲取 Redis 鏡像

docker pull redis

		
# docker pull redis  
Using default tag: latest
latest: Pulling from library/redis
10a267c67f42: Pull complete 
5b690bc4eaa6: Pull complete 
4cdd94354d2a: Pull complete 
71c1f30d820f: Pull complete 
c54584150374: Pull complete 
d1f9221193a6: Pull complete 
d45bc46b48e4: Pull complete 
Digest: sha256:548a75066f3f280eb017a6ccda34c561ccf4f25459ef8e36d6ea582b6af1decf
Status: Downloaded newer image for redis:latest
		
		

1.9.2.2. 啟動一個 Redis 實例

# docker run --name my-redis -d redis
10207174e18f61290f9c869e6437fa787e459e07b076b82cedf800a8c37c515d		
		

查看啟動情況

		
# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
10207174e18f        redis               "docker-entrypoint..."   8 minutes ago       Up 8 minutes        6379/tcp            my-redis
		
		

1.9.2.3. 進入 Redis

		
# docker run -it --link my-redis:redis --rm redis redis-cli -h redis -p 6379
redis:6379> set name neo
OK
redis:6379> get name
"neo"
redis:6379> exit
				
		

1.9.2.4. 啟動一個 Redis 實例並映射 6379 連接埠

		
# docker rm my-redis
my-redis

# docker stop my-redis
my-redis

# docker run --name my-redis -d -p 6379:6379 redis
10207174e18f61290f9c869e6437fa787e459e07b076b82cedf800a8c37c515d

# docker ps -a                                    
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1c4540d8617f        redis               "docker-entrypoint..."   2 seconds ago       Up 1 second         0.0.0.0:6379->6379/tcp   my-redis
		
		

檢查連接埠

		
# ss -lnt | grep 6379
LISTEN     0      128         :::6379                    :::*  
		
		

1.9.2.5. 維護容器

使用下面命令進入容器維護 Redis

		
# docker exec -it my-redis /bin/bash
root@1c4540d8617f:/data#