docker-compose

Compose profile

Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

Compose uses three steps:

  • Use Dockerfile to define the environment/image of your application.

  • Use docker-comemage.yml to define the services that make up the application so that they can run together in an isolated environment.

  • Finally, execute the docker-compose up command to get the entire application up and running.

Compose the installation

$sudo curl - L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-composeCopy the code

Apply executable permissions to binaries:

$ sudo chmod +x /usr/local/bin/docker-compose
Copy the code

Create a soft chain:

$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Copy the code

Test whether the installation is successful:

$docker-compose -- Versioncker-compose version 1.24.1, build 4667896BCopy the code

Create a docker – compose. Yml

# yaml configuration

Version: * * * * '3' services: the web: image: hub.cangku.com/aaa/mysql:3.0.0 # building good image ports: * * * * - "5000-5000" redis. Image: * * * * "redis: alpine" mysql server: image: hub.cangku.com/aaa/mysql:3.0.0 container_name: mysql server environment: - MYSQL_DATABASE: "netops_proxy" MYSQL_USER: "netops" MYSQL_PASSWORD: "netops_ps" MYSQL_ROOT_PASSWORD: "root_NetOps.2019" TZ: Asia/Shanghai command: ['--character-set-server=utf8', '--collation-server=utf8_bin'] volumes: - /data/mysql:/var/lib/mysql ports: - 23307:3306Copy the code

Build and run your application using the Compose command

In the test directory, run the following command to start the application:

docker-compose up
Copy the code

If you want to run the service in the background, add the -d parameter:

docker-compose up -d
Copy the code

Yml configuration instruction reference

version

build

images

Image: redisimage: ubuntu: 14.04 image: tutum/influxdbimage: example-registry.com: 4000 / postgresqlimage: a4bc65fd # image idCopy the code

Cap_add, cap_drop

cgroup_parent

command

container_name

depends_on

deploy

devices

entrypoint

environment

expose

logging

The configuration is as follows:

Logging: driver: json-file options: max-size: "200K" # Single file size: 200K max-file: "10" # maximum 10 files: "json-file"driver: "json-file" "syslog"driver: "none"Copy the code

network_mode

network_mode: "bridge"network_mode: "host"network_mode: "none"network_mode: "service:[service name]"network_mode: "container:[container name/id]"
Copy the code

volumes

Mount data volumes or files from the host to a container.

Version: "3.7" Services: DB: image: latest volumes: - "/localhost/postgres.sock:/var/run/postgres/postgres.sock" - "/localhost/data:/var/lib/postgresql/data"Copy the code

Here are some common commands for Docker Compose.

1) docker-compose up

Use to deploy a Compose application.

By default this command reads a file named docker-comemess. yml or docker-comemess. yaml.

Of course, the user can also use -f to specify other file names. Typically, the -d argument is used to start the application in the background.

2) docker-compose stop

Stop all containers associated with the Compose application, but do not delete them.

Stopped applications can be easily restarted with the docker-compose restart command.

3) docker-compose rm

Use to delete the stopped Compose application.

It removes containers and networks, but not volumes and mirrors.

4) docker-compose restart

Restart the Compose application that has been stopped.

If a user changes an application after stopping it, the changed content will not be reflected in the restarted application. In this case, you need to redeploy the application to make the changes take effect.

5) docker-compose ps

Use to list the individual containers in the Compose application.

The output includes the current state, the commands the container runs, and the network port.

6) docker-compose down

Stop and delete the Compose application that is running.

It removes containers and networks, but not volumes and mirrors.