Docker-Compose content introduction

1. Introduction to Docker-Compose

The Docker-Compose project is Docker's official open source project, responsible for realizing the rapid orchestration of Docker container clusters.

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 platform you are operating on supports the Docker API, you can use Compose for orchestration management.

2. Docker-Compose is used to implement rapid orchestration of Docker containers

With Docker-Compose, you do not need to use a shell script to start the container, but use a YAML file to configure all the services required by the application, and then use a command to create and start all services based on the YAML file configuration.

3. Introduction to Docker-compose template files

Compose allows users to define a set of associated application containers as a project through a docker-compose.yml template file (YAML format).

A Compose template file is a YAML file that defines services, networks, and volumes.

The default path of the Compose template file is docker-compose.yml in the current directory. You can use .yml or .yaml as the file extension.

The Docker-Compose standard template file should contain three parts: version, services, and networks. The most critical parts are services and networks.

version: '3.5'
services:
  nacos1:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos1
    privileged: true
    ports:
     - "8001:8001"
     - "8011:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M 
    env_file: 
     - ./nacos.env 
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_1}
        NACOS_APPLICATION_PORT: 8001
        NACOS_SERVERS: ${NACOS_SERVERS}     
    volumes:
     - ./logs_01/:/home/nacos/logs/
     - ./data_01/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
  nacos2:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos2
    privileged: true
    ports:
     - "8002:8002"
     - "8012:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M    
    env_file: 
     - ./nacos.env     
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_2}
        NACOS_APPLICATION_PORT: 8002
        NACOS_SERVERS: ${NACOS_SERVERS}
    volumes:
     - ./logs_02/:/home/nacos/logs/
     - ./data_02/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
  nacos3:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos3
    privileged: true
    ports:
     - "8003:8003"
     - "8013:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M    
    env_file: 
     - ./nacos.env 
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_3}
        NACOS_APPLICATION_PORT: 8003
        NACOS_SERVERS: ${NACOS_SERVERS}         
    volumes:
     - ./logs_03/:/home/nacos/logs/
     - ./data_03/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
networks:
   ha-network-overlay:
     external: true

4. YAML template file syntax

The default template file is docker-compose.yml. Each service defined in it must specify the image through the image instruction, or it can be automatically built through the build instruction (requires Dockerfile).

Most of the others are similar to docker run. If you use the build command, the options set in the Dockerfile (for example: CMD, EXPOSE, VOLUME, ENV, etc.) will be automatically obtained and do not need to be set again in docker-compose.yml.

docker-compose.yml syntax description

1、image

Specify as image name or image ID.

If the image does not exist, Compose will try to pull the image from the Internet, for example: image: ubuntu image: orchardup/postgresql image: a4bc65fd

Specify the image name of the service. If it does not exist locally, Compose will go to the warehouse to pull the image:

2、build

Specify the path to the folder where the Dockerfile is located. Compose will use it to automatically build this image and then use this image. build:./dir

3、command

Override the default command executed after the container is started. command: bundle exec thin -p 3000

4、links

To link to other service containers, you can use the service name (also as an alias) or the service alias (SERVICE:ALIAS)

 Note: Using an alias will automatically be created in /etc/hosts on the server, such as: 172.17.2.186 db, and the corresponding environment variables will also be created.

5、external_links

Containers linked to outside docker-compose.yml are not even containers managed by Compose. The parameter format is similar to links. external_links:

6、ports

Expose port information. Format

Host machine port: Container port (HOST:CONTAINER)

Or just specify the port of the container (the host machine will randomly assign the port).

Note: When using the HOST:CONTAINER format to map ports, if the container port you use is less than 60 you may get incorrect results because YAML will parse the xx:yy number format as base 60. So it is recommended to use string format.

7、expose

Exposed ports are different from posts in that expose can only expose ports and cannot be mapped to hosts. It is only used for external service connections; only internal ports can be specified as parameters.

8、volumes

Set the path to which the volume is mounted.

