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

The webpage cannot be opened because the div element lacks a closing tag

At first I thought it was a speed issue, so I late...

What is em? Introduction and conversion method of em and px

What is em? em refers to the font height, and the ...

How to solve the problem that Docker container has no vim command

Find the problem Today, when I tried to modify th...

MySQL establishes efficient index example analysis

This article uses examples to describe how to cre...

React Diff Principle In-depth Analysis

Table of contents Diffing Algorithm Layer-by-laye...

Analyze the difference between ES5 and ES6 apply

Table of contents Overview Function signature Opt...

Summary of react basics

Table of contents Preface start React Lifecycle R...

Solution to the data asymmetry problem between MySQL and Elasticsearch

Solution to the data asymmetry problem between My...

Optimizing query speed of MySQL with tens of millions of data using indexes

1. The role of index Generally speaking, an index...

Install and use Git and GitHub on Ubuntu Linux

Introduction to Git Git is an open source version...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

An article tells you how to implement Vue front-end paging and back-end paging

Table of contents 1: Front-end handwritten paging...