5 minute read

Prerequisites

All information for this tutorial was tested on Ubuntu 20.04 (Focal Fossa). Having this operating system is not a hard requirement but some information may be different on your personal machine. This information should be consistent for most linux distros but might differ on operating systems such as Mac or Windows.

There are no specific skills needed for this tutorial besides basic familiarity with the command line and text editor

Before we get started we need to make sure we install Docker Engine. Docker Engine is the main product of Docker which we will be using.

Once Docker Engine is installed, try running the following command:

me@mycomputer:~$ docker run hello-world

Like every other hello world program, this is used to make sure our installation is set up correctly. You should see your terminal display the following results:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Once you are able to see this message, that means everything is set up correctly and we can go ahead and get started!

Getting Started

With your clean installation of Docker Engine, we are ready to get started.

Docker is a way to package your software so it can run on any hardware. In order to achieve this, there are three main components you must understand.

  • Dockerfile This is a text document that holds all the user commands required in assemble an image. We can think of this as our blueprint
  • Image A Docker image is a template containing a set of instructions for creating a Docker container
  • Container The Docker container is a running instance of an Image

Those three concepts are all we need to know to have a simple understanding of Docker. For this tutorial we will be creating our own dockerfile to generate an image and using docker run to start a container from the image we created. Our Docker container will run a simple python script that creates an array using NumPy and prints it to the console.

The python script will be located under /usr/src/docker_example and will be called app.py. The contents inside app.py are the following:

import numpy as np

arr = np.array( [[1, 2, 3], [3, 2, 1]])
print(arr)

The file location, file name, and file contents were all arbitrarily set by me. For your own applications feel free to change any of these parameters for your own specifications. All the example code can be found on my personal gitub.

Dockerfile

We’ll begin by constructing our Dockerfile. As mentioned before, the Dockerfile serves as the blueprint for creating our image. In your working directory, begin by creating a dockerfile. You can do this in the command line by navigating to your working directory and creating the dockerfile.

me@mycomputer:~$ cd /insert-your-dir
me@mycomputer:~/insert-your-dir$ touch dockerfile

Now open up the dockerfile you just created and let’s get started!

When starting the docker image, first you must start by setting a base image for subsequent instructions. To do this we use the FROM instruction. A dockerfile must always begin with this instruction. Typically we would start by using our operating system as our parent image, FROM ubuntu:20.04, however since our application requires python, we can use the offically supported python image. By doing so, we are not required to download python in our dockerfile.

FROM python:3 #Sets our parent image

We then navigate to our working directory and copy all of the files into our container. The WORKDIR instruction sets our working directory to whichever existing location we want. The COPY instruction will copy any specified files into our container.

WORKDIR /usr/src/docker_example
COPY . .

Now that we have all the files we need inside our container, we want to install any python dependencies our program will use. In this case, we need to install NumPy. We use the RUN instruction in order to execute any command inside our Docker image.

RUN pip install numpy

Finally, we run our python file using the CMD instruction

CMD ["python", "./app.py"]

Building and loading the container

Now that our docker file is complete we can build our docker image. In the same directory that your docker image lives, run the build command:

docker build -t python-docker-image .

Here we use docker build to build the dockerfile. the -t flag allows us to tag our docker image. This is just the name that we will give our docker image everytime we want to use it. Here we are calling in python-docker-image. Finally we specify where our dockerfile lives, since we are in the directory where our dockerfile lives, we use the . to indicate that the dockerfile lives in our current working directory.

We can verify our image has built by using the grep tool after listing our current docker images:

docker image ls | grep python-docker-image

output:

python-docker-image      latest    ad47ffb98ac2   About a minute ago   995MB

Now that we’ve confirmed that our docker image is built and ready to be used, let’s go ahead and run it!

docker run python-docker-image

We simply use docker run and specify our docker image name in order to run it, you should see the following output:

[[1 2 3]
 [3 2 1]]

This is exactly what we expected to run since that’s what our app.py application is expected to print out!

Conclusion

And just like that, you now know the basics of docker. Don’t stop there! Docker has so much more to offer. Our example application only ran a python file for us but you can do so much more. Docker provides interactive modes where you can mount a filesystem and use docker as an emulator based on whatever parent image you provide it. Docker can run in the background and you can give it commands for it to whatever you want. There is so much more to learn about docker, we are barely touching the surface. I hope you learned something new about docker today.

Happy Learning!