Docker Compose
Docker Compose
[email protected]
+91-9980923226
Let's talk about real life applications first!
• One application consists of multiple containers.
• One container is dependent on another.
• Mutual dependency/ startup order.
• Process involves building images and then deploy them
• Long docker run commands
• Complexity is proportional to the number of containers involved.
Docker Compose
• Tool for defining and running multi-container Docker application.
• It is a YML file.
• Compose contains information about how to build the containers and deploy containers.
• Integrated with Docker Swarm.
• Competes with Kubernetes.
Note: Generally the containers in an application built using Docker Compose will all run on the
same host. Managing containers running on different hosts usually requires an additional tool,
such as Docker Swarm or Kubernetes.
Installation
You can run Compose on macOS, Windows, and 64-bit Linux.
Prerequisites
• Docker Compose relies on Docker Engine for any meaningful work, so make sure you have
Docker Engine installed either locally or remote, depending on your setup.
• On desktop systems like Docker for Mac and Windows, Docker Compose is included as part
of those desktop installs.
• On Linux systems, first install the Docker for your OS as described on the Get Docker page,
then come back here for instructions on installing Compose on Linux systems.
Basic Usage
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
2. Save the file and run Docker Compose from the same directory:
docker-compose config (To validare yml file)
docker-compose up –d
This will build and run the db and wordpress containers. Just as when running a single container
with docker run, the -d flag starts the containers in detached mode.
3. You now have a WordPress container and MySQL container running on your host. Navigate to
http://localhost:8000/wordpress in a web browser to see your newly installed WordPress
application.
If localhost doesn’t work in windows execute below command get and use ip address displayed
instead of localhost.
You can also use docker ps to further explore the resulting configuration:
docker ps
4. Stop and remove the containers:
docker-compose down
Compose File Syntax
Directive Use
volumes Mounts a linked path on the host machine that can be used
by the container.
Most of this guide will focus on setting up containers using the services section. Here are some
of the common directives used to set up and configure containers:
Directive Use
Sets the image that will be used to build the container. Using
image this directive assumes that the specified image already exists
either on the host or on Docker Hub.
Caution
The example docker-compose.yml above uses the environment directive to store MySQL user
passwords directly in the YAML file to be imported into the container as environment variables.
This is not recommended for sensitive information in production environments. Instead,
sensitive information can be stored in a separate .env file (which is not checked into version
control or made public) and accessed from within docker-compose.yml by using the env_file
directive.
Persistent Data Storage
Storing MySQL,MongoDB or PostgreSQL data directly inside a container is not recommended.
Docker containers are intended to be treated as ephemeral: your application’s containers are
built from scratch when running docker-compose up and destroyed when running docker-
compose down. In addition, any unexpected crash or restart on your system will cause any data
stored in a container to be lost.
For these reasons it is important to set up a persistent volume on the host that the database
containers will use to store their data.
version: '2'
services:
mongodb:
image: mongo
container_name: mongo
volumes:
- data:/data/db
restart: always
volumes:
data:
external: true
external: true tells Docker Compose to use a pre-existing external data volume. If no volume
named data is present, starting the application will cause an error. Create the volume:
docker volume create --name=data
Start the application as before:
docker-compose up -d
Attach bash to the running container
docker-compose pull
It will verify updates for any used image in docker-compose.yml file and download it.
The output will look like:
Pulling mongo (mongo:latest)... latest: Pulling from library/mongo c4bb02b17bb4:
Pull complete 158f54c96e9a:
Pull complete Digest:
sha256:d16539343d6b47ac150a9fae8e1278253e5f00a4c1d9d3f4a3858bd90d5f3097
Status: Downloaded newer image for mongo:latest
It will verify changes in Dockerfile and recreate image in case when Dockerfile was changed.
run docker-compose up -d command.
It will recreate and restart only our application container:
Docker Compose with Spring Boot, MongoDB, NGINX
Access to http://localhost/
If localhost doesn’t work in windows execute below command get and use ip address displayed
instead of localhost.
Ex: http://192.168.99.100
docker-compose commands
Following commands can be used with docker-compose <command> d
Ex: docker-compose up
To get more help about particular command > docker-compose <command> --help
Ex: docker-compose up --help
Using Multiple Docker Compose Files
Use multiple Docker Compose files when you want to change your app for different
environments (e.g., dev, staging, and production) or when you want to run admin tasks against a
Compose application. This gives us one way to share common configurations.
Docker Compose already reads two files by default: docker-compose.yml and docker-
compose.override.yml. The docker-compose-override.yml file can be used to store overrides for
existing services or define new services. Use multiple files (or an override file with a different
name) by passing the -f option to docker-compose up (order matters):
In the following example, the new value overrides the old, and command runs my_new_app.py:
# original service
command: python my_app.py
# new service
command: python my_new_app.py
Different Environments
Start with your base Docker Compose file for your application (docker-compose.yml):
web:
image: "my_dockpy/my_django_app:latest"
links:
- db
- cache
db:
image: "postgres:latest"
cache:
image: "redis:latest"
On our development server, we want to expose some ports, mount our code as a volume, and build our web image (docker-
compose.override.yml):
web:
build: .
volumes:
- ".:/code"
ports:
- "8883:80"
environment:
DEBUG: "true"
db:
command: "-d"
ports:
- "5432:5432"
cache:
ports:
- "6379:6379"
docker-compose up automatically reads the override file and applies it. We also need a production version of our Docker
Compose app, and we want to call that docker-compose.production.yml:
web:
ports:
- "80:80"
environment:
PRODUCTION: "true"
cache:
environment:
TTL: "500"
When you want to deploy your production file, simply run the following:
Administrative Tasks
We want to run an administrative copy of our application so we can perform tasks such as backing up our database. Using
the same docker-compose.yml file above, we create the following docker-compose.admin.yml file:
dbadmin:
build: database_admin/
links:
- db
Then we run this with the following command: