8.2 编写配置文件docker-compose.yml
vim /opt/compose_nginx/docker-compose.yml version: '3' services: nginx: container_name: web1 hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: lnmp: ipv4_address: 172.18.0.10 volumes: - ./wwwroot:/usr/local/nginx/html networks: lnmp: driver: bridge ipam: config: - subnet: 172.18.0.0/16
- 参数说明:
version: '3' #docker-compose版本为3,版本2只支持单机编排 services: #使用services定义服务 nginx: #容器名为nginx container_name: web1 #使用dockerfile来构建镜像 hostname: nginx-test #主机名为nginx-test build: context: ./nginx #指定Dockerfile文件所在位置 dockerfile: Dockerfile #指定文件名 ports: #映射端口 - 1216:80 - 1217:443 networks: lnmp: ipv4_address: 172.18.0.10 #加入网络设置此容器ip volumes: - ./wwwroot:/usr/local/nginx/html #设置数据卷挂载 networks: #设置网络为自定义网络 lnmp: driver: bridge #网络模式 ipam: config: - subnet: 172.18.0.0/16 #设置自定义网络的网段,设置完后保存退出
8.3 生成镜像容器
cd /opt/compose_nginx/ docker-compose -f docker-compose.yml up -d -f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml -p, --project-name NAME :指定项目名称,默认使用目录名称 -d :在后台运行
8.4 验证测试
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b205bbcc8b37 compose_nginx_nginx "/run.sh" About a minute ago Up About a minute 0.0.0.0:1216->80/tcp, :::1216->80/tcp, 0.0.0.0:1217->443/tcp, :::1217->443/tcp web1 cd /opt/compose_nginx/ docker-compose ps #必须在docker-compose.yml所在目录执行此命令 浏览器访问:http://192.168.147.105:1216
九、Docker-Compose 撰写 Tomcat
9.1 准备安装包
cd /opt mkdir -p /opt/compose-tomcat/tomcat 拖入安装包apache-tomcat-8.5.16.tar.gz 、jdk-8u91-linux-x64.tar.gz
9.2 准备依赖文件
vim Dockerfile FROM centos:7 MAINTAINER this is tomcat image <xc> ADD jdk-8u91-linux-x64.tar.gz /usr/local/ WORKDIR /usr/local/ RUN mv jdk1.8.0_91 /usr/local/java ENV JAVA_HOME /usr/local/java ENV JRE_HOME ${JAVA_HOME}/jre ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib ENV PATH $JAVA_HOME/bin:$PATH ADD apache-tomcat-8.5.16.tar.gz /usr/local/ WORKDIR /usr/local/ RUN mv apache-tomcat-8.5.16 /usr/local/tomcat EXPOSE 8080 #CMD ["/usr/local/tomcat/bin/catalina.sh","run"] ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
9.3 编写配置文件docker-compose.yml
cd .. vim docker-compose.yml version: '3' #docker-compose版本 services: #定义servers tomcat: #容器名tomcat container_name: web2 hostname: tomcat #主机名tomcat build: context: ./tomcat dockerfile: Dockerfile #镜像构建dockerfile文件夹位置及名称 ports: - 1280:8080 #端口映射 networks: lnmp: ipv4_address: 172.19.0.10 #tomcat ip定义 networks: lnmp: driver: bridge ipam: config: - subnet: 172.19.0.0/16
docker-compose -f docker-compose.yml up -d
9.4 验证
docker ps -a docker-compose ps #必须在docker-compose.yml所在目录执行此命令 真机访问http://192.168.147.105:1280