docker compose Tutorial

  • 2020-12-05 17:30:18
  • OfStack

Docker provides a container orchestration tool, Docker Compose, which allows users to define a set of associated application containers in a template (YAML format) based on parameters such as "--link" in the configuration template

By automatically prioritizing startup priorities, you can simply perform an "ES9en-ES10en up" to create and start multiple containers in the same service once.

Install docker - compose:

curl -L https://github.com/docker/compose/release/download/1.6.0/docker-compose-`uname -s`-`uname -r` > /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-compose

To manage multiple containers with Docker Compose, you first need to write the container to its configuration file. The default configuration file is called ES25en-ES26en.yml, and we can specify the configuration file via the "-ES28en" option.

This is illustrated by installing redmine

Transform the docker run container creation directive to the Docker Compose configuration file

The commands to create and start the postgresql container are:


[root@localhost ~]# docker run --name postgresql-redmine -d \
> --env 'DB_NAME=redmine_production' \
> --env 'DB_USER=redmine' \
> --env 'DB_PASS=password' \
> sameersbn/postgresql:9.4-12 

It uses the sameersbn/postgresql:9.4-12 image to create a container named postgresql-ES50en and configure three environment variables. The conversion to Docker Compose configuration file reads as follows:


postgresql:
 image: sameersbn/postgresql:9.4-12
 environment:
 - DB_NAME=readmine_production
 - DB_USER=redmine
 - DB_PASS=password 

The commands to create and start the redmine container are:

docker run --name redmine -d --link postgresql-redmine:postgresql --publish 10083:80 --env 'REDMINE_PORT=10083' sameersbn/redmine:3.2.0-4 

It uses sameersbn/redmine:3.2.0-4 image to create a container named redmine, which is converted to Docker Compose configuration file with the following contents:


redmine:
 image: sameersbn/redmine:3.2.0-4
 links:
 - postgresql:postgresql
 ports:
 - "10083:80"
 environment:
 - REDMINE_PORT=10083 

Create configuration file ~/redmine/ docker-ES74en. yml and merge as follows:


postgresql:
 image: sameersbn/postgresql:9.4-12
 environment:
 - DB_NAME=readmine_production
 - DB_USER=redmine
 - DB_PASS=password
  
redmine:
 image: sameersbn/redmine:3.2.0-4
 links:
 - postgresql:postgresql
 ports:
 - "10083:80"
 environment:
 - REDMINE_PORT=10083 

Perform the creation and startup of new container groups:


docker-compose up -d 

Finally, the website can be accessed via http://ip10083.

Then it becomes very easy to start and stop:

Stop command:


docker-compose stop 

Start command:


docker-compose start

Related articles: