Introduction to Docker Compose

Sanidhya Singh
Analytics Vidhya

--

Docker is an amazing containerisation solution that can help you ship your code seamlessly, irrespective of the environment it runs on. Docker provides the ability to package and run an application in a loosely isolated environment called a container.

The backbone of delivering a Docker container is docker-compose. As a developer, you can use the docker-compose command to design and run multi-container Docker applications.

Using docker-compose involves 2 steps,

  1. Create a docker-compose.yml file defining the services that will form your app
  2. Run docker-compose up to create and start your app

Before you begin, you must have Docker and Docker-Compose installed on your machine.

docker-compose takes a .yml file and executes the “recipe” stored within it. By default it looks for a docker-compose.yml file in the current directory. A typical docker-compose.yml file looks like,

version: '3'services:
sql-server:
image: mcr.microsoft.com/mssql/server
restart: always
environment:
ACCEPT_EULA: y
SA_PASSWORD: <your_password>
adminer:
image: adminer
restart: always
ports:
- 8080:8080

Here, we have 2 containers running in our app, namely, SQL Server and Adminer.

The .yml file above creates a Docker container running Microsoft SQL Server. The “image” specifies what image Docker needs to download for the container. You can find a complete list of all Docker images available at Docker Hub.

The containers in the app can access each other via the names. For eg. from the above .yml file, to access SQL Server you may use sql-server:1433.

Docker hub images come with certain environment variables that you can configure to your requirement, for eg. in the above .yml file we have configured a service account password. Explore the documentation of the Docker image being used to understand what configuration options it makes available to you.

Main sections of a docker-compose file,

Descriptions for the most commonly used sections in a docker-compose file

Container level options,

Configuration options available for a container in docker-compose

A few commonly used docker-compose commands,

Create & Start containers

docker-compose up

List the containers

docker-compose ps

Delete a stopped container

docker-compose rm

Specify a .yml file

docker-compose -f docker-compose.yml

Full list of options available with docker-compose

Define and run multi-container applications with Docker.

Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help

Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to

--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent

Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information

That concludes this introduction to docker-compose

--

--