Common template commands of docker-compose.yml file

Note: when writing docker-compose.yml file, a space should be added after all colons (:) and dashes (-).

1,command

Overrides the default command executed after the container is started

command: echo "hello"

2,container_name

Specifies the container name. The project name will be used by default_ Service name_ Format such as serial number

container_name: docker-web-container

3,configs

For Swarm mode only

4,deploy

For Swarm mode only

5,devices

Specify device mapping relationships

devices:
  - "/dev/dir:/dev/dir"

6,depends_on

Solve the problems of container dependency, startup sequence and communication between containers.

7,links

Connect to another container. Note: this instruction is not recommended. It is recommended to use dependencies_ on.

You should use docker network to establish a network, and docker run --network to connect to a specific network.

Or use docker-compose.yml of version: '2' or later to directly define a custom network and use it.

8,dns

Custom DNS server. It can be a value or a list.

dns: 8.8.8.8
dns: 
  - 8.8.8.8
  - 114.114.114.114

9,environment

Set environment variables. You can use both array and dictionary formats. A variable with only a given name will automatically obtain the value of the corresponding variable on the host running Compose, which can be used to prevent unnecessary data disclosure.

environment:
   MYSQL_ROOT_PASSWORD: 666666

10,expose

The exposed port, but not mapped to the host, is only accessed by the connected service. Only internal ports can be specified as parameters.

11,extra_hosts

Similar to the – add host parameter in Docker, specify additional host name mapping information. An entry will be added to the / etc/hosts file in the service container after startup. For example: 8.8.8.8 Google DNS

12,healthcheck

Check the healthy operation of the container through the command

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3

13,image

Specify as the image name or image ID. if the image does not exist locally, Compose will try to pull the image

14,labels

Add Docker metadata information to the container. For example, you can add auxiliary description information to a container.

15,network_mode

Set the network mode. Use the same value as the – network parameter of docker run.

network_mode: "bridge"
network_mode: "host"
network_mode: "none"

16,networks

Configure the network to which the container is connected

networks:
  network-demo

17,ports

Expose port information in the format of host port: container port (HOST:CONTAINER), or just specify the port of the container (the host will randomly select the end).

ports:
      - "80:80"
      - "443:443"
      - "8081:8081"

18,volumes

Setting the mounting path of the data volume, you can set the host path, and support relative paths at the same time

volumes:
      - ../Site:/data/www:rw
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/cert:/etc/nginx/cert:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/phpcgi.conf:/etc/nginx/phpcgi.conf:ro
      - ./nginx/fastcgi.conf:/etc/nginx/fastcgi.conf:ro
      - ./nginx/pathinfo.conf:/etc/nginx/pathinfo.conf:ro
      - ../logs/nginx:/var/log/nginx

19,ulimits

Specifies the ulimits limit value for the container.

For example, the specified maximum number of processes is 65535, the specified number of file handles is 20000 (soft limit, the application can be modified at any time, and cannot exceed the hard limit) and 40000 (system hard limit, which can only be increased by root user)

ulimits:
  nproc: 65535
  nofile:
    soft: 20000
    hard: 40000

20,entrypoint

Specifies the entry file to execute after the service container is started

entrypoint: /code/entrypoint.sh

21,user

Specify the user name to run the application in the container

22,working_dir

Specify the working directory in the container

working_dir: /data/www

23,domainname

Specifies the domain name to search in the container

domainname: your_domain.com

24,hostname

Specifies the host name in the container

25,mac_address

Specifies the mac address in the container

mac_address: 01-02-22-0A-0B

26,privileged

Allow some privileged commands to run in the container

privileged: true

27,restart

Specifies that the restart policy after the container exits is always restart. In the production environment, the recommended configuration is always or unless stopped

restart: always

28,read_only

Mounting the root file system of the container in read-only mode means that the contents of the container cannot be modified

read_only: true

29,stdin_open

Open standard input to accept external input

stdin_open: true

30,tty

Simulate a pseudo terminal

tty: true

Keywords: Operation & Maintenance Docker Container docker compose

Added by paran0id Dan on Tue, 16 Nov 2021 10:25:31 +0200