Detailed explanation of common template commands in docker-compose.yml files

Detailed explanation of common template commands in docker-compose.yml files

Note: When writing the docker-compose.yml file, all colons (:) and dashes (-) need to be followed by a space.

1. command

Override the default command executed after the container starts

command: echo "hello"

2. container_name

Specify a container name. By default, the format of project name_service name_serial number will be used.

container_name: docker-web-container

3. configs

Only used in Swarm mode

4. deploy

Only used in Swarm mode

5. devices

Specify device mapping

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

6. depends_on

Solve the problems of container dependencies, startup order, and communication between containers.

7. links

Connect to other containers. Note: This directive is deprecated in favor of depends_on.

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

Or use version: '2' and higher of docker-compose.yml to define a custom network directly and use that.

8. DNS

Custom DNS servers. Can be a single 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 either array or dictionary format. Variables with a given name will automatically get the value of the corresponding variable on the host running Compose, which can be used to prevent unnecessary data leakage.

environment:
MYSQL_ROOT_PASSWORD: 666666

10. expose

The port is exposed but not mapped to the host machine and is only accessible to 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 started service container. For example: 8.8.8.8 googledns

12. Health check

Check whether the container is running healthily by command

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

13. Image

Specify 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 information to the 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, using the format HOST:CONTAINER, or just specify the container's port (the host will choose a random port).

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

18. Volumes

The path where the data volume is mounted can be set to the host path, and relative paths are also supported

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

Specify the ulimits limit value for the container.

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

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

20. entrypoint

Specify the entry file to be executed after the service container is started

entrypoint: /code/entrypoint.sh

21. user

Specify the user name for running the application in the container

22. working_dir

Specify the working directory in the container

working_dir: /data/www

23.domainname

Search domain name in specified container

domainname: your_domain.com

24. hostname

Specify the host name in the container

25. mac_address

Specify the mac address in the container

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

26. privileged

Allows some privileged commands to run in the container

privileged: true

27. restart

Specifies that the restart policy after the container exits is always restarted. In a production environment, it is recommended to configure it to always or unless-stopped

restart: always

28. read_only

Mount the container's root file system in read-only mode, which means that the container contents 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

This is the end of this article about commonly used template commands for docker-compose.yml files. For more relevant docker-compose.yml template command content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker-compose installation yml file configuration method

<<:  Discussion on image path issues in css (same package/different package)

>>:  Detailed explanation of the application and difference between filter attribute and backdrop-filter in CSS

Recommend

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

Detailed explanation of Nodejs array queue and forEach application

This article mainly records the problems and solu...

Detailed steps to install mysql 8.0.18-winx64 on win10

1. First go to the official website to download t...

JS ES new features: Introduction to extension operators

1. Spread Operator The spread operator is three d...

5 Ways to Send Emails in Linux Command Line (Recommended)

When you need to create an email in a shell scrip...

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

How to install and configure the Docker Compose orchestration tool in Docker.v19

1. Introduction to Compose Compose is a tool for ...

How to use HTML+CSS to create TG-vision homepage

This time we use HTML+CSS layout to make a prelim...

Script to quickly list all host names (computer names) in the LAN under Linux

Recently, I have a need to list all host names in...