Compose template directive

The directive used to write in the docker-comemess. yml file is called a template directive, and the object is the Services in compose.

1. Image

Function:

Specifies the image name or image ID. Compose will attempt to pull the image if it does not already exist locally.

Example:

version: "3.2"

services:
  tomcat:
    image: Tomcat: 8.0 jre8
Copy the code

2. build

Function:

If you use image, you usually edit the Dockerfile first, then manually build the Dockerfile into an image to the local library, and then use image to import the image to the local library.

Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose Compose

Example:

version: "3.2"

services:
  postilhub:
    build:
      # Context directory (relative path)
      context: ./postilhub
      # Dockerfile file name (default Dockerfile)
      dockerfile: Dockerfile
    The name of the container in which the image is packaged to run
    container_name: postilhub
    Map 9000 ports in the container
    ports: 
      - "9000:9000"
    # This service relies on the Tomcat service
    depends_on: "tomcat"
  tomcat:
	image: Tomcat: 8.0 jre8
Copy the code

3. ports

Function:

Expose port information.

Specify both the HOST port and the CONTAINER port (HOST: CONTAINER format), or just the CONTAINER port (the HOST will randomly select the port).

That’s minus p.

When using the HOST:CONTAINER format to map interfaces, you may get an error if you use CONTAINER ports less than 60 and do not put them in quotes, because YAML automatically parses xx: YY as base 60. To avoid this problem, it is recommended that numeric strings be quoted.

Example:

version: "3.2"

services:
  tomcat01:
    image: Tomcat: 8.0 jre8
      ports:
	  - "8080:8080"
  tomcat02:
    image: Tomcat: 8.0 jre8
      ports:
	- "8081:8080"
	- "5672:5672"
Copy the code

4. volumes

Function:

You can set the data VOLUME mounting path to HOT: CONTAINER or VOLUME: CONTAINER before:. You can also set the access mode to HOST: CONTAINER: Ro), all paths in this directive support relative paths.

That’s minus v.

Example:

version: "3.2"

services:
  tomcat:
    image: Tomcat: 8.0 jre8
    volumes:
      - /app/postilhub:/usr/local/tomcat/webapps
Copy the code

The above is mapped using the host path, if using the data volume name mapping, as follows

version: "3.2"

services:
  tomcat:
    image: Tomcat: 8.0 jre8
    volumes:
      - postilhub:/usr/local/tomcat/webapps
	  
# the statement
volumes:
  postilhub:
Copy the code

The shampooApp name must be declared separately, but the data volume name is not the same as the one we specified. Compose will first see which path the docker_comemage. yml file is in, for example, /home. For Compose, docker_compose. Yml is composed by combining the directory name with the custom volume name: shampoo_postilHub.

If you do not want to add a directory name, you can add the following configuration:

version: "3.2"

services:
  tomcat:
    image: Tomcat: 8.0 jre8
    volumes:
      - postilhub:/usr/local/tomcat/webapps
			
Declare data volume
volumes:
  postilhub:
    # Whether to use a custom data volume name
    external:
      true
Copy the code

Compose will not automatically create the data volume for us, we need to use the following command to manually create the data volume, and then start the Project.

docker volume create postilhub
Copy the code

5. network

Function:

If the container connection bridge is configured, the default bridge is the container connection bridge.

Equivalent to –network.

Example:

version: "3.2"

services:
  tomcat01:
    image: Tomcat: 8.0 jre8
    Tomcat01 service using postilHub bridge
    networks:
      - postilhub
  tomcat02:
    image: Tomcat: 8.0 jre8
    Tomcat02 service using postilHub bridge
    networks:
      - postilhub
			
Declare the bridge
networks:
  postilhub:
Copy the code

When the bridge is created, the bridge name is also a combination of the name of the directory where docker_comemage.yml is located and the custom bridge name.

If you do not want to add a directory name, you can add the following configuration:

version: "3.2"

services:
  tomcat01:
    image: Tomcat: 8.0 jre8
    Tomcat01 service using postilHub bridge
    networks:
      - postilhub
  tomcat02:
    image: Tomcat: 8.0 jre8
    Tomcat02 service using postilHub bridge
    networks:
      - postilhub
			
Declare the bridge
networks:
  postilhub:
    # Whether to use a custom data volume name
    external:
      true
Copy the code

The Compose setup will not automatically create the bridge for you. Instead, use the following command to manually create the bridge and start the Project.

docker network create -d bridge postilhub
Copy the code

6. container_name

Function:

Specify the container name.

Equivalent to –name.

Example:

version: "3.2"

services:
  tomcat01:
    container_name: tomcat01
    image: Tomcat: 8.0 jre8
  tomcat02:
    container_name: tomcat02
    image: Tomcat: 8.0 jre8
			
Declare the bridge
networks:
  postilhub:
Copy the code

7. environment

Function:

Set environment variables. You can use either an array or a dictionary.

That’s minus e.

Example:

version: "3.2"

services:
  mysql:
    container_name: mysql
    image: Mysql: 8.0
    environment:
      - MYSQL_ROOT_PASSWORD=123456
Copy the code

We can also use a dictionary:

version: "3.2"

services:
  mysql:
    container_name: mysql
    image: Mysql: 8.0
    environment:
      MYSQL_ROOT_PASSWORD: 123456
Copy the code

8. command

Function:

Overrides the commands executed by default when the container is started.

Example:

version: "3.2"

services:
  redis:
    image: Redis: 5.0.10
      command: "redis-server --appendonly yes"
Copy the code

Is equivalent to:

Docker run redis:5.0.10 redis-server --appendonly yesCopy the code

9. env_file

Function:

