docker-compose command

YAML template file syntax

The default template file is docker-compose.yml, and each service defined in it must be automatically built by specifying an image or build command (requires a Dockerfile) through the image directive.
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 acquired, and there is no need to set them again in docker-compose.yml.

1、image

Specified as image name or image ID. If the mirror does not exist, Compose will try to pull the mirror from the internet, for example:

image: ubuntu
image: orchardup/postgresql
image: a4bc65fd

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

Overrides the default command executed after the container starts.

command: bundle exec thin -p 3000

4、links

Link to other service containers, either using the service name (also as an alias) or the service alias (SERVICE:ALIAS)

links:
 - db
 - db:database
 - redis

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 variable will also be created.

5、external_links

Links to containers outside of docker-compose.yml that are not even managed by Compose. The parameter format is similar to links.

external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_2:sqlserver

6、ports

Expose port information.
Host machine port: The container port (HOST:CONTAINER) format or just specify the container port (the host machine will randomly assign the port).

ports:
 - "3306"
 - "8080:80"
 - "127.0.0.1:8090:8001"

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

7、expose

Expose the port. Unlike posts, expose can only expose the port and cannot be mapped to the host. It is only used for external service connections; only the internal port can be specified as a parameter.

expose:
 - "3000"
 - "8000"

8、volumes

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

volumes:
 - /var/lib/mysql:/var/lib/mysql
 - /configs/mysql:/etc/configs/:ro

9、volunes_from

Mount all data volumes of another service or container.

volumes_from:
 - service_name
 - container_name

10、environment

Set environment variables. Can belong to either an array or a dictionary format.
If only the name of the variable is given, its value on the Compose host will be automatically loaded, which can be used to prevent unnecessary data leakage.

environment:
 - RACK_ENV=development
 - SESSION_SECRET

11、env_file

Get environment variables from a file, either as individual file paths or as a list.
If a template file is specified with docker-compose -f FILE, the path in env_file will be based on the template file path.
If there are variable names that conflict with the environment directive, the latter will prevail.

env_file: .env
env_file:
 - ./common.env
 - ./apps/web.env
 - /opt/secrets.env

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

# common.env: Set Rails/Rack environment
RACK_ENV=development

12、extends

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

# common.yml
webapp:
build: ./webapp
environment:
\ - DEBUG=false
\ - SEND_EMAILS=false

Write a new development.yml file that extends with the webapp service in common.yml.

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 the network mode. Use the same value as the --net parameter of the docker client.

# 容器默认连接的网络,是所有Docker安装时都默认安装的docker0网络.
net: "bridge"
# 容器定制的网络栈.
net: "none"
# 使用另一个容器的网络配置
net: "container:[name or id]"
# 在宿主网络栈上添加一个容器,容器中的网络配置会与宿主的一样
net: "host"

Docker automatically creates three networks for each node:

network name role
bridge The network that the container connects to by default is the docker0 network that is installed by default when all Docker is installed.
none Container-customized networking stack
host Add a container to the host network stack, the network configuration in the container will be the same as that of the host

appendix:

action name command
Create a network docker network create -d bridge mynet
View network list docker network ls

14、pid

The process namespace is shared with the host system. Containers with this option turned on can access and operate each other through the process id.

pid: "host"

15、dns

Configure the 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 Capability of the container.

cap_add:
 - ALL
cap_drop:
 - NET_ADMIN
 - SYS_ADMIN

17、dns_search

Configure DNS search domains. 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
# 这些都是和 docker run 支持的选项类似。
cpu_shares: 73
working_dir: /code
entrypoint: /code/entrypoint.sh
user: postgresql
hostname: foo
domainname: foo.com
mem_limit: 1000000000
privileged: true
restart: always
stdin_open: true
tty: true

docker-compose.yml实例

version: "2"

services:
### console
    console:
        build:
            context: ./images/console
            args:
                # console 容器 www-data用户密码
                - USERPASS=root
                - GIT_NAME=yangnan
                - GIT_EMAIL=20706149@qq.com
                - INSTALL_YARN=false
        volumes_from:
            - php-fpm
            - nginx
            - mysql
            - redis
        volumes:
            - ./ssh:/home/www-data/.ssh
        links:
            - redis
            - mysql
        tty: true

### php-fpm
    php-fpm:
        build: ./images/php-fpm
        volumes:
            - ./app/:/var/www/

### nginx
    nginx:
        image: nginx
        ports:
            - "8081:80"
        volumes_from:
            - php-fpm
        volumes:
            - ./logs/nginx/:/var/log/nginx/
            - ./images/nginx/sites:/etc/nginx/conf.d/
        links:
            - php-fpm

### mysql
    mysql:
        image: mysql
        ports:
            - "7706:3306"
        environment:
            MYSQL_ROOT_PASSWORD: "123"
            MYSQL_DATABASE: "test"
            MYSQL_USER: "root"
            MYSQL_PASSWORD: "123"
        volumes:
            - ./data/mysql:/var/lib/mysql

### redis
    redis:
        image: redis
        ports:
            - "6379:6379"
        volumes:
            - ./data/redis:/data

Precautions:

When using compose to orchestrate and manage Docker containers, you need to write the docker-compose.yml file. When writing it for the first time, it is easy to encounter some relatively low-level problems, resulting in the error of parsing the yml file first when executing docker-compose up. The more common is yml's strict requirements for indentation.
The indentation after the yml file is OK, the tab key character is not allowed, only spaces can be used, and the number of spaces is also required. After actual testing , it is found that it is normal to add a space to each line for indentation.
for example:

web:
<空格>build:
<空格><空格>command:
...

Otherwise, it is easy to cause various yaml.scanner.ScannerError: error prompts.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326367344&siteId=291194637