Skip to content

Containerising with Docker

How to install Docker

Docker can be downloaded from https://www.docker.com/get-started/. Select the installer for your machine and execute it once downloaded.

What is Docker?

Docker is a platform that allows you separate your applications from your infrastructure to allow you to package and ship applications that can run on any machine that also runs Docker. To do this, Docker builds a Dockerfile to package your environments in an image, and runs your application in a container. This relationship is visualised below.

graph LR A[Dockerfile ] -->|build | B[Image ]; B -->|run | C[Container ];

What is a Dockerfile?

A Dockerfile is simply a text file that comprises a set of instructions on how to build a Docker image. Example commands that can be used in a Dockerfile are:
- FROM - Creates a layer a base image, like ubuntu
- WORKDIR - Sets the working directory for any RUN, CMD, ...
- PULL - Adds files from your Docker repository
- COPY - Copy files from the host machine to the container
- RUN - Builds your container
- CMD - Specifies what command to run within the container

To use a Dockerfile to build a Docker image, we can run:

docker build [image]

Example Dockerfile

FROM node:12-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "src/index.js"]

What is an image?

A Docker image is an executable package that contains the instructions required to build a Docker container. Docker images can be used to create more complex Docker images, and in fact most Docker images are built on top of barebones versions of ubuntu, centos, etc.

We can look at what images are available on our local machine by using the following command.

docker images

When we want to start a container based off a Docker image, we use.

docker run [image]

What is a container?

A container is a runnable instance of an image. Containers are relatively well isolated from both other containers and the host machine by default. However, they can be connected to one or more networks, or have storage attached to them to give them persisting memory.

To list containers running on a machine, we can run:

docker ps

Getting started with Docker

After opening open the Docker daemon, which is done by opening the application that you downloaded from https://www.docker.com, we can start containers with Docker in the terminal. To run a container from Docker run:

docker run [options] {container} {command}

Docker contains a pre-made container which contains a webpage tutorial to help you start to get familiar with Docker. To start this tutorial, open up your terminal and run:

docker run -d -p 80:80 docker/getting-started
This command will run the docker image called docker/getting-started, since this image is not on the local machine, it is downloaded from Docker Hub. The flag -d detaches the command from the terminal so that the prompt is returned to us. Finally, -p 80:80 maps the container's ports to the host's. To view the webpage that it is now available, open a browser, and navigate to 'localhost:80'. From here you can follow along with the tutorial.

Next step

Once you are happy with the content of this section, you can move onto the next section – Sanic.