Docker, Docker Compose and Ansible

30 Oct 2020

I have installed Ubuntu 20.04 within Oracle’s VirtualBox.


Install Docker using the documentation.

Update and Install required packages for repositories over HTTPS

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \

Add the Docker repository

curl -fsSL | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] \
   $(lsb_release -cs) \

Install the Docker engine

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli

Verify install

sudo docker run hello-world

Add user to groups

sudo usermod -aG ubridge $(whoami)
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)
sudo usermod -aG wireshark $(whoami)
sudo usermod -aG docker $(whoami)

Install Docker Compose using the documentation.

Docker Compose required Docker to be installed, so make sure you have it installed and working before installing Compose.



sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply an execute permission

sudo chmod +x /usr/local/bin/docker-compose


docker-compose --version

Install Ansible using the documentation.

Update and Install required packages then add the Ansible PPA

sudo apt-get update
sudo apt-get install software-properties-common

sudo apt-add-repository --yes --update ppa:ansible/ansible

Install Ansible

sudo apt-get install ansible -y

Docker Basics

Pulling an image from the docker repository

docker pull debian

We can download images for use in our Docker environment using the Docker Hub, the example above will download the latest Debian image. We can download different versions by using supported tags such as:

docker pull debian:jessie

We can list our images by using the command.

docker images

Creating a Docker container

docker run -it debian

We can use the run command to create a new container, using the switches -i for interactive and -t for TTY (terminal).

There are many different options, use the Docker docs for more.

Entering a container

Using the exec command we can enter a running container

docker exec -it mycontainer /bin/bash

Listing containers

To list all active container processes use the command

docker ps

To list all containers use an -a switch

docker ps -a

Changing the container - Saving

We can save a container as a new image using the command

docker commit mycontainer mynewcontainer:1.0

Stop and Start Docker containers

We can use their container ID or Names

docker stop 4afe090d56a8
docker start wizardly_tereshkova

Removing Images

We can prune images that are not being used by using the command

docker image prune

We can also remove an image by using the command, remember :jessie is the tag

docker image rm debian:jessie

Removing Containers

To remove an Container use the following command:

docker rm <CONTAINER ID / NAME>

You can also query all containers and remove using that, be careful with this!

docker rm `docker ps -q -a`

Using a Dockerfile

A Dockerfile is a set of instructions used when building an image.

# Ubuntu
FROM ubuntu

# Update and Install net-tools, allowing use of the 'ifconfig' command
RUN apt-get update && apt-get install net-tools -y

A quick example of how to a use a Dockerfile

  1. make a directory for your container files. mkdir mycontainer

  2. move into that directory. cd mycontainer

  3. create your Dockerfile. nano Dockerfile

  4. add the above example and save

  5. build your image docker build -t mycontainer:1.0 .

  6. view your new image. docker images

Building an Image

When building an image you need a Dockerfile and Context. A context is a location either a PATH on your local filesystem or a URL to a git repository.

This command will build from the current directory using the default Dockerfile called “Dockerfile”.

docker build .

Some common flags include:

  • using the -f flag we can point to a Dockerfile anywhere in your file system

      docker build -f /home/user/Documents/Dockerfile-production .
  • using the -t flag save image using a repository and tag

      docker build -t user/myapp:1.0 .

Docker Compose

Docker Compose allows you to configure two or more containers that can work together, for example a web server with a SQL database with communication between them while still running in isolation.

A Docker Compose file can also state networks created and data stores or volumes.

By default a network is created between all containers within the docker-compose file.


Create a Docker-compose file

Make a docker-compose.yml file by using your preferred editornano docker-compose.yml.

Add the contents of a docker-compose, an extremely minimal example is:

version: "3.8"
  # creates a container called web
    # build is the location of the Dockerfile
    build: ./web
    # which image to use
    image: nginx
  # creates a container called db
    image: redis

You can use a YAML linter to check your code, examples include; YAML Lint, YAML Validator or a built-in one from your IDE

What Version to use?

In the above sample there is a version number, this relates to your Docker Engine and Compose file format

Compose file version 3 reference

Run docker version on your host machine and match your version number to the compose file format

Client: Docker Engine - Community
 Version:           19.03.13
Compose file format Docker Engine release
3.8 19.03.0+

Validate your YAML

Validate and view the compose file

docker-compose config

Create the containers

While still in the same directory we can use the docker-compose up command to bring both containers online.

docker-compose up

We can see the containers running by using the process command

docker ps
Back to Top