You are on page 1of 6

Develop Intelligence – Docker Docker Compose

Agenda: Using Docker Compose


 Overview of Docker Compose
 Docker Compose features.
 Building docker-compose.yml file.
 Docker-compose command.
 Working with multiple images in an application.
 Configuration Files and Environment Variables.

Overview of 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, docker-compose up you create and start
all the services/volumes/networks from your configuration.
Docker Compose file can automate starting containers and passing all the various different parameters that we
like.

Using Compose is basically a three-step process:


1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated
environment.
3. Run docker-compose up and Compose starts and runs your entire app.

A docker-compose.yml looks like this:

version: '3.4'

networks:

frontend:

backend:

volumes:

v1:

v2:

services:

hellowebapp:

build:

context: ./HelloWebApp

dockerfile: Dockerfile
Develop Intelligence – Docker Docker Compose

image: hellowebapp

ports:

- "8081:80"

networks:

- frontend

- backend

volumes:

- v1:/mydemo

counterdemo:

build:

context: ./CounterDemo

dockerfile: Dockerfile

image: counterdemo

The features of Compose that make it effective are:


 Multiple isolated environments on a single host.
The Compose file provides a way to document and configure all of the application’s service dependencies
(databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and
start one or more containers for each dependency with a single command.
Compose provides a convenient way to create and destroy isolated testing environments for your test suite.
By defining the full environment in a Compose file, you can create and destroy these environments in just a
few commands:
$ docker-compose up -d
$ ./run_tests
$ docker-compose down
 Only recreate containers that have changed
Compose caches the configuration used to create a container. When you restart a service that has not
changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to
your environment very quickly.
 Preserve volume data when containers are created
When docker-compose up runs, if it finds any containers from previous runs, it copies the volumes from the
old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.
 Variables and moving a composition between environments
Develop Intelligence – Docker Docker Compose
Compose supports variables in the Compose file. You can use these variables to customize your composition
for different environments, or different users.
You can extend a Compose file using the extends field or by creating multiple Compose files.

Building docker-compose.yml
1. Execute the following commands to build and run the docker container
docker build -t sandeepsoni/hellowebapp .
docker run --rm -p "8080:80" sandeepsoni/hellowebapp

Let us now replace the above docker run command with following docker-compose.yml
2. Add the following docker-compose.yml to the folder
version: '3.4'
services:
hellowebapp:
image: sandeepsoni/mydemoapp
ports:
- "8080:80"
3. Execute the following command
docker-compose up
Note that the above command has now replaced docker run command and did exactly same thing.
Use the below command to execute in DETACHED mode
docker-compose up -d

Use the below command to view the logs


docker-compose logs

4. Execute the following command and note that all containers created by up command are deleted.
docker-compose down
5. Edit the docker-compose.yml as below. This time we are able to provide the build command also in docker-
compose yaml file.
version: '3.4'
services:
hellowebapp:
image: sandeepsoni/mywebserver
build:
context: .
Develop Intelligence – Docker Docker Compose
dockerfile: dockerfile
ports:
- "8080:80"
6. Execute the following command
docker-compose up
Note that the above command has now replaced docker build and run commands

Also note the WARNING: "Image for service hellowebapp was not built because it already existed. To rebuild this
image you must use `docker-compose build` or `docker-compose up --build

docker-compose up:
Builds, (re)creates, starts, and attaches to containers for a service.
If there are existing containers for a service, and the service’s configuration or image was changed after the
container’s creation, it picks up the changes by stopping and recreating the containers (preserving mounted
volumes)
Unless they are already running, this command also starts any linked services.
When the command exits, all containers are stopped.

docker-compose up -d
Starts the containers in the background and leaves them running.

docker-compose up hellowebapp
To run only the "hellowebapp" service from the yml file

docker-compose up --build
Force the creation of docker image.

docker-compose up --force-recreate
Force Compose to stop and remove existing containers and recreate all containers.

docker-compose down
Stops containers and removes containers, networks, volumes, and images created by up.

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up


docker-compose -f docker-compose.yml -f docker-compose.uat.yml up
Compose combines both compose files into a single configuration.
Develop Intelligence – Docker Docker Compose

The -f flag is optional. If you don’t provide this flag on the command line, Compose traverses the working directory
and its parent directories looking for a docker-compose.yml and a docker-compose.override.yml file.

Docker Compose Reference:


https://docs.docker.com/compose/compose-file/

Sample Compose File

version: "3.2"
services:
myapi:
image: sandeepsoni/myapi
build:
context: .\DemoApi
dockerfile: Dockerfile
environment:
ASPNETCORE_ENVIRONMENT: Development
ConnectionString: "Server=mssqlserver;Database=CatalogDb;User Id=sa;Password=Password_123"
container_name: myapicontainer
ports:
- "5100:80"
- "5101:443"
depends_on
- mssqlserver

mssqlserver:
image: <xyz>
ports
- 1433:1433

webapp:
image: sandeepsoni/webmvc
build:
context: .\WebApp
dockerfile: Dockerfile
environment:
Develop Intelligence – Docker Docker Compose
- ASPNETCORE_ENVIRONMENT=Development
- WebAPIUrl=http://myapi
container_name: webappcontainer
ports:
- "8080:80"
- "5201:443"
depends_on:
- myapi

Wordpress lab
• Build a new Docker Compose file
• Use two Docker Hub images. One for Wordpress, and the other a Mysql image. You’ll need to review the
documentation for the images to find out what important options to pass the containers.
• Bring the stack up and try to use the blog.

Docker Compose Syntax: https://docs.docker.com/compose/compose-file/

You might also like