Of course, we have already understood the basic use of Docker, of course, we still have a long way to go to complete Docker. Today, we bring you the scheduling tool, and use Docker Run for complexity Containers need to configure a lot of content, such as port mapping, disk mount, environment variables, etc., all in the command format is troublesome and difficult to save, and if multiple containers need to be associated before, it is also very troublesome, so docker-compose, one of the Docker three swordmakers, comes to solve this problem.

Attach:

A meow blog :w-blog.cn

Official Git address :github.com/moby/moby

1. The docker – compose installation

https://github.com/docker/compose/releases/download/1.21.0/docker-compose- > curl - L ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose > chmod +x /usr/local/bin/docker-composeCopy the code

Or install using PIP

> pip install -U docker-composeCopy the code

Check the version after the installation is complete

> docker-compose -v
docker-compose version 1.21.0, build 5920eb0Copy the code

2. Compose the image using Docker-compose

Here GitLab is taken as an example:

Postgresql: image: registry.cn-hangzhou.aliyuncs.com/acs-sample/postgresql-sameersbn:9.4-24 environment: - DB_USER=gitlab - DB_PASS=password - DB_NAME=gitlabhq_production - DB_EXTENSION=pg_trgm labels: aliyun.probe.url: tcp://container:5432 volumes: - /srv/docker/gitlab/postgresql:/var/lib/postgresql gitlab: image: registry.cn-hangzhou.aliyuncs.com/acs-sample/gitlab-sameersbn:latest links: - redis:redisio - postgresql:postgresql ports: - "10080:80" - "10022:22" environment: - TZ=Asia/Shanghai - SMTP_ENABLED=false - SMTP_DOMAIN=www.example.com - SMTP_HOST=smtp.gmail.com - SMTP_PORT=587 - [email protected] - SMTP_PASS=password - SMTP_STARTTLS=true - SMTP_AUTHENTICATION=login - GITLAB_TIMEZONE=Kolkata - GITLAB_HOST=localhost - GITLAB_PORT=80 - GITLAB_SSH_PORT=22 - [email protected] -  [email protected] - GITLAB_BACKUPS=daily - GITLAB_BACKUP_TIME=01:00 - GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alphanumeric-string - GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string - GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string volumes: - /srv/docker/gitlab/gitlab:/home/git/data labels: aliyun.probe.url: tcp://container:80 aliyun.probe.initial_delay_seconds: "10" aliyun.routing.port_80: gitlab-test redis: image: registry.cn-hangzhou.aliyuncs.com/acs-sample/redis-sameersbn:latest volumes: - /srv/docker/gitlab/redis:/var/lib/redisCopy the code

Deploying Gitlab requires three programs (Gitlab, Redis, postgresQL), if docker is used It is particularly troublesome to build the above image by running, and it is also a problem to modify the image when a little update is needed. Using Docker-compose, you only need to write a command to compose and run it

Start the

Docker-compose up -d docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4679b902b3c registry.cn-hangzhou.aliyuncs.com/acs-sample/gitlab-sameersbn:latest "/sbin/entrypoint...." 6 minutes ago Up 25 seconds 443/ TCP, 0.0.0.0:10022->22/ TCP, 0.0.0.0:10080 - > 80 / TCP root_gitlab_1 b9026534481d Registry.cn-hangzhou.aliyuncs.com/acs-sample/postgresql-sameersbn:9.4-24 "/ sbin/entrypoint. Sh" 6 minutes line Up to 25 seconds 5432/tcp root_postgresql_1 84a222af6c9e registry.cn-hangzhou.aliyuncs.com/acs-sample/redis-sameersbn:latest "/sbin/entrypoint.sh" 6 minutes ago Up 25 seconds 6379/tcp root_redis_1Copy the code

Modify the update

> vim docker-compose.yml ... ports: - "10080:8080" ... > docker-compose up -d root_redis_1 is up-to-date root_postgresql_1 is up-to-date Recreating root_gitlab_1 ... done > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4416b7e4d262 registry.cn-hangzhou.aliyuncs.com/acs-sample/gitlab-sameersbn:latest "/sbin/entrypoint...." 16 seconds ago Up 15 seconds 80/ TCP, 443/ TCP, 0.0.0.0:10022->22/ TCP, 0.0.0.0:10080 - > 8080 / TCP root_gitlab_1 b9026534481d Registry.cn-hangzhou.aliyuncs.com/acs-sample/postgresql-sameersbn:9.4-24 "/ sbin/entrypoint. Sh" 8 minutes line Up About a minute 5432/tcp root_postgresql_1 84a222af6c9e registry.cn-hangzhou.aliyuncs.com/acs-sample/redis-sameersbn:latest "/sbin/entrypoint.sh" 8 minutes ago Up About a minute 6379/tcpCopy the code

Shut down

> docker-compose down
Stopping root_gitlab_1     ... done
Stopping root_postgresql_1 ... done
Stopping root_redis_1      ... done
Removing root_gitlab_1     ... done
Removing root_postgresql_1 ... done
Removing root_redis_1      ... doneCopy the code

Note: I have limited ability to say the wrong place hope we can point out, but also hope to communicate more!