docker-compose详解

选项

检查docker-compose文件语法

docker-compose config
docker-compose config -q 语法没问题不回显

创建容器不启动

docker-compose create

启动

docker-compose start

停止

docker-compose stop

创建并启动service

docker-compose up -d
不加-d是在当前运行

停止并删除容器、网络、图像和卷

删除docker-compose文件中的所有运行的容器和网络、图像、卷等。

docker-compose down

捕获docker-compose操作

docker-compose events --json
--json:表示将捕获的操作以json格式显示出来

进入容器

docker-compose exec nginx sh
其中nginx表示services中的nginx
version: '3'
services:
  nginx:
    image: nginx-base:1.16.1
    container_name: nginx-web1
    expose:
        - 80
        - 443
    ports:
        - "80:80"
        - "443:443"

查看yaml文件使用的镜像

docker-compose images

[root@localhost docker-compose]# docker-compose images
Container    Repository    Tag       Image Id       Size  
----------------------------------------------------------
nginx-web1   nginx-base   1.16.1   699b9fa70217   279.9 MB

查看yaml文件启动的容器

docker-compose ps

[root@localhost docker-compose]# docker-compose ps
   Name               Command            State                    Ports                  
-----------------------------------------------------------------------------------------
nginx-web1   sbin/nginx -g daemon off;   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

强制停止yaml文件运行中的容器

[root@localhost docker-compose]# docker-compose kill nginx
Killing nginx-web1 ... done

[root@localhost docker-compose]# docker-compose ps
   Name               Command             State     Ports
---------------------------------------------------------
nginx-web1   sbin/nginx -g daemon off;   Exit 137   
     
[root@localhost docker-compose]# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                        PORTS               NAMES
605ec97f696d        nginx-base:1.16.1            "sbin/nginx -g 'daem…"   22 minutes ago      Exited (137) 16 seconds ago                       nginx-web1

查看容器的启动日志

查看的是容器启动时的日志
docker-compose logs -f nginx

重新拉取镜像

#拉取不是启动
docker-compose pull

删除容器

docker-compose rm -f -s nginx
-f:强制删除
-s:先停止
-v:删除匿名数据卷

设置指定服务运行的容器个数

docker-compose scale nginx=2

docker-compose实例

nginx转发tomcat,redis session共享

准备文件

[root@localhost data]# tree 
.
├── conf
│   ├── nginx
│   │   └── nginx.conf
│   ├── redis
│   │   └── redis.conf
│   └── tomcat
│       ├── context.xml
│       └── server.xml
├── data
│   └── redis
│       ├── appendonly.aof
│       └── dump.rdb
├── logs
│   ├── nginx
│   │   ├── access.log
│   │   └── error.log
│   ├── redis
│   │   └── redis_6379.log
│   ├── tomcat-web1
│   │   ├── catalina.2021-06-23.log
│   │   ├── catalina.2021-06-28.log
│   │   ├── catalina.2021-07-06.log
│   │   ├── catalina.2021-07-07.log
│   │   ├── catalina.out
│   │   ├── host-manager.2021-06-23.log
│   │   ├── host-manager.2021-06-28.log
│   │   ├── host-manager.2021-07-06.log
│   │   ├── host-manager.2021-07-07.log
│   │   ├── localhost.2021-06-23.log
│   │   ├── localhost.2021-06-28.log
│   │   ├── localhost.2021-07-06.log
│   │   ├── localhost.2021-07-07.log
│   │   ├── localhost_access_log.2021-06-23.txt
│   │   ├── localhost_access_log.2021-06-28.txt
│   │   ├── localhost_access_log.2021-07-06.txt
│   │   ├── localhost_access_log.2021-07-07.txt
│   │   ├── manager.2021-06-23.log
│   │   ├── manager.2021-06-28.log
│   │   ├── manager.2021-07-06.log
│   │   └── manager.2021-07-07.log
│   └── tomcat-web2
│       ├── catalina.2021-06-23.log
│       ├── catalina.2021-06-28.log
│       ├── catalina.2021-07-06.log
│       ├── catalina.2021-07-07.log
│       ├── catalina.out
│       ├── host-manager.2021-06-23.log
│       ├── host-manager.2021-06-28.log
│       ├── host-manager.2021-07-06.log
│       ├── host-manager.2021-07-07.log
│       ├── localhost.2021-06-23.log
│       ├── localhost.2021-06-28.log
│       ├── localhost.2021-07-06.log
│       ├── localhost.2021-07-07.log
│       ├── localhost_access_log.2021-06-23.txt
│       ├── localhost_access_log.2021-06-28.txt
│       ├── localhost_access_log.2021-07-06.txt
│       ├── localhost_access_log.2021-07-07.txt
│       ├── manager.2021-06-23.log
│       ├── manager.2021-06-28.log
│       ├── manager.2021-07-06.log
│       └── manager.2021-07-07.log
└── web
    ├── nginx
    │   └── index.html
    ├── tomcat-web1
    │   └── app
    │       ├── index.jsp
    │       └── redis.jsp
    └── tomcat-web2
        └── app
            ├── index.jsp
            └── redis.jsp

17 directories, 56 files

docker-compose.yaml

version: '3'
services:
  nginx:
    image: nginx-base:1.16.1
    container_name: nginx-web1
    volumes:
        - /data/conf/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
        - /data/logs/nginx:/usr/local/nginx/logs
        - /data/web/nginx:/usr/local/nginx/html
    expose:
        - 80
    ports:
        - "80:80"
    links:
        - tomcat1
        - tomcat2

  tomcat1:
    image: tomcat-alpine-redis:8.5.42
    container_name: tomcat-web1
    volumes:
        - /data/conf/tomcat/server.xml:/usr/local/tomcat/conf/server.xml
        - /data/conf/tomcat/context.xml:/usr/local/tomcat/conf/context.xml
        - /data/logs/tomcat-web1:/usr/local/tomcat/logs
        - /data/web/tomcat-web1:/usr/local/tomcat/webapps
    expose:
        - 8080
    #ports:
    #    - "8081:8080"
    links:
        - redis1
  tomcat2:
    image: tomcat-alpine-redis:8.5.42
    container_name: tomcat-web2
    volumes:
        - /data/conf/tomcat/server.xml:/usr/local/tomcat/conf/server.xml
        - /data/conf/tomcat/context.xml:/usr/local/tomcat/conf/context.xml
        - /data/logs/tomcat-web2:/usr/local/tomcat/logs
        - /data/web/tomcat-web2:/usr/local/tomcat/webapps
    expose:
        - 8080
    #ports:
    #    - "8082:8080"
    links:
        - redis1

  redis1:
    image: redis-alpine-base:5.0.11
    container_name: redis-server1
    volumes:
        - /data/conf/redis/redis.conf:/etc/redis.conf
        - /data/logs/redis:/usr/local/redis/logs
        - /data/data/redis:/usr/local/redis/data
    expose:
        - 6379
    #ports:
    #    - "16379:6379"
服务启动停止
创建并启动容器
docker-compose up -d

停止
docker-compose stop

停止并删除容器
docker-compose down
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值