Docker Compose installation tutorial
Refer to the rookie tutorial
1 Introduction
Docker Compose is Docker's assembly tool for creating and debugging multiple Docker containers and running them on the same Docker host. Docker Compose is based on YAML files, describing the relationship between multiple containers, as well as the configuration and environment variables required by each container.
Docker Compose can simplify the deployment of multiple containers, and also supports data transmission between multiple containers , including network connections between containers and service connections between containers, and can provide more flexible solutions during debugging and packaging . Docker Compose can create Docker containers on a local environment or cloud server and package them into a docker-compose file for easier deployment to a production environment.
Compose works in all environments: production, development, testing, and CI workflows. Three steps used by Docker Compose:
1. Define the application's environment using a Dockerfile for replication anywhere.
2. Use docker-compose.yml to define the services that make up the application so they can run together in an isolated environment.
3. Execute the docker-compose up command to get the entire application up and running.
In the Dockerfile, not only the operations that users need to perform in the container can be defined, but also the configuration required to run the software in the container can be defined, so software development and operation and maintenance can finally be unified on a configuration file. The operation and maintenance personnel can use the same Dockerfile to "reproduce" the exact same operating unit (Docker container) as that in the developer environment in different occasions.
2. Download and install
Method 1: GitHub official download and installation (recommended, slow speed but no error)
1. GitHub official download
curl -SL https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
To install other versions of Compose, replace v2.17.2.
Correspondence between Docker Compose and Docker version: https://docs.docker.com/compose/compose-file/compose-file-v3/
Docker Compose download address: https://github.com/docker/compose/releases
2. Grant execution permission: sudo chmod +x /usr/local/bin/docker-compose
3. Establish a soft connection: sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
4. Check the docker version:docker-compose --version
Method 2: Domestic source installation (not recommended, fast but error-prone)
1. DaoCloud domestic source download
curl -L http://get.daocloud.io/docker/compose/releases/download/v2.17.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2. Grant execution permission: sudo chmod +x /usr/local/bin/docker-compose
3. Establish a soft connection: sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
4. Check the docker version: docker-compose --version
This method may cause the following error:
Executing docker install script, commit:a8a6b338bdfedd7ddefb96fe3e7fe7d4036d945a
Warning: the “docker” command appears to already exist on this system.
If you already have Docker installed, this script can cause trouble, which is why we’re displaying this warning and provide the opportunity to cancel the installation.
If you installed the current Docker package using this script and are using it again to update Docker, you can safely ignore this message.
You may press Ctrl+C now to abort this script.
After the error is reported, you can uninstall docker-compose first: rm /usr/local/bin/docker-compose
then install method 1 to download and install.
3. Simple use case
Case requirements: deploy mysql, redis, nginx, nacos and other component containers at the same time.
1. Create a folder: mkdir -p /usr/local/mycompose
2. Enter the folder: cd /usr/local/mycompose
3. Create a docker-compose.yml file: vi docker-compose.yml
the content is as follows:
version: '3'
services:
redis1:
image: redis
ports:
- "6379:6379"
container_name: "redis1"
networks:
- dev
mysql1:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: "ut.123456"
ports:
- "3306:3306"
container_name: "mysql1"
networks:
- dev
nginx1:
image: nginx
ports:
- "8080:80"
container_name: "nginx1"
networks:
- dev
nacos1:
image: nacos/nacos-server
ports:
- "8848:8848"
container_name: "nacos1"
networks:
- dev
networks:
dev:
driver: bridge
pro:
driver: bridge
4. Start the container created in the configuration in the background: docker-compose up -d
5. Stop the container created in the configuration: docker-compose stop
6. Delete the container created in the configuration:docker-compose down
4. Introduction to the writing rules of docker-compose.yml
The docker-compose.yml configuration file is mainly divided into three layers: version, services, and networks.
Take the mysql service as an example to explain the meanings of commonly used fields:
version: '3'#第一层:compose的版本号
services: #第二层:服务配置信息
mysql1: #服务名
image: mysql #该服务所基于的镜像名
environment: #该服务的环境变量
MYSQL_ROOT_PASSWORD: "ut.123456"
ports: #该服务的暴露端口
- "3306:3306"
container_name: "mysql1" #容器名
networks: #该服务所加入的网络段
- dev
volumes: #挂载数据卷
- /platform/mysql/conf:/etc/my.cnf.d/my.cnf
- /platform/mysql/data:/var/lib/mysql:rw"
networks: #第三层:网络环境
dev:
driver: bridge
For more details on specific fields, please refer to: https://blog.csdn.net/qq_36148847/article/details/79427878
The following is an example of deployment:
5.Docker-compose common commands
The basic format of the docker compose command is:
docker-compose [-f …] [options] [COMMAND] [ARGS…]
常用命令(COMMAND)如下:
build 构建或重建服务
help 命令帮助
kill 杀掉容器
logs 显示容器的输出内容
port 打印绑定的开放端口
ps 显示容器
pull 拉取服务镜像
restart 重启服务
rm 删除停止的容器
run 运行一个一次性命令
scale 设置服务的容器数目
start 开启服务
stop 停止服务
up 创建并启动容器
更多命令可以查看帮助 `docker-compose -h`和`docker compose COMMAND --help`
Taking the nginx container operation as an example, common operations are as follows:
Order | describe |
---|---|
docker-compose ps | show all containers |
docker-compose build nginx | Build nginx image |
docker-compose up -d nginx | Build and start the nignx container |
docker-compose exec nginx bash | Log in to the nginx container |
docker-compose pause nginx | Pause the nignx container |
docker-compose unpause nginx | restore ningx container |
docker-compose start nginx | Start the nignx container |
docker-compose stop nginx | stop nignx container |
docker-compose restart nginx | Restart the nginx container |
docker-compose rm nginx | delete nginx container |
docker-compose down | Delete nginx container and mirror |
docker-compose logs -f nginx | View real-time logs of nginx |