Docker Compose is a command-line tool for defining and configuring multi-container Docker applications. In other words, Docker Compose is used to link multiple containers and deploy an application from a single file. The Docker Compose tool can be used in the development, testing, environment preparation, and CI (Continuous Integration) workflow.
In Serverspace you can create a server with already installed app "Docker".
For example, you need to deploy a WordPress website inside a container, and it requires one web server container (Apache/Nginx) and one database container (MySQL/MariaDB). With Docker Compose, you can easily include multiple containers in the docker-compose file. You can also add any other configuration you need to make your application fully functional.
This tutorial reviews installing Docker Compose on an existing host containing Docker and touches on deploying containers with the docker-compose command.
It is assumed that the host with Docker is already set up and running. Let's move on to the steps of installing the Docker Compose Tool.
The steps of installing Docker Compose
Run this commands on your server
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
yum -y install java/bin/bashAnd now run the commands listed below in the same order:
yum install epel-release -y yum install python-pip -y pip install docker-compose Note
We recommend using the pip version 6.0 or higher package manager for the stable operation of Docker Compose. If the pip version is lower than 6.0, run the following command to update it:
pip install --upgrade pipCheck the version of Docker by submitting the following command:
docker-compose --version Result:
docker-compose version 1.25.4, build 8d51620aDeploying containers with the Docker Compose tool
Create a directory, then create a compose file in it. Name the file "docker-compose.yml" or "docker-compose.yaml". You will define the services for the applications and the container images in the compose file.
Before you start creating the compose file, download images of the WordPress and MySQL containers:
docker pull wordpressdocker pull mysqldocker image lsREPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest d44c65e8e9a3 9 days ago 540MB
mysql latest 9b51d9275906 3 weeks ago 547MBThen create a directory named "siteonwordpress":
mkdir siteonwordpresscd siteonwordpress/Create a docker-compose.yml file with the contents listed:
version: '3.0'
services:
frontserver:
image: wordpress
container_name: wp_cont
ports:
- 8080:80
links:
- databaseserver:mysql
environment:
WORDPRESS_DB_PASSWORD: erf6UiwkzjTH
databaseserver:
image: mysql:latest
container_name: wordpressdb_cont
environment:
MYSQL_ROOT_PASSWORD: erf6UiwkzjTHTwo services named "frontserver" and "databaseserver" are defined in the compose file above. For them, the container images are also specified. Environment variables are defined, and the MySQL root and DB WordPress passwords are also mentioned. Note that you should use spaces for indentation, according to the YAML markup.
Deploy your application, in this case, a WordPress website, using the command:
docker-compose upNote
You should run "docker-compose up" from the directory where the docker-compose file is located.
The command above will deploy two containers named "wp_cont" and "wordpressdb_cont". Try accessing your WordPress website using the following URL:
http://{dockerserver-ip}:8080Follow the instructions on the screen to complete the WordPress installation. This confirms that the WordPress site has been successfully deployed inside the containers by using the docker-compose utility.
Let's look at the parameters of the "docker-compose" command.
Output the containers deployed for the application
To output run the following command:
docker-compose psName Command State Ports
--------------------------------------------------------------------------------
wordpressdb_cont docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
wp_cont docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcpStopping and starting containers and their services
Press Ctrl+C while running "docker-compose up" or run the command below:
docker-compose stopStopping wp_cont ... done
Stopping wordpressdb_cont ... doneRun the command "docker-compose start" to run the containers and their services:
docker-compose startStarting databaseserver ... done
Starting frontserver ... doneBrowse container logs
To browse the all container logs or the logs of a particular container, run the command "docker-compose logs {service-name}":
docker-compose logsdocker-compose logs databaseserverStop and delete containers along with the created network
With the "docker-compose down" command, you can stop and delete containers with just one command:
docker-compose downStopping wp_cont ... done
Stopping wordpressdb_cont ... done
Removing wp_cont ... done
Removing wordpressdb_cont ... done
Removing network compose_defaultAdditional parameters you can find in the help section by running the command "docker-compose -help".