Gets environment variables from a file, either as individual file paths or as a list of file paths.

Example:

Follow the previous configuration of the environment command

version: "3.2"

services:
  mysql:
    container_name: mysql
    image: Mysql: 8.0
    # environment:
    # - MYSQL_ROOT_PASSWORD=123456
    env_file:
      - ./mysql.env
Copy the code

These passwords are sensitive information and should not be configured in plaintext in docker-comemage. yml.

Create the environment variable configuration file mysql.env in the docker-comemess. yml directory.

Docker Compose configuration files must end with env, follow the key=value format, and support # comments.

MYSQL_ROOT_PASSWORD=123456
Copy the code

10. depneds_on

Function:

Resolve container dependencies and startup priorities.

Example:

version: "3.2"

services:
  postilhub:
    image: postilhub:01
    depneds_on:
      - mysql
      - redis
  mysql:
    image: Mysql: 8.0
  redis:
    image: Redis: 5.0.10		
Copy the code

The above configuration indicates that the PostilHub service is dependent on mysql and Redis services. Postilhub will be started after mysql and Redis projects are started to a certain extent (not when mysql and Redis services are fully started).

Depends_on specifies the service name, not container_name.

Dependencies can have cascading effects, such as A dependent on B, B dependent on C, and A dependent on C.

11. healthcheck

Function:

Heartbeat detection, using commands to check whether the container is running properly.

Example:

version: "3.2"

services:
  postilhub:
    image: Postilhub: 1.0
    Perform a heartbeat check on the PostilHub service
    healthcheck:
  	  Send a curl request to the current Docker engine
      test: [ "CMD"."curl"."-f"."http://localhost" ]
      Specify response time
      interval: 1m30s
      # timeout
      timeout: 10s
      # retries
      retries: 3
Copy the code

If a remote Docker is deployed, change localhost to the IP address of the remote Docker.

12. sysctls

Function:

Configure container kernel parameters.

Example:

This configuration is not required. Some services may fail to start due to the limitations of the operating system parameters in the container and must be started by modifying the parameters in the container (for example, ES).

version: "3.2"

services:
  postilhub:
    image: Postilhub: 1.0
    Configure the system kernel for the container running the PostilHub service
    sysctls:
      - net.core.somaxconn=1024 
      - net.ipv4.tcp_syncookies=0 
Copy the code

13. ulimits

Function:

Specifies the number of processes that the container runs.

Example:

It is not required to be configured, but for some special service.

version: "3.2"

services:
  postilhub:
    image: Postilhub: 1.0
    # configure the number of processes in the container running postilHub
    ulimits:
      nproc: 65535
      nofile:
        soft: 20000
        hard: 40000
Copy the code

Compose instruction

Docker-comemage. yml is a directive that runs after the docker-comemage. yml file has been edited.

1. up

Function: Start all services of Project.

Example:

Start all services.

docker-compose up
Copy the code

This command can also start several services, and the Service name must be passed in

Docker-compose up service name 1 docker-compose up service name 2...Copy the code

By default, docker-compose up starts all containers in the foreground, if you want to start them in the background (Ctrl + C stops all containers)

docker-compose up -d
Copy the code

2. down

Function: Stops all services of Project and removes Bridges automatically created by template files (external Bridges are not removed), but data volumes are not removed.

Example:

Stop all services.

docker-compose down
Copy the code

3. exec

Function: Enter the container of a Service in Project.

Example:

docker-compose execThe service nameCopy the code

4. ps

Function: Lists all current containers in Project.

Example:

docker-compose ps
Copy the code

5. restart

Effect: Restart all services in Project.

Example:

docker-compose restart
Copy the code

This command can also restart several services. The Service name must be passed in

Docker-compose restart Docker-compose service name 1 Service name 2...Copy the code

6. rm

Delete all services from Project.

Example:

docker-compose rm
Copy the code

This command can also delete several services. The Service name must be passed in

Docker-compose RM service name 1 Service name 2...Copy the code

If you need to forcibly delete

Docker-compose rm -f docker-compose rm -f docker-compose rm -f docker-compose RM -f docker-compose RM -f docker-compose RMCopy the code

Delete the data volume if necessary

Docker-compose rm -v docker-compose rm -v docker-compose RM -v docker-compose RM -v docker-compose RM -v docker-compose RM -v docker-composeCopy the code

7. stop

Function: To close all services in a Project, just close the Service and do not delete the Service or bridge.

Example:

docker-compose stop
Copy the code

This command can also close several services. You must pass in the Service name

Docker-compose Stop Docker-compose Stop service name 1 Service name 2...Copy the code

8. top

Function: View the processes running in each Service container in Project.

Example:

docker-compose top
Copy the code

This command can also view several Service containers, and you must pass in the Service name

Docker-compose Top docker-compose service name 1 Docker-compose top docker-compose service name 2...Copy the code

9. pause

Pause all services in Project. Use ps to check whether the State of the Service is pause (up or Down).

Example:

docker-compose pause
Copy the code

This command can also suspend several services, and the Service name must be passed in

Docker-compose Pause service name 1 Service name 2...Copy the code

10. unpause

Effect: Resumes suspending all services in Project.

Example:

docker-compose unpause
Copy the code

This command can also suspend and resume several services. The Service name must be passed in

Docker-compose unpause service name 1 Service name 2... docker-compose unpause service name 1 Service name 2...Copy the code

11. logs

Function: View logs of all services in the Project.

Example:

docker-compose logs
Copy the code

This command can also view logs of several services. The Service name must be passed in

Docker-compose logs Docker-compose logs docker-compose logs docker-compose logs docker-compose logsCopy the code