What is Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:
Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere.Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment.Run
docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively rundocker-compose up
using the docker-compose binary
Each of these can be considered a microservice. The more microservices you build into your environment, the more valuable it is to have each of these services in their containers. But as a developer, you should be able to jump from one container to another. This is where you can relate this example to Docker, where Docker Compose can connect different containers as a single service.
Docker Compose is used for running multiple containers as a single service. Each of the containers here run in isolation but can interact with each other when required. Docker Compose files are very easy to write in a scripting language called YAML, which is an XML-based language that stands for Yet Another Markup Language. Another great thing about Docker Compose is that users can activate all the services (containers) using a single command.
Basic Commands in Docker Compose
- Start all services: Docker Compose up
- Stop all services: Docker Compose down
- Install Docker Compose using pip: pip install -U Docker-compose
- Check the version of Docker Compose: Docker-compose-v
- Run Docker Compose file: Docker-compose up -d
- List the entire process: Docker ps
- Scale a service - Docker Compose up -d -scale
Install Compose on Linux systems
- Use YAML files to configure application services - Docker Compose.yml
Run this command to download the current stable release of Docker Compose:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
To install a different version of Compose, substitute
1.29.2
with the version of Compose you want to use. For instructions on how to install Compose2.2.3
on Linux, see Install Compose 2.0.0 on LinuxIf you have problems installing with
curl
, see Alternative Install Options tab above.Apply executable permissions to the binary:
$ sudo chmod +x /usr/local/bin/docker-compose
Note:
If the command
docker-compose
fails after installation, check your path. You can also create a symbolic link to/usr/bin
or any other directory in your path.For example:
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Optionally, install command completion for the
bash
andzsh
shell.Test the installation.
$ docker-compose --version docker-compose version 1.29.2, build 1110ad01
Hands-on
Define the project
Create an empty project directory.
You can name the directory something easy for you to remember. This directory is the context for your application image. The directory should only contain resources to build that image.
This project directory contains a
docker-compose.yml
file which is complete in itself for a good starter wordpress project.Tip: You can use either a
.yml
or.yaml
extension for this file. They both work.Change into your project directory.
For example, if you named your directory mkdir wordpress:
$ cd wordpress/
$ vi docker-compose.yml
Create a
docker-compose.yml
file that starts yourWordPress
blog and a separateMySQL
instance with volume mounts for data persistence:version: "3.9" services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest volumes: - wordpress_data:/var/www/html ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {} wordpress_data: {}
Notes:
The docker volumes
db_data
andwordpress_data
persists updates made by WordPress to the database, as well as the installed themes and plugins. Learn more about docker volumesWordPress Multisite works only on ports
80
and443
.
Build the project
Now, run
docker-compose up -d
from your project directory.This runs
docker-compose up
in detached mode, pulls the needed Docker images, and starts the wordpress and database containers, as shown in the example below.$ docker-compose up -d Creating network "my_wordpress_default" with the default driver Pulling db (mysql:5.7)... 5.7: Pulling from library/mysql efd26ecc9548: Pull complete a3ed95caeb02: Pull complete <...> Digest: sha256:34a0aca88e85f2efa5edff1cea77cf5d3147ad93545dbec99cfe705b03c520de Status: Downloaded newer image for mysql:5.7 Pulling wordpress (wordpress:latest)... latest: Pulling from library/wordpress efd26ecc9548: Already exists a3ed95caeb02: Pull complete 589a9d9a7c64: Pull complete <...> Digest: sha256:ed28506ae44d5def89075fd5c01456610cd6c64006addfe5210b8c675881aff6 Status: Downloaded newer image for wordpress:latest Creating my_wordpress_db_1 Creating my_wordpress_wordpress_1
Note: WordPress Multisite works only on ports
80
and/or443
. If you get an error message about binding0.0.0.0
to port80
or443
(depending on which one you specified), it is likely that the port you configured for WordPress is already in use by another service.Bring up WordPress in a web browser
At this point, WordPress should be running on port
8000
of your Docker Host, and you can complete the “famous five-minute installation” as a WordPress administrator.Note: The WordPress site is not immediately available on port
8000
because the containers are still being initialized and may take a couple of minutes before the first load.If you are using Docker Desktop for Mac or Docker Desktop for Windows, you can use
http://localhost
as the IP address, and openhttp://localhost:8000
in a web browser.Shutdown and cleanup
The command
docker-compose down
removes the containers and default network, but preserves your WordPress database.The command
docker-compose down --volumes
removes the containers, default network, and the WordPress database.
No comments:
Post a Comment