Docker-compose Detailed

Docker-compose


  • Article Directory

1. Docker-compose what is that?

Docker Docker-Compose official project is an open source project, responsible for fast layout of Docker container cluster.
Docker-Compose will be managed by a container divided into three levels, namely the project (project), service (service) and container (container). All files (docker-compose.yml, extends file or environment variable file, etc.) in Docker-Compose run up a project directory, in the absence of special designated project name is the name of the current directory. A project which may comprise a plurality of services, each defined image container runs, parameters dependent. A service which may include multiple container instance, Docker-Compose does not solve the problem of load balancing, and therefore need to use other tools to achieve service discovery and load balancing.
Docker-Compose project configuration files by default docker-compose.yml, can COMPOSE_FILE through the environment variable or -f parameter custom configuration file, which defines several services that depend on each service and run a container.
Dockerfile use a template file, allowing users to easily define a single application container. At work, often encounter case to complete a task requiring multiple containers with each other. For example, to implement a Web project, in addition to Web services container itself, often also need to add the back-end database services container, even including load balancing containers.
Compose application allows the user to define a group of containers associated by a single docker-compose.yml template file (YAML format) for a project (project).
Docker-Compose project written by Python, call the API Docker service provided to manage the container. Therefore, as long as the operating platform to support Docker API, you can manage to orchestrate use Compose thereon.

2. Docker-compose install uninstall

If you use the following command to install fails, see the python version:

$ yum install python-pip

$ pip install docker-compose

Solution:

$ yum install python3-pip

$ pip3 install docker-compose

View installation:

$ docker-compose -version

Uninstall:

$ pip3 uninstall docker-compose

3. Docker-compose commonly used commands

3.1 Docker-compose command format

docker-compose [-f …] [options] [COMMAND] [ARGS…]

-f --file FILE specify Compose a template file, the default is docker-compose.yml

-p --project-name NAME
specify a project name, the current default directory name for the project

-verbose output more debugging information

-v, -version Print version and exit

-log-level LEVEL defined logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

3.2 docker-compose up

docker-compose up [options] [–scale SERVICE=NUM…] [SERVICE…]

Options include:

-d container service running in the background

-no-color is not a color to distinguish different service control output

-no-deps not start the linked container services

-force-recreate forced to re-create the container can not be used in conjunction with -no-recreate

-no-recreate If the container already exists, it is not re-created, can not be used with the -force-recreate

-No-build service is not automatically deleted constructed mirror
-build image build service before starting container
-abort-on-container-exit stop all containers, a container is stopped if any, can not be used with -d

-t, -timeout TIMEOUT container is stopped when the timeout (default 10 seconds)

-remove-orphans delete container services that are not defined in the compose documents

3.3 docker-made ps

docker-compose ps [options] [ SERVICE ...]
lists all the items in a container

3.4 docker-compose stop

docker-compose stop [options] [SERVICE…]

Options include

-t, -timeout TIMEOUT container is stopped when the timeout (default 10 seconds)

docker-compose stop
stop the container is running, you can start again by docker-compose start

3.5 docker-compose -h

docker-compose -h
View Help

3.6 docker-compose down

docker-compose down [options]
Stop and remove the container, the network, volume mirror.

Options include:
-rmi of the type, remove the mirror, the type must be: all, all mirrored delete compose defined in the file; local, remove the mirror image called empty

Data volumes on the container and attached to the anonymous -v, -volumes, delete the already defined in the compose documents

-Remove-orphans, delete container services that are not defined in the compose the
docker-compose down

Remove all containers and disable network-related

3.7 docker-compose down

docker-compose logs [options] [ SERVICE ...]
look at the output of the service container.

By default, docker-compose the output will be different services use different colors to distinguish. Color can be closed by -no-color.

docker-compose logs
to view the service container output

-f trace log output

3.8 docker-compose bulid

docker-compose build [options] [ -build-arg key = val ...] [SERVICE ...]
building (rebuilding) service container project.

Options include:

-compress gzip compression by building up and down environment

-force-rm delete the temporary container building process

No cache build -no-cache mirroring process

-pull always try to get an updated version of the image by pulling operations

-m, -memory MEM container is provided for building memory size

-Build-arg key = val service provided build-time variable
service container once constructed, will bring a tag name. You can always run docker-compose build under the project to rebuild the directory service

3.9 docker-compose pull

docker-compose pull [options] [ SERVICE ...]
mirrored pull service dependent.

Options include:

-ignore-pull-failures, ignoring the mirror during pull error

-parallel, simultaneously pull a plurality of mirrors

-quiet, the process does not pull the mirror printing progress information

docker-compose pull
pull service dependent mirror

4.0 docker-compose restart

docker-compose restart [options] [ SERVICE ...]
to restart the project in service.

Options include:

-t, -timeout TIMEOUT, a front container stop specified timeout restart (default 10 seconds)

docker-compose restart
to restart the project in the service

4.1 docker-compose rm

docker-compose rm [options] [ SERVICE ...]
deletes all (stop state) service vessel.

Options include:

-f, -force, forced to delete, including non-stop state of the container

-v, delete container mounted data volumes

docker-compose rm
delete all service container (stop state). Recommended first-Compose execution Docker
STOP command to stop the vessel.

4.2 docker-compose start

docker-compose start [SERVICE…]

docker-compose start
to start the service container that already exists.

4.3 docker-compose run

docker-compose run [options] [ -v VOLUME ...] [-p PORT ...] [-e KEY = VAL ...] SERVICE [COMMAND] [ARGS ...]
execute a command on the specified service.

docker-compose run ubuntu ping www.baidu.com
execute a ping command in the specified container.

4.4 docker-compose scale

docker-compose scale web = 3 db = 2
the number of containers provided to run the specified service. To set the number of parameters by num service =

4.5 docker-compose pause

docker-compose pause [SERVICE ...]
to suspend a container service

4.6 docker-compose kill

docker-compose kill [options] [ SERVICE ...]
by transmitting a signal to forcibly stop the services SIGKILL container.
Support signal transmitted through -s specified parameters, such as sending a signal by command SIGINT:
Docker-Compose the kill -s SIGINT

4.7 docker-compose config

docker-compose config [options]
verify compose and view file configuration.

Options include:

-resolve-image-digests the label image labeled as a digest

-q, -quiet only verify the configuration, is not output. When properly configured, does not output anything, when the file configuration errors, output an error message

-services print service name, one per line

-volumes print data volume name, one per line

4.8 docker-compose create

docker-compose create [options] [ SERVICE ...]
to create a container for the service.

Options include:
-force-the recreate: to re-create the container, even if the configuration has not changed and mirrors, is not compatible with -no-recreate parameters

-no-recreate: If the container already exists, do not need to re-create, is not compatible with -force-recreate parameters

-no-build: do not create a mirror, even if missing

-build: Before you create a container, generated image

4.9 docker-compose exec

docker-compose exec [options] SERVICE COMMAND [ARGS…]

Options include:

-d separation mode, running in the background command.

-privileged get privileges.

-user USER Specifies the user runs.

-T Disable allocation TTY, default docker-compose exec assignment TTY.

-index = index, when a service has a plurality of containers, this parameter can be found to any service in the service, for example: docker-compose exec -index = 1 web / bin / bash, web service comprising a plurality of containers

5.0 docker-compose port

docker-compose port [options] SERVICE PRIVATE_PORT
display common port of a container port mapping.

Options include:

-protocol = proto, designated port protocol, TCP (default) or UDP

-index = index, if there are multiple service containers agree, commands specified index object container (default 1)

5.1 docker-compose push

docker-compose push [options] [ SERVICE ...]
push service according to the Mirror.

Options include:

-ignore-push-failures ignore errors during a push mirror

5.2 docker-compose stop

docker-compose stop [options] [ SERVICE ...]
displays the progress of each container running situation.

5.3 docker-compose uppause

docker-compose unpause [SERVICE ...]
recovery in a suspended state services.

4. Docker-compose the template file

4.1 Docker-compose the template file Profile

Compose application allows the user to define a group of containers associated by a docker-compose.yml template file (YAML format) for a project (project).
Compose a template file is the definition of service, and network volumes YAML file. Compose a template file in the default path is docker-compose.yml in the current directory, you can use .yml or .yaml as the file extension.
Docker-Compose standard template file should contain version, services, networks of three parts, the most critical services and networks is in two parts.

version: '2'
services:
  web:
    image: dockercloud/hello-world
    ports:
      - 8080
    networks:
      - front-tier
      - back-tier
 
  redis:
    image: redis
    links:
      - web
    networks:
      - back-tier
 
  lb:
    image: dockercloud/haproxy
    ports:
      - 80:80
    links:
      - web
    networks:
      - front-tier
      - back-tier
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
 
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

Compose There are three versions Version 1, Version 2, Version 3, Compose distinguish Version 1 and Version 2 (Compose 1.6.0 +, Docker Engine 1.10.0+). Version 2 supports more instructions. Version 1 will be abandoned in the future.

4.2 image

image is a mirror image ID or name specified services. If the mirror does not exist locally, Compose will try to pull the mirror.

services:
    web:
        image: hello-world

4.3 build

In addition to the service based on the specified image can also be based on a Dockerfile, while using up to start the implementation of building tasks, build tag is build, you can specify the path to the folder where Dockerfile. Compose use Dockerfile will automatically build the mirror, and then use the image to start the service container.

build: /path/to/build/dir

It may be a relative path, as long as it can read the context determination Dockerfile.

build: ./dir

Setting context root, and then subject to the specified directory Dockerfile.

build:
  context: ../
  dockerfile: path/of/Dockerfile

build is a directory, if you want to specify Dockerfile file needs to specify the use of dockerfile tag in the child tag build tag.
If you specify the image and build two labels at the same time, the Compose builds mirror and the mirror image name specified value name.

4.4 context

context options may be Dockerfile file path, may also be a link to a git repository url, when the value of providing a relative path, the path is resolved relative to write the file, this directory is sent to the Docker daemon context

build:
  context: ./dir

4.5 dockerfile

Use dockerfile files to build, you must specify the build path

build:
  context: .
  dockerfile: Dockerfile-alternate

4.6 commond

Use command can override the default command is executed after the vessel started.

command: bundle exec thin -p 3000

4.7 container_name

Compose container name format is: <project name> <service name> <number>
can customize the project name, service name, but if you want full control of the vessel name, you can use the tag to specify:

container_name: app

4.8 depends_on

When using Compose, the biggest advantage is to play a small start-up command, but the order of the general project started by the container is required, if the direct start from top to bottom of the container, because the container will inevitably depend on the issue failed to start. Such as starting an application container, the container will use the database and quit because they can not when we do not start the database container. depends_on label for container-dependent solve, the problem has started the

version: '2'
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

Above YAML file defines the container will first start redis and db two services, and finally start the web service.

4.9 PID

pid: “host”

The PID PID mode is set to host mode, shared with the host system processes namespace. Pid will be able to use the container tag access and manipulate the namespace of the host and other containers.

5.0 ports

ports for mapping the label port.

Use HOST: CONTAINER format or just specified container port, port mapping host random chance.

ports:
 - "3000"
 - "8000:8000"
 - "49100:22"
 - "127.0.0.1:8001:8001"

When HOST: CONTAINER port mapped format, if the container is smaller than the port 60 can get incorrect results obtained, as will parse YAML xx: yy This digital format is 60-ary. It is recommended that the use of string format.

5.1 extra_hosts

Add tags host name, will add some records in the / etc / hosts file.

extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"

After you start to see inside the container hosts:

162.242.195.82  somehost
50.31.209.229   otherhost

5.2 volumes

A directory or mount an existing data volume container, can be used directly [HOST: CONTAINER] format, or use [HOST: CONTAINER: ro] format, the latter to the container, the data volume is read-only, can be effectively protection of the host file system.
Compose specified path data volume may be a relative path, use. ... or to specify the relative directory.
The format may be a data volume forms below

volumes:
  // 只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。
  - /var/lib/mysql
  // 使用绝对路径挂载数据卷
  - /opt/data:/var/lib/mysql
  // 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。
  - ./cache:/tmp/cache
  // 使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。
  - ~/configs:/etc/configs/:ro
  // 已经存在的命名的数据卷。
  - datavolume:/var/lib/mysql

If no path host can specify a volume_driver.

volume_driver: mydriver

5.3 volumes_from

Mount it from another service or data volume container:

volumes_from:
   - service_name   
     - container_name

5.4 dns

Custom DNS server. Can be a value, it can be a list.

dns:8.8.8.8
dns:
    - 8.8.8.8   
      - 9.9.9.9

5.5 expose

Exposure to port, but does not map to the host, allowing only access to services that can be connected. Designated internal port can only be a parameter, as follows:

expose:
    - "3000"
    - "8000"

5.6 links

Links to other services in the container. Service name (alias simultaneously), or "Service Name: Alias ​​Service" (e.g. SERVICE: ALIAS), for example:

links:
    - db
    - db:database
    - redis

5.7 net

Set network mode.

net: "bridge"
net: "none"
net: "host"

5. Docker-compose the template file

Write docker-compose.yml 5.1 Docker-compose the template file

version: '2'
services:
  web1:
    image: nginx
    ports:
      - "6061:80"
    container_name: "web1"
    networks:
      - dev
  web2:
    image: nginx
    ports:
      - "6062:80"
    container_name: "web2"
    networks:
      - dev
      - pro
  web3:
    image: nginx
    ports:
      - "6063:80"
    container_name: "web3"
    networks:
      - pro
 
networks:
  dev:
    driver: bridge
  pro:
    driver: bridge

5.2 Start the application

Create a webapp directory and copy the file to the docker-compose.yaml webapp directory, start the application using the docker-compose.

docker-compose up -d

5.3 Service Access

curl http://127.0.0.1:6061
curl http://127.0.0.1:6062
curl http://127.0.0.1:6063
Published 11 original articles · won praise 2 · Views 439

Guess you like

Origin blog.csdn.net/qq_41112063/article/details/105313807