Table of contents
1. Introduction to Docker-compose
2. YAML file format and preparation precautions
3. Docker Compose configuration common fields
Fourth, Docker-compose common commands
introduction
Dockerfile allows users to manage a single application container, while Compose allows users to define a set of associated application containers (called a project) in a template (YAML), such as a Web server plus backend. Database service containers, etc.
1. Introduction to Docker-compose
The Docker-compose project is an official Docker open source project based on Python, which is responsible for the rapid orchestration of Docker container clusters.
Docker-compose divides the managed containers into three layers, namely project, service and container.
- All files in the Docker-Compose running directory (docker-compose.yml, extends file or environment variable file, etc.) form a project. If there is no special specified project name, it is the current directory name.
- A project can contain multiple services, and each service defines the image, parameters, and dependencies of the container running.
- A service can include multiple container instances. Docker-compose does not solve the problem of load balancing, so it is necessary to use other tools to realize service discovery and load balancing, such as Consul
The project configuration file of Docker-Compose is docker-compose.yml by default, which can be accessed through environment variable The COMPOSE_FILE or -f parameter customizes the configuration file, which defines multiple dependent services and the container that each service runs.
Using a Dockerfile template file allows users to easily define a single application container. At work, there are often situations where multiple containers are required to cooperate with each other to complete a certain task. For example, to implement a Web project, in addition to the Web service container itself, it is often necessary to add a back-end database service container, and even a load balancing container.
Compose allows users to define a set of associated application containers as a project through a single docker-compose.yml template file (YAML format).
The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage containers. Therefore, as long as the operating platform supports the Docker API, you can use Compose on it for orchestration management.
2. YAML file format and preparation precautions
2.1, YAML file format
- YAML is a markup language that can display data serialization format intuitively and is highly readable.
- Similar to json data description language, the syntax is much simpler than json.
- YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed in square brackets [ ], and hash es are enclosed in curly braces { }.
2.2. Notes on YAML format
- tab indentation is not supported, only spaces are used for indentation
- Usually indent 2 spaces at the beginning
- Indent 1 space after characters, such as colon : , comma , , dash -
- Comment with #
- If it contains special characters, use single quotation marks '' as ordinary characters; if use double quotation marks "", it means the special characters themselves
- Boolean values must be enclosed in quotes ""
- Case sensitive
2.3, YAML data structure
object: dictionary of key-value pairs animal: pets array: A set of ordered lists - cat - dog - goldfish Boolean value debug: "true" debug: "false" #Yaml example languages: #sequence mapping - Java - Golang - Python websites: #map to map Baidu: www.baidu.com Wangyi: www.163.com Souhu: www.souhu.com #or languages: ["Java","Golong","Python"] websites: Baidu: www.baidu.com Wangyi: www.163.com Souhu: www.souhu.com #Json format { languages: [ 'Java', 'Golong', 'Python', ], websites: [ Baidu: 'www.baidu.com', Wangyi: 'www.163.com', Souhu: 'www.souhu.com', ] }
3. Docker Compose configuration common fields
field | describe |
build | Specify the Dockerfile file name (the Dockerfile file to be specified needs to be specified with the dockerfile tag in the child tag of the build tag) |
dockerfile | Build image context path |
context | It can be the dockerfile path, or the url address where the git repository is executed |
image | Specified mirror (already exists) |
command | Executing the command will override the default command executed after the container is started (it will override the CMD command of the Dockerfile) |
container_name | Specify the container name. Since the container name is unique, if you specify a custom name, you cannot scale to specify the number of containers |
deploy | Specify the configuration related to deploying and running the service, which can only be used in Swarm mode |
environment | add environment variable |
networks | Join the network, refer to the entry under the top-level networks |
network-mode | Set the network mode of the container |
ports | Expose the container port, same as -p, but the port cannot be lower than 60 |
volumes | Mount a host directory or command volume to the container, name the volume to define the volume name at the top-level volumes |
volumes_from | Mount volume from another service or container, optional parameters :ro and :rw, (only supported in version '2') |
hostname | hostname |
hostname | Setting kernel parameters inside the container |
links | connect to another container, - service name [ : ] |
privileged | Used to give the container root permission, note that it is not safe, true |
restart | The restart policy defines whether to restart the container; 1.no, the default policy, does not restart the container when the container exits. 2.on-failure, when the container exits abnormally (exit status is not 0), the container will be restarted. 3.on-failure: 3. Restart the container when the container exits abnormally, up to 3 times. 4.always, always restart the container when the container exits. 5.unless-stopped, always restarts the container when the container exits, but does not consider containers that were stopped when the Docker daemon started. |
depends_on | This tag is used to resolve container dependencies and start sequence issues. If you start the application container, you need to start the database container php:depends_on:- apache- mysql |
Fourth, Docker-compose common commands
field | describe |
---|---|
build | rebuild the service |
ps | list containers |
up | Create and start containers |
exec | Execute commands inside the container |
scale | Specify the number of service containers to start |
top | Show running container processes |
logs | View the output of the service container |
down | Delete containers, networks, data volumes and images |
stop/start/restart | stop/start/restart service |
Five, compose deployment
Docker-Compose is an independent product of Docker, so you need to install Docker Compose separately after installing Docker.
//Environment deployment All hosts install the docker environment (the content is the docker foundation) yum install docker-ce -y //Download compose (upload docker_compose) curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose cp -p docker-compose /usr/local/bin/ chmod +x /usr/local/bin/docker-compose mkdir /root/compose_nginx yum install -y tree vim /root/compose_nginx/docker-compose.yml version: '3' services: nginx: hostname: nginx build: context: ./nginx dockerfile: Dockerfile ports: - 1216:80 - 1217:443 networks: - cluster volumes: - ./wwwroot:/usr/local/nginx/html networks: cluster:
[root@docker /]# mkdir /opt/compose_nginx [root@docker /]# vim /opt/compose_nginx/docker-compose.yml [root@docker /]# vim /opt/compose_nginx/docker-compose.yml [root@docker /]# cd /opt/compose_nginx/ [root@docker compose_nginx]# ls docker-compose.yml [root@docker compose_nginx]# mkdir nginx [root@docker compose_nginx]# mkdir wwwroot [root@docker compose_nginx]# ls docker-compose.yml nginx wwwroot [root@docker compose_nginx]# echo "i am superman" > wwwroot/index.html [root@docker compose_nginx]# cat wwwroot/index.html i am superman [root@docker compose_nginx]# cd nginx [root@docker nginx]# ls nginx-1.22.0.tar.gz [root@docker nginx]#
[root@docker nginx]# vim Dockerfile FROM centos:7 as build ADD nginx-1.22.0.tar.gz /mnt WORKDIR /mnt/nginx-1.22.0 RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \ yum clean all && \ sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \ ./configure --prefix=/usr/local/nginx &> /dev/null && \ make &>/dev/null && \ make install &>/dev/null && \ rm -rf /mnt/nginx-1.22.0 FROM centos:7 EXPOSE 80 VOLUME ["/usr/local/nginx/html"] COPY --from=build /usr/local/nginx /usr/local/nginx CMD ["/usr/local/nginx/sbin/nginx","daemon off;"]
[root@docker compose_nginx]# tree ./ ./ ├── docker-compose.yml ├── nginx │ ├── dockerfile │ └── nginx-1.22.0.tar.gz └── wwwroot └── index.html 2 directories, 4 files
docker-compose -f docker-compose.yml up -d docker network ls
6. Summary
The project configuration file of Docker-Compose defaults to docker-compose.yml
Using a Dockerfile template file allows users to easily define a single application container.
Compose allows users to define a set of associated application containers as a project through a single docker-compose.yml template file (YAML format).
docker-compose is divided into three layers: project, service, container
project: represents a project composed of multiple service s. The default is to use the name of the working directory as the project name of the project
service: A service can contain one or more containers, in which parameters such as network mode port mirroring data volumes can be defined
container: can be implemented directly by running an existing image, or by building an image through dockerfile