Table of Contents
Docker-compose
1. Three major concepts of Docker-compose
2. YAML file format and writing considerations
1) When using YAML, you need to pay attention to the following matters
2) ymal file format
3) json format
3. Docker Compose configuration common fields
4. Four restart strategies of Docker-compose
5. Docker Compose common commands
6. Preparation of Docker-compose configuration file
Docker-compose
docker-compose is a tool for orchestrating and managing container clusters on a single machine. Developed using python, any platform that can run docker can also use docker-compose to orchestrate and manage containers.
The essence is to define the startup parameters and dependencies of multiple containers in the docker-compose configuration template file in yaml format, and use docker-compose to start and manage the container cluster according to the configuration of this template file.
1. Three major concepts of Docker-compose
Project project -> Contains one or more services service -> Contains startup parameters such as images, mapped ports, and environment variables of one or more containers
By default, the project directory name is used as the project name. It is supported to use -p or –project-name to specify the project name.
A docker-compose configuration template file must be included in the project directory. The default is docker-compose.yml. You can use -f or –file to specify the project configuration template file.
The configuration template file must contain one or more services. Each service includes the name, image, mapped port, environment variables, mount point, network mode, dependencies and other configuration parameters for container startup.
2. YAML file format and writing precautions
YAML is a markup language that can display data serialization format very 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 hashes are enclosed in curly braces {}.
1) Things to note when using YAML
- Case Sensitive
- Represent hierarchical relationships through indentation
- Tab key indentation is not supported, only spaces can be used for indentation
- The number of indented spaces does not matter, as long as the same level is left aligned, usually 2 spaces are indented at the beginning.
- Comment with # sign
- Indent 1 space after the symbol character, such as colon:, comma,, horizontal bar –
- If it contains special characters enclosed in single quotation marks (”), it will be treated as an ordinary string. Double quotation marks (“”): the special characters are used as the meaning they want to express.
2) ymal file format
The file name format uses .yml
or .yaml
as the suffix, and uses space indentation to indicate the hierarchical relationship of the fields. It is highly readable and easy for humans to manage.
#An expression in which a field key has multiple values Horizontal writing: test: ["test1", "test2", "test3"] Vertical writing: ntest: - "test1" - "test2" - "test3" #Format of multi-level fields Top-level/first-level fields: Secondary field 1: ["value1", "value2"] Secondary field 2: Third-level field: value object object type value: value: equivalent to echo helloworld! hello world! value: | value: equivalent to echo -e "hello\ world!" hello world! value: |- value: equivalent to echo -n helloworld! hello world! value: | + value: equivalent to echo -e "hello\ world!\ " hello world! value: > value: equivalent to echo "hello world!" hello world! name: &a zhangsan is equivalent to a=yaml book: *a is equivalent to echo $a , book: zhangsan
3) json format
The file name format uses .json as the suffix, and uses {} to represent the hierarchical relationship of the fields. The program interface has high parsing efficiency.
{ "field": "value"} { "debug": true } Boolean value type { "ht": "dashuaige" } String type { "heitui01": ["ht", "shellking", "paidaxing"], "heitui02": ["zhangsan", "lisi", "wangwu"] } { "websites": { first-level fields "memory": "512M", secondary field "disk": "100G", "cpu": {Level 3 field "name": ["inter", "amd"] } } }
3. Common fields for Docker Compose configuration
Fields | Description |
build | Specify the Dockerfile file name. To specify the Dockerfile file, you need to add it to the build tag Use the dockerfile tag in the child tag to specify the |
dockerfile | build image context path |
context | It can be the path of the dockerfile, or URL address pointing to the git repository |
image | Specify the image |
command | Execute the command, Override the default command executed after the container is started |
container_name | Specify the container name. Since the container name is unique, if you specify a custom name, you cannot scale the specified number of containers |
deploy | Specifies configuration related to deployment and running services, which can only be used in Swarm mode |
environment | Add environment variables |
networks | Join the network and reference the entries under top-level networks |
network_mode | Set the network mode of the container, such as host, bridge,… |
ports | Expose the container port, the same as -p, but the port cannot be lower than 60 |
volumes | Mount a host directory or command volume to the container, name it Volume To define the volume name in top-level volumes |
volumes_from | To mount the volume from another service or container, optional parameters: ro and: rw, only Version ‘2’ supported |
hostname | Container hostname |
sysctls | Set kernel parameters in the container |
links | Connect to another container, – service name [:service alias] |
privileged | is used to give root permissions to the container. Note that it is unsafe. true | false |
restart | Set restart policy, no, always, no-failure, unless-stopped |
depends_on | Define dependencies between services |
4. Four restart strategies of Docker-compose
1) no: The default policy is to not restart the container when it exits.
2) always: Always restart the container when the container exits.
3) no-failure: The container will only be restarted when the container exits abnormally (exit status is non-0).
4) unless-stopped: Always restart the container when the container exits, but does not consider containers that have been stopped when the Docker daemon is started.
5. Docker Compose common commands
Fields | Description |
build | Rebuild service |
ps | List containers |
up | Create and start containers |
exec | Execute commands in the container |
scale | Specify the number of service containers to start |
top | Show container process |
logs | View container output |
down | Delete containers, networks, data volumes and images |
stop/start/restart | stop/start/ Restart container |
6. Writing of Docker-compose configuration file
The docker-compose configuration file is written in yaml file format, so the yaml file format was introduced earlier.
Download and install Docker compose
Docker Compose is an independent product of Docker, so you need to install Docker and then install Docker Compose separately.
#download
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#View version
docker-compose –version
Prepare dependency files
mkdir -p /data/compose_nginx/nginx /opt/compose_nginx/wwwroot
cd /data/compose_nginx/nginx
cp nginx-1.12.0.tar.gz ./vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginxvimDockerfile
#Based on base image
FROM centos:7
#User Info
MAINTAINER this is nginx image
#Add environment package
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#Upload the nginx software compressed package and decompress it
ADD nginx-1.12.0.tar.gz /usr/local/src/
#Specify working directory
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
–prefix=/usr/local/nginx \
–user=nginx \
–group=nginx \
–with-http_stub_status_module & amp; & amp; make & amp; & amp; make install
ENV PATH /usr/local/nginx/sbin:$PATH
#Specify http and https ports
EXPOSE 80
EXPOSE 443
//method one:
RUN echo “daemon off;” >> /usr/local/nginx/conf/nginx.conf #Close nginx running in the background
#Add run.sh in the host to the container
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD [“/run.sh”]
//Method Two:
ENTRYPOINT [ “/usr/local/nginx/sbin/nginx”, “-g”, “daemon off;” ]echo “
this is test web
” > /opt/compose_nginx/wwwroot/index.html
Write the configuration file docker-compose.yml
vim /data/compose_nginx/docker-compose.yml
version: ‘3’
services:
nginx:
container_name: web1
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
– 1216:80
– 1217:443
networks:
lnmp:
ipv4_address: 172.18.0.10
volumes:
– ./wwwroot:/usr/local/nginx/html
networks:
lnmp:
driver: bridge
ipam:
config:
– subnet: 172.18.0.0/16
Start docker-compose
cd /data/compose_nginx/
docker-compose -f docker-compose.yml up -d #Start docker-compose
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeContainer orchestration (production environment k8s)kubelet, kubectl, kubeadm three-piece set 16941 people Currently studying the system