docker-compose入门教程

查看docker-compose版本

$ docker -v
Docker version 20.10.3, build 48d30b5
$
$ docker-compose -v
docker-compose version 1.8.0, build unknown
$

配置文件

验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。

docker-compose config -q

docker-compose.yml示例

构建镜像

# 构建镜像
# 构建所有镜像
docker-compose build

# docker-compose build nginx
#
# 不带缓存
# docker-compose build --no-cache nginx

构建镜像并启动容器

# 构建启动docker-compose.yml文件中的容器
# -d表示后台启动
docker-compose up -d
# 构建启动docker-compose.yml文件中的nignx容器
docker-compose up -d nginx

默认情况下,docker-compose up 会集中每个容器的输出,当存在时,所有的容器会停止,运行 docker-compose up -d 会在后台启动容器并使它们运行。
默认情况下,如果服务存在容器的话,docker-compose up 会停止并再创建它们(使用了 volumes-from 会保留已挂载的卷),
如果不想使容器停止并再创建的话,使用docker-compose up --no-recreate,如果有需要的话,这会启动任何停止的容器

删除容器

# 删除所有容器等
docker-compose down -v

查看所有容器信息

docker-compose ps

进入容器

docker-compose exec nginx bash

容器执行命令

# --no-deps 不启动关联容器
# --rm 执行完成后删除创建的容器
# nginx -v shell命令查看nginx版本
docker-compose run --no-deps --rm nginx nginx -v

查看容器日志

# 查看nginx的日志 
docker-compose logs nginx
# -f 实时查看
docker-compose logs -f nginx
# --tail 1000 查看最近1000行
docker-compose logs --tail 1000 -f nginx

# 以json的形式输出nginx的docker日志
docker-compose events --json nginx

启动与重启

# 暂停
docker-compose pause nginx
# 恢复暂停的容器
docker-compose unpause nginx

# 删除容器(删除前必须关闭容器)
docker-compose rm nginx

# 启动单个容器
docker-compose start nginx
# 停止单个容器
docker-compose stop nginx

# 重启所有容器
docker-compose restart
# 重启单个容器
docker-compose restart nginx

所有命令

$ docker-compose --help
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)
  --verbose                   Show more output
  -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 (for example if your docker host
                              is an IP address)

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  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
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pulls 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
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version
$

你可能感兴趣的:(docker,docker)