docker之docker-composemo模板文件(二)

docker所支持的docker-compose版本

https://docs.docker.com/compose/compose-file/
注:可向下兼容

在这里插入图片描述

1.image

  • 指定镜像名称或镜像ID,如果没有就会自动拉取。

语法

image: 镜像名称
image: 镜像ID

验证

#本地有的镜像
docker images
REPOSITORY   TAG        IMAGE ID       CREATED       SIZE
tomcat       8.0-jre8   8391ef8f6ae4   2 years ago   463MB

#docker-compose.yaml文件
version: "2.8"
services:
  nginx :
    image: nginx:latest         #镜像名称方式           
  
  tomcat :
    image: 8391ef8f6ae4         #镜像ID方式
    

#启动过程
docker-compose up -d
Creating network "docker-composeyaml_default" with the default driver
Pulling nginx (nginx:latest)...
latest: Pulling from library/nginx
e1acddbe380c: Pull complete
e21006f71c6f: Pull complete
f3341cc17e58: Pull complete
2a53fa598ee2: Pull complete
12455f71a9b5: Pull complete
b86f2ba62d17: Pull complete
Digest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
Status: Downloaded newer image for nginx:latest
Creating docker-composeyaml_nginx_1  ... done
Creating docker-composeyaml_tomcat_1 ... done

结论
image可以使用“镜像名称”也可使用“镜像ID”

2.ports

  • 暴漏端口信息。

语法

注意:这里要用双引号圈起来,如果你的端口小于60,可能会引起错误,标准写法就是圈起来

 ports
   - "宿主机端口:容器端口"

验证

#docker-compose.yaml文件
version: "2.8"
services:
  nginx :
    image: nginx:latest             
    ports:
     - "90:80"          #"宿主机端口:容器端口"

  tomcat :
    image: 8391ef8f6ae4         
    ports:
     - "9090:8080"      #"宿主机端口:容器端口"
 
 #访问测试
 curl -I localhost:90
HTTP/1.1 200 OK
Server: nginx/1.21.1
Date: Sat, 21 Aug 2021 13:54:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 06 Jul 2021 14:59:17 GMT
Connection: keep-alive
ETag: "60e46fc5-264"
Accept-Ranges: bytes

curl -I localhost:9090
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 21 Aug 2021 13:55:03 GMT

3.container_name

  • 设置容器名称

语法

 container_name: 容器名称

验证

#docker-compose.yaml文件
version: "2.8"
services:
  nginx :
    container_name: nginx-test  #容器名称
    image: nginx:latest
    ports:
     - "90:80"          

  tomcat :
    container_name: tomcat-test #容器名称
    image: 8391ef8f6ae4
    ports:
     - "9090:8080" 

在这里插入图片描述

4.volumes

  • 数据卷映射

语法

volumes:
 - 宿主机路径:容器路径
 - ../:容器路径	#这是以docker-compose.yaml为相对路径

验证

docker-compose.yaml文件
version: "2.8"
services:
  nginx :
    container_name: nginx-test  
    image: nginx:latest
    ports:
     - "90:80"
    volumes:
     - /root/nginx.html:/usr/share/nginx/html/index.html   #数据卷映射
     #- ../nginx:/usr/share/nginx/html/index.html 这是以docker-compose为相对路径中心进行数据卷挂载容器

  tomcat :
    container_name: tomcat-test 
    image: 8391ef8f6ae4
    ports:
     - "9090:8080"

#宿主机文件内容
#cat /root/nginx.html 
docker-compose

在这里插入图片描述

5.networks

  • 配置容器连接的网络
  • 此部分以后在讲,网络部分知识太深。

6.environment

  • 设置环境变量

语法

environment:

验证

#docker-compose.yaml文件
 mysql :
    container_name: mysql-test
    image: mysql
    ports:
     - "3307:3306"
    environment:                        #环境变量
      - MYSQL_ROOT_PASSWORD=rensiyu     #MySQL密码

docker exec -it mysql-test bash
mysql -uroot -prensiyu

7.env_file

  • 与"environment"作用一样,是环境变量,不同的地方是将变量放置在一个文本内。

语法

env_file:
  - 路径 	#支持以docker-conmpose.yaml的相对路径与绝对路径

验证

#docker-compose.yaml
 mysql :
    container_name: mysql-test
    image: mysql
    ports:
     - "3307:3306"
    env_file:
     - ./mysql.env      #文件名称,注意,一定要以env结尾  
    
#mysql.env
MYSQL_ROOT_PASSWORD=rensiyu     #MySQL密码

docker exec -it 1b bash
root@1b191bb5b031:/# mysql -uroot -prensiyu  

8.depends_on

  • 解决容器启动依赖关系

语法

depends_on
  - 服务名	#在哪个容器之后启动

验证

docker-compose.yaml
version: "2.8"  
services:       
  nginx :      #服务名
    container_name: nginx-test  
    image: nginx:latest             
    ports:
     - "90:80"          
    volumes:
     - /root/nginx.html:/usr/share/nginx/html/index.html 
    depends_on:         
     - mysql            #在mysql服务之后启动

  tomcat :
    container_name: tomcat-test 
    image: 8391ef8f6ae4         
    ports:
     - "9090:8080"      
    depends_on:
     - nginx            #在nginx服务之后启动


  mysql :
    container_name: mysql-test
    image: mysql
    ports:
     - "3307:3306"
    env_file:
     - ./mysql.env    

启动顺序如下在这里插入图片描述

9.restart

这个没有什么可讲的了,跟run --restart=alway一样。

version: "2.8"
services:
  nginx :      
    container_name: nginx-test
    image: nginx:latest
    ports:
     - "90:80"
    volumes:
     - /root/nginx.html:/usr/share/nginx/html/index.html
    restart: always     #一直重启
    depends_on:
     - mysql            

10. build

语法

build:
  context: ../dockerfile			#指定上下位目录,以compose.yaml为相对路径,也可以用绝对路径的方式表达。
  dockerfile: dockerfile-httpd		#选择哪个dockerfile

验证

#docker-compose.yaml如下
  httpd:
    container_name: httpd
    build:
      context: ../dockerfile		#路径
      dockerfile: dockerfile-httpd	#选择build哪个dockerfile
    ports:
     - "81:80"
#路径及dockerfile内容
[root@localhost dockerfile]# pwd
/root/dockerfile
[root@localhost dockerfile]# cat dockerfile-httpd 
FROM centos:7
MAINTAINER rensiyu <ren_siyu3580.163.com>
RUN yum -y install httpd \
	&& echo dockerfile >/var/www/html/index.html \
	&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#测试
curl -I localhost:81
HTTP/1.1 200 OK
Date: Fri, 27 Aug 2021 12:41:17 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 27 Aug 2021 12:40:36 GMT
ETag: "b-5ca89ca3ed900"
Accept-Ranges: bytes
Content-Length: 11
Content-Type: text/html; charset=UTF-8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值