目录

1、Docker Compose 简介

2、为什么要使用Docker Compose

3、Docker Compose安装使用(Linux)

3.1 下载

3.2 mkdir docker 文件夹目录


1、Docker Compose 简介

图片来源:.NET Core容器化之多容器应用部署@Docker-Compose – JavaShuo

官网简介Docker Compose overview | Docker Docs

根据官网的说明Compose一个定义运行容器Docker应用程序工具使用Compose,您可以使用YAML文件配置应用程序服务然后,只需一个命令,就可以配置创建启动所有服务

Compose的关键特性使其有效

因为在实际开发中,一个应用往往包含多个服务,而docker应用是作为一个容器只运行一个应用进程。因此如果要运行多个服务需要运行多个容器,compose出现就是为了有效地管理这些容器,包括定义创建启动等。它是一个能够编排多容器分布式部署工具。使用 compose 可以简化容器镜像构建以及容器的运行

本质上,compose 把 YAML 文件解析docker 命令的参数然后调用相应的 docker 命令行接口,从而将应用以容器化的方式管理起来。它通过解析容器间的依赖关系顺序地启动容器。而容器间的依赖关系由 YAML 文件中的 links 标记指定。(本段引自:https://www.jianshu.com/p/87d701d0801d

简单来说,就是使用compose能够docker操作更加简单和便于管理

下载地址(以v2.14.2版本为例):

docker-compose-linux-x86_64

[root@localhost software]# mkdir docker
[root@localhost software]# ls
canal  docker  gitlab  jdk  mysql  nexus  nginx  project  rabbitmq  redis  redis_sentinel

mv docker-compose-linuxx86_64  /usr/local/bin

[root@localhost bin]# chmod u+x docker-compose-linuxx86_64

[root@localhost bin]# ll

总用量 43868

-rwxr–r–. 1 root root 44920832 12月 1 14:15 docker-compose-linuxx86_64

进入bin目录

[root@localhost bin]# mv docker-compose-linux-x86_64 docker-compose

[root@localhost local]# cd bin/
[root@localhost bin]# ls
docker-compose
[root@localhost bin]# docker-compose —version
Docker Compose version v2.14.2

简单测试版

version: ‘2’
services:
  hello-compose01:
    build: .
    image: nginx
    container_name: nginx_01
    ports:
      – “10001:80”
  hello-compose02:
    build: .
    image: nginx
    container_name: nginx_02
    ports:
      – “10002:80”

图片来源:Docker-Compose简介 – 简书

运行测试

docker-compose的相关命令

  1. [root@localhost test]# docker-compose
  2. Usage: docker compose [OPTIONS] COMMAND
  3. Docker Compose
  4. Options:
  5. --ansi string Control when to print ANSI control characters
  6. ("never"|"always"|"auto") (default "auto")
  7. --compatibility Run compose in backward compatibility mode
  8. --env-file string Specify an alternate environment file.
  9. -f, --file stringArray Compose configuration files
  10. --parallel int Control max parallelism, -1 for unlimited (default -1)
  11. --profile stringArray Specify a profile to enable
  12. --project-directory string Specify an alternate working directory
  13. (default: the path of the, first specified, Compose file)
  14. -p, --project-name string Project name
  15. Commands:
  16. build Build or rebuild services
  17. convert Converts the compose file to platform's canonical format
  18. cp Copy files/folders between a service container and the local filesystem
  19. create Creates containers for a service.
  20. down Stop and remove containers, networks
  21. events Receive real time events from containers.
  22. exec Execute a command in a running container.
  23. images List images used by the created containers
  24. kill Force stop service containers.
  25. logs View output from containers
  26. ls List running compose projects
  27. pause Pause services
  28. port Print the public port for a port binding.
  29. ps List containers
  30. pull Pull service images
  31. push Push service images
  32. restart Restart service containers
  33. rm Removes stopped service containers
  34. run Run a one-off command on a service.
  35. start Start services
  36. stop Stop services
  37. top Display the running processes
  38. unpause Unpause services
  39. up Create and start containers
  40. version Show the Docker Compose version information
  41. Run 'docker compose COMMAND --help' for more information on a command.

创建

docker-compose up

查看使用docker-compose创建的容器

docker ps

容器和访问端口关系

编辑Dockerfile文件


 

 编辑Edit Configurations

服务台开启查看

创建运行compose

注意:运行compose up 后报错 可能是由于本机未安装 Docker Desktop 程序下载安装后需重启电脑,再次运行成功。

下载地址Docker Desktop: The #1 Containerization Tool for Developers | Docker

参考

Docker-Compose简介 – 简书

容器服务的深入应用及原理剖析(4)带你了解Docker Compose – 知乎

.NET Core容器化之多容器应用部署@Docker-Compose – JavaShuo

docker高级篇-docker-compose容器编排介绍及实战 – 知乎

「Linux云计算SRE课程」docker-compose简介及使用!

Docker入门之docker-compose – minseo – 博客园


1. docker常用命令总结docker常用命令总结_林隐w的博客-CSDN博客

2. docker搭建mysql主从数据库docker搭建mysql主从数据库-CSDN博客

3. SpringBoot整合Dockerfile:SpringBoot整合Dockerfile-CSDN博客


感谢阅读,码字不易,多谢点赞!如有不当之处,欢迎反馈指出,感谢!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注