Introduction to Docker Compose
Docker Compose is a tool for defining and running multi-container applications;
Docker Compose defines multi-container docker applications through yml files;
Docker Compose uses a command to create or manage multiple containers according to the definition of yml files;
Docker Compose is It is used for Docker's multi-container control, which is a thing used to automate Docker. With Docker Compose, you can automate all complex Docker operations with one command.
Docker Compose can do the following:
- Provide tools for defining and running multiple docker container applications;
- Use yaml files to configure application services (docker-compse.yml);
- You can start all services according to dependencies with a simple command docker-compse up;
- All services can be stopped with a simple command docker-compose down;
- When a service needs it, it can be easily expanded through --scale;
Docker Compose has the following characteristics:
Higher portability, Docker Compose only needs one docker-compose up to start all services according to dependencies, and then use docker-compose down to easily disassemble them. Help us deploy complex applications more easily;
multiple isolated environments on a single host, Compose can use project names to isolate environments from each other, this brings multiple copies of the same environment running on a single computer, it prevents different The items and services of the Company interfere with each other;
Docker Compose installation
The latest version installed by Docker Compose is Docker Compose version v2.12.2. After installing Docker for Mac and Windows, Docker Compose has already been installed, and no manual installation is required. The installation method here is based on Cnetos of Linux. You can also refer to Official website to install.
Since Docker has been installed on the virtual machine before, you only need to install compose-plugin this time; the virtual machine version is as follows:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
Upgrade all packages and also upgrade the software and system kernel:
[root@localhost ~]# sudo yum update
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* extras: ftp.ksu.edu.tw
* updates: mirror.lzu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.6.8-3.1.el7 将被 升级
---> 软件包 containerd.io.x86_64.0.1.6.9-3.1.el7 将被 更新
---> 软件包 copy-jdk-configs.noarch.0.3.3-10.el7_5 将被 升级
---> 软件包 copy-jdk-configs.noarch.0.3.3-11.el7_9 将被 更新
---> 软件包 docker-ce.x86_64.3.20.10.18-3.el7 将被 升级
---> 软件包 docker-ce.x86_64.3.20.10.21-3.el7 将被 更新
---> 软件包 docker-ce-cli.x86_64.1.20.10.18-3.el7 将被 升级
---> 软件包 docker-ce-cli.x86_64.1.20.10.21-3.el7 将被 更新
---> 软件包 docker-ce-rootless-extras.x86_64.0.20.10.18-3.el7 将被 升级
---> 软件包 docker-ce-rootless-extras.x86_64.0.20.10.21-3.el7 将被 更新
---> 软件包 docker-scan-plugin.x86_64.0.0.17.0-3.el7 将被 升级
---> 软件包 docker-scan-plugin.x86_64.0.0.21.0-3.el7 将被 更新
---> 软件包 java-1.8.0-openjdk.x86_64.1.1.8.0.345.b01-1.el7_9 将被 升级
---> 软件包 java-1.8.0-openjdk.x86_64.1.1.8.0.352.b08-2.el7_9 将被 更新
---> 软件包 java-1.8.0-openjdk-headless.x86_64.1.1.8.0.345.b01-1.el7_9 将被 升级
---> 软件包 java-1.8.0-openjdk-headless.x86_64.1.1.8.0.352.b08-2.el7_9 将被 更新
---> 软件包 tzdata.noarch.0.2022d-1.el7 将被 升级
---> 软件包 tzdata.noarch.0.2022e-1.el7 将被 更新
---> 软件包 tzdata-java.noarch.0.2022d-1.el7 将被 升级
---> 软件包 tzdata-java.noarch.0.2022e-1.el7 将被 更新
Install compose-plugin:
Choose different installation commands according to different operating systems
[root@localhost ~]# sudo yum install docker-compose-plugin
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* extras: ftp.ksu.edu.tw
* updates: mirror.lzu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-compose-plugin.x86_64.0.2.12.2-3.el7 将被 安装
--> 解决依赖关系完成
Check the installation
[root@localhost ~]# docker compose version
Docker Compose version v2.12.2
Introduction to basic commands of Docker Compose
Docker Compose commands are basically similar to Docker, mainly for Docker Compose life cycle control, log format and other related commands, which can be helped through docker-compose --help.
[root@localhost ~]# docker compose --help
Usage: docker compose [OPTIONS] COMMAND
Docker Compose
Options:
--ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
--compatibility Run compose in backward compatibility mode
--env-file string Specify an alternate environment file.
-f, --file stringArray Compose configuration files
--profile stringArray Specify a profile to enable
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
Commands:
build Build or rebuild services
convert Converts the compose file to platform's canonical format
cp Copy files/folders between a service container and the local filesystem
create Creates containers for a service.
down Stop and remove containers, networks
events Receive real time events from containers.
exec Execute a command in a running container.
images List images used by the created containers
kill Force stop service containers.
logs View output from containers
ls List running compose projects
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 service containers
rm Removes stopped service containers
run Run a one-off command on 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
Run 'docker compose COMMAND --help' for more information on a command.
#构建建启动nignx容器
docker-compose up -d nginx
#进入nginx容器中
docker-compose exec nginx bash
#将会停止UP命令启动的容器,并删除容器
docker-compose down
#显示所有容器
docker-compose ps
#重新启动nginx容器
docker-compose restart nginx
#构建镜像
docker-compose build nginx
#不带缓存的构建
docker-compose build --no-cache nginx
#查看nginx的日志
docker-compose logs nginx
#查看nginx的实时日志
docker-compose logs -f nginx
#验证(docker-compose.yml)文件配置,
#当配置正确时,不输出任何内容,当文件配置错误,输出错误信息
docker-compose config -q
#以json的形式输出nginx的docker日志
docker-compose events --json nginx
#暂停nignx容器
docker-compose pause nginx
#恢复ningx容器
docker-compose unpause nginx
#删除容器
docker-compose rm nginx
#停止nignx容器
docker-compose stop nginx
#启动nignx容器
docker-compose start nginx