docker-compose命令

查看所有命令

执行下面命令,查看所有命令

docker-compose

执行结果如下:

Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  -c, --context NAME          Specify a context name
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

更多命令参见

https://www.jianshu.com/p/658911a8cff3

创建并启动containers

创建并启动docker-compose.yml或docker-compose.yaml文件中的service

docker-compose up   # 不推荐

执行结果如下:

注意: 打印详细日志,执行ctrl+c退出时容器会停止,一般用于 调试

创建网络

由上图可见,通过docker-compose创建容器时,也会 创建网络

查看docker网络:

docker network ls

执行结果如下:

创建一个新的docker网络,是bridge网络模式

查看网络详细信息

docker network inspect docker_tomcat_default

执行结果如下:

[
    {
        "Name": "docker_tomcat_default",
        "Id": "0d5e26732eb3ad42304de4ff115ecf244e5c62d7cc9343d61b5f09db51feaf71",
        "Created": "2020-07-27T10:12:09.684302068+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.30.0.0/16",
                    "Gateway": "172.30.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "docker_tomcat",
            "com.docker.compose.version": "1.26.2"
        }
    }
]

ip网段是172.30.0.0/16

查看linux系统网络

ip a

执行结果如下:

虚拟网卡 红框 部分0d5e26732eb3,与 docker网络0d5e26732eb3, 是一致的

注意网段

docker-compose 创建容器时,也会创建网络,该网络ip网段 与 默认bridge网络的ip网段,一般不同
docker run创建容器时,使用 默认bridge网络

2个容器的网段不一样,无法网络通信

解决方法

创建自定义bridge网络,在 docker-compose.yml文件中指定该网络,然后启动

通过docker run创建容器时,也指定该网络,见:
https://www.malaoshi.top/show_1EF5xlsxdwTZ.html

后台启动

正常启动,不会打印详细日志

docker-compose up -d

指定文件启动

要执行 xxx.yml文件时,按照下面格式指定:

docker-compose -f abc.yml up -d

列出 containers

列出docker-compose.yml或docker-compose.yaml文件中的 containers

docker-compose ps

停止service

停止docker-compose.yml或docker-compose.yaml文件中的service

docker-compose stop

停止service并删除

停止docker-compose.yml或docker-compose.yaml文件中的service,并删除containers, networks, images, and volumes

docker-compose down

启动service

启动docker-compose.yml或docker-compose.yaml文件中的service

docker-compose start

相当于docker exec

docker-compose exec 名字 bash

其中 名字 是在docker-compose.yml定义的名字


原文出处:/show_1EF5xCV2BIyn.html