Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | About

165.8. Docker Example

	

165.8.1. Example Java - Spring boot with Docker

165.8.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

165.8.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
		

165.8.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
		
		

165.8.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 手札》

165.8.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		
		
		

165.8.2. Redis

165.8.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
		
		

165.8.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
		
		

165.8.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
				
		

165.8.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                    :::*  
		
		

165.8.2.5. 维护容器

使用下面命令进入容器维护 Redis

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

root@1c4540d8617f:/data# redis-server -v
Redis server v=3.2.9 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=a30533b464d1689b
		
		

165.8.3. Nginx

本例子使用 alpine 版本

165.8.3.1. nginx:latest

过程 165.1. 

  1. [root@iZj6ciilv2rcpgauqg2uuwZ]~# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    Digest: sha256:41ad9967ea448d7c2b203c699b429abe1ed5af331cd92533900c6d77490e0268
    Status: Image is up to date for nginx:latest
    				
  2. 启动容器

    docker run --name my-nginx-container -p 80:80 -d nginx
    				

    上面不能满足生产环境的需求,通常不会将数据放在容器中,我的做法如下。

    docker rm my-nginx-container -f
    docker run --name my-nginx-container \
    	-v /srv/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
    	-v /srv/nginx/conf.d:/etc/nginx/conf.d:ro \
    	-v /var/log/nginx:/var/log/nginx:rw \
    	-v /www:/www:ro \
    	-p 80:80 -d nginx
    docker ps
    				

165.8.3.2. 安装 Docker Nginx alpine

过程 165.2. Docker nginx

  1. 获取镜像

    # docker pull nginx:alpine
    				
  2. 运行容器

    				
    docker run --name my-nginx-container -v /srv/nginx:/etc/nginx:ro -v /www:/www:ro -p 80:80 -d nginx:alpine
    				
    				
  3. docker exec -it my-nginx-container /bin/bash