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.
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:
Example Dockerfile
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.
When we want to start a container based off a Docker image, we use.
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:
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 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:
This command will run the docker image calleddocker/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.