You can set the host path: container path (host:container) or add the access mode (host:container:ro). ro means readonly, read-only mode.

9、volunes_from

Mount all data volumes of another service or container.

10、environment

Set environment variables. Can be in array or dictionary format.

If only the name of a variable is given, its value on the Compose host will be automatically loaded, which can be used to prevent unnecessary data leakage.

11、env_file

Get environment variables from a file, which can be a separate file path or a list. If a template file is specified through docker-compose -f FILE, the path in env_file will be based on the template file path. If any variable name conflicts with the environment directive, the latter shall prevail.

Each line in the environment variable file must have a comment, and comment lines starting with # are supported.

12、extends

Expand services based on existing services. For example, we already have a webapp service, and the template file is common.yml.

# common.yml
webapp:
build: ./webapp
environment:
 - DEBUG=false
 - SEND_EMAILS=false
编写一个新的 development.yml 文件,使用 common.yml 中的 webapp 服务进行扩展。 development.yml
 
web:
extends:
file: common.yml
service: 
  webapp:
    ports:
      - "8080:80"
    links:
      - db
    envelopment:
      - DEBUG=true
   db:
    image: mysql:5.7

 The latter will automatically inherit the webapp service and related environment variables in common.yml.

13、net

Set network mode. Use the same value as the docker client's --net parameter.

Docker will automatically create three networks for each node: The network name acts as bridge. The network that the container is connected to by default is the docker0 network that is installed by default in all Docker installations. None. The container's customized network stack host. Add a container on the host network stack. The container The network configuration in will be the same as that of the host. Appendix: Operation name command to create a network docker network create -d bridge mynet View network list docker network ls

14、pid

Share the process namespace with the host system. Containers with this option turned on can access and operate each other through the process ID.

15、dns

Configure DNS server. Can be a value or a list.

dns: 8.8.8.8
dns:
 - 8.8.8.8
 - 9.9.9.9

16、cap_add,cap_drop

Add or drop the Linux capabilities of the container (Capability).

17、dns_search

Configure DNS search domain. Can be a value or a list.

dns_search: example.com
dns_search:
 - domain1.example.com
 \ - domain2.example.com
working_dir, entrypoint, user, hostname, domainname, mem_limit, privileged, restart, stdin_open, tty, cpu_shares

 These are similar to the options supported by docker run.

18、healthcheck

Health check, this is very necessary. Wait until the service is ready before going online to avoid temporary inaccessibility during the update process.

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost/alive"]
  interval: 5s
  timeout: 3s

 In fact, in most cases health check rules will be written in Dockerfile:

FROM nginx
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
HEALTHCHECK --interval=5s --timeout=3s CMD curl -f http://localhost/alive || exit 1

19、depends_on

Dependent services are started first, for example:

depends_on:
  - redis

20、deploy

Deployment-related configurations are all under this node, for example:

deploy:
  mode: replicated
  replicas: 2
  restart_policy:
    condition: on-failure
    max_attempts: 3
  update_config:
    delay: 5s
    order: start-first # 默认为 stop-first,推荐设置先启动新服务再终止旧的
  resources:
    limits:
      cpus: "0.50"
      memory: 1g
deploy:
  mode: global # 不推荐全局模式(仅个人意见)。
  placement:
    constraints: [node.role == manager]

If there are no special services, the configuration of each node above can meet most deployment scenarios.

docker-compose.yml实例
version: '3.5'
services:
  nacos1:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos1
    privileged: true
    ports:
     - "8001:8001"
     - "8011:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M 
    env_file: 
     - ./nacos.env 
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_1}
        NACOS_APPLICATION_PORT: 8001
        NACOS_SERVERS: ${NACOS_SERVERS}     
    volumes:
     - ./logs_01/:/home/nacos/logs/
     - ./data_01/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
  nacos2:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos2
    privileged: true
    ports:
     - "8002:8002"
     - "8012:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M    
    env_file: 
     - ./nacos.env     
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_2}
        NACOS_APPLICATION_PORT: 8002
        NACOS_SERVERS: ${NACOS_SERVERS}
    volumes:
     - ./logs_02/:/home/nacos/logs/
     - ./data_02/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
  nacos3:
    restart: always
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos3
    privileged: true
    ports:
     - "8003:8003"
     - "8013:9555"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 1024M    
    env_file: 
     - ./nacos.env 
    environment:
        NACOS_SERVER_IP: ${NACOS_SERVER_IP_3}
        NACOS_APPLICATION_PORT: 8003
        NACOS_SERVERS: ${NACOS_SERVERS}         
    volumes:
     - ./logs_03/:/home/nacos/logs/
     - ./data_03/:/home/nacos/data/
     - ./config/:/home/nacos/config/
    networks:
      - ha-network-overlay
networks:
   ha-network-overlay:
     external: true

docker-compose run --no-deps --rm php-fpm php -v does not start the associated container in php-fpm, and the container executes php -v and deletes the container after the execution is completed.

1、Docker-Compose

docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]

The command options are as follows:

-f, --file FILE specifies the Compose template file, which defaults to docker-compose.yml and can be specified multiple times.
-p, --project-name NAME specifies the project name. By default, the name of the directory will be used as the project name.
-x-network-driver Use Docker's pluggable network backend feature (requires Docker 1.9+ version) -x-network-driver DRIVER specifies the network backend driver, the default is bridge (requires Docker 1.9+ version) -verbose output
updates Multiple debugging information
-v, --version print version and exit

2、docker-compose up

This command must be remembered. It must be used every time it is started. As long as those who learn to use it remember this.

docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options include:

-d Run the service container in the background
–no-color Do not use colors to distinguish the control output of different services
–no-deps Do not start the container linked to the service
–force-recreate Forces to recreate the container, cannot be used together with –no-recreate –no-recreate If the container already exists, do not recreate it. Cannot be used with –force-recreate
–no-build Do not automatically build missing service images
–build Build the service image before starting the container
–abort-on-container-exit Stop all containers. If any container is stopped, -t cannot be used together with -d
, –timeout TIMEOUT timeout when stopping the container (default is 10 seconds)
–remove-orphans deletes containers in the service that are not defined in the compose file
– scale SERVICE=NUM Set the number of service running containers, which will overwrite the parameter specified by scale in compose
docker-compose up Start all services
docker-compose up -d Start all services in the background
-f Specify the Compose template file to use, default It is docker-compose.yml and can be specified multiple times.
docker-compose -f docker-compose.yml up -d

3、docker-compose ps

docker-compose ps [options] [SERVICE...]

docker-compose ps lists all containers currently in the project

4、docker-compose stop

docker-compose stop [options] [SERVICE...]

Options include:

-t, –timeout TIMEOUT timeout when stopping the container (default is 10 seconds)
docker-compose stop stops the running container and can be started again through docker-compose start

5、docker-compose -h

docker-compose -h 查看帮助

6、docker-compose down

docker-compose down [options]

Stop and delete containers, networks, volumes, and images.
Options include:

–rmi type, delete the image, the type must be: all, delete all the images defined in the compose file; local, delete the image with an empty image name
-v, –volumes, delete the ones already defined in the compose file and anonymously attached Data volumes on the container
–remove-orphans, delete containers in the service that are not defined in compose
docker-compose down, disable the removal of all containers and network related

7、docker-compose logs

docker-compose logs [options] [SERVICE...]

View the output of the service container.
By default, docker-compose will use different colors for different service outputs to distinguish them. Color can be turned off with --no-color.

8、docker-compose build

docker-compose build [options] [--build-arg key=val...] [SERVICE...]

Build (rebuild) the service container in the project.
Options include:

–compress Build the context through gzip compression
–force-rm Delete temporary containers during the build process
–no-cache Do not use cache during image building
–pull Always try to obtain an updated version of the image through a pull operation
-m, –memory MEM Set the memory size for the built container
–build-arg key=val Set the build-time variable for the service
Once the service container is built, it will have a tag name. You can run docker-compose build in the project directory at any time to rebuild the service

9、docker-compose pull

docker-compose pull [options] [SERVICE...]

Pull the image that the service depends on.
Options include:

–ignore-pull-failures, ignore errors during the image pulling process
–parallel, pull multiple images at the same time
–quiet, do not print progress information during the image pulling process

10、docker-compose restart

docker-compose restart [options] [SERVICE...]

Restart the services in the project.
Options include:

-t, –timeout TIMEOUT, specifies the timeout to stop the container before restarting (default is 10 seconds)

11、docker-compose rm

docker-compose rm [options] [SERVICE...]

Delete all (stopped) service containers.
Options include:

-f, -force, force direct deletion, including non-stopped containers
-v, delete the data volume mounted by the container
docker-compose rm deletes all (stopped) service containers. It is recommended to execute the docker-compose stop command first to stop the container.

12、docker-compose start

docker-compose start [SERVICE...]

Start an existing service container.

13、docker-compose run

docker-compose run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

Execute a command on the specified service.
docker-compose run ubuntu ping www.baidu.com Execute a ping command on the specified container.

14、docker-compose scale

docker-compose scale web=3 db=2 sets the number of containers running the specified service. Set the quantity through the parameter service=num

15、docker-compose pause

docker-compose pause [SERVICE...]

Pause a service container

16、docker-compose kill

docker-compose kill [options] [SERVICE...]

Force the service container to stop by sending a SIGKILL signal. Supports specifying the signal to be sent through the -s parameter, for example, sending the SIGINT signal through the following command: docker-compose kill -s SIGINT

17、dokcer-compose config

docker-compose config [options]

Verify and view the compose file configuration.
Options include:

–resolve-image-digests marks the image label as digest
-q, –quiet only verifies the configuration and does not output. When the configuration is correct, nothing is output. When the file configuration is incorrect, an error message is output.
–services prints service names, one per
line –volumes prints data volume names, one per line.

18、docker-compose create

docker-compose create [options] [SERVICE...]

Create a container for the service.
Options include:

–force-recreate: Recreate the container, even if the configuration and image have not changed, incompatible with –no-recreate parameter
–no-recreate: If the container already exists, no need to recreate it, incompatible with –force-recreate parameter
–no-build: No image is created, even if –build is missing
: before creating the container, generate the image
19. docker-compose exec

docker-compose exec [options] SERVICE COMMAND [ARGS...]

Options include:

-d detached mode, run commands in the background.
–privileged Get privileges.
–user USER specifies the running user.
-T disables allocating TTY. By default, docker-compose exec allocates TTY.
–index=index, when a service has multiple containers, you can use this parameter to log in to any service under the service, for example: docker-compose exec –index=1 web /bin/bash, the web service contains multiple containers
20. docker-compose port

docker-compose port [options] SERVICE PRIVATE_PORT

Displays the public port to which a container port is mapped.
Options include:

–protocol=proto, specify the port protocol, TCP (default value) or UDP
–index=index, if there are multiple containers for the agreed service, specify the sequence number of the command object container (default is 1)

21、docker-compose push

docker-compose push [options] [SERVICE...]

Push service mirror.
Options include:

--ignore-push-failures Ignore errors during image push

22、docker-compose unpause

docker-compose unpause [SERVICE...]

Resume a suspended service.

23、docker-compose version

docker-compose version

Print version information.

Summarize

docker-compose is an open source docker tool that simplifies the management of complex container environments. docker-compose combines Swarm and docker process container deployment to easily deploy an environment. The specific process is as follows: Use of docker-compose v2 version Official documentation: https://docs.docker.com/compose/compose-file/#version-2 docker-compose is written in python, so we install it using pip install doke

Guess you like

Origin blog.csdn.net/lyhshs/article/details/130819339
Recommended