Getting Started with Docker

Let's learn how to leverage Docker environments in the Educative platform.

What is Docker?

Docker is a container management service that allows developers to build applications, ship them into containers and run them anywhere. Hence, you can create your own custom environments which can include a variety of Operating Systems and developing frameworks.

Docker containers are pretty lightweight and can be deployed on any physical and virtual machines and even on the cloud.

The Jargon

  • Base Image: An OS userspace.
  • Image: A read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
  • Container: A runnable instance of an image.

Here are two basic tutorials for beginners. They can help you out in getting started on Docker locally:

Docker Basics

We use Docker to allow authors to create their own custom environments to accommodate any language/framework that is not supported by our platform.

Authors can create custom docker images that deploy on the Google Cloud and allow end-users to directly have access to these customized environments through our platform.

In the coming lessons, we will see how setting up a Docker environment is slightly different on our platform as compared to running it locally.

Creating a Dockerfile

The first step in setting up a Docker environment is creating a Dockerfile. This is a text file in which we specify the OS and the applications/frameworks/servers that we want in our environment. The Dockerfile has no file extension.

You can use any text editor to make your Dockerfile. Just save it as Dockerfile (without any extension).

The Dockerfile is used to create a Docker image. A container of your environment will then be instantiated based on the image.

Now let’s make a custom environment that supports the mlxtend library in Python. We will start by making a simple Dockerfile.

#This is a sample image
FROM gcc:latest
MAINTAINER Educative
RUN apt-get update &&\
mkdir myproject &&\
cd myproject
CMD [“echo”,”Image Created”]

FROM

The FROM keyword is used to specify our base image. The base image defines the OS of our Docker environment.

In our Dockerfile, we have used FROM gcc:latest. This is a base image that contains the latest version of GCC in a Linux OS.

Other common base images are:

  • ubuntu:20.04 - A basic Ubuntu OS.
  • node:12 - A Linux environment with Node 12 installed.

Note: On our platform, we support the ubuntu base image. Hence, using it strongly recommended.

MAINTAINER

The MAINTAINER command simply specifies the owner of the image. This is optional.

RUN

RUN is used to specify commands that are run inside the container. These commands are executed right after our container is instantiated. Think of RUN as a means to provide commands in the terminal.

Since our container is running Linux, we can use Linux-based terminal commands.

apt-get update updates all existing packages. Then we create a myproject directory using mkdir and move into that directory. Hence, when a container is instantiated from this Dockerfile, we will automatically be taken to the myproject directory.

We can have multiple RUN commands in our Dockerfile. However, RUN commands increase the size of a Docker image, and as a result, your image will take longer to build. Hence, a good practice is to use a single RUN and chain all your commands using &&.

Note: \ is used to indicate that the command is being continued in the next line.

CMD

CMD is used to display a message to the user. It will be executed when we will build the image. We can use the array ([]) syntax to encapsulate the command that we want CMD to execute. In our case, ["echo", "Image Created"] is equivalent to echo "Image Created".

Building a Docker image

Once we have a Dockerfile with all our instructions, the image can now be built. A Docker image is generally a large file, containing all the data required to instantiate a container.

Locally, you would have to run the docker build command. However, on Educative, this step is performed for you (more on this later).

So by now, we have understood the flow of Docker environments: