According to the official docker page -

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Docker helps programmers to deliver their software in a reliable and consistent manner. Since it has everything required to the application it is designed for, it alleviates the problem of “It works on my PC, don’t know why it doesn’t work on yours!”

Getting started with Docker is as simple as-

  1. Download docker community edition for your system from the official docker download link.
  2. Install docker on your system.
  3. Run Docker on the system.

Now if you open up the terminal and type-

1
docker version

You should see something like this-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        6247962
 Built:             Sun Feb 10 04:12:39 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       6247962
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     false

We have successfully installed docker onto our system. But now, how do we use this wonderful tool for the benefits of a containerised application?

Let’s begin with something really simple. One of the starting points of writing a Dockerised application is writing something called a Dockerfile. The Dockerfile defines how the containerised application is to be built.

We shall be calling our application docker-example. Go to the terminal and create a new project directory and then navigate to it-

1
2
mkdir ~/docker-example
cd docker-example

Suppose we have a program called application.py, a Python3 application that we need to containerise.

1
cat application.py
1
2
3
4
import platform
print(platform.system())
print(platform.release())
print("Hello! This message is from a container!")

The output when the program is run from the computer directly looks like this-

1
2
3
4
python3 application.py
Darwin
18.5.0
Hello! This message is from a container!

Open up your favorite text editor and create a file called Dockerfile. You can read more about Dockerfiles here.

1
touch Dockerfile

Now edit the contents of the file to this-

1
2
3
4
5
6
7
FROM python:3

WORKDIR /usr/src/app

COPY . .

CMD [ "python", "application.py" ]

Once the file is saved, drop back into the terminal and type this command-

1
docker build -t docker-example .

The -t is being used here to tag the image to the name docker_example.

Docker will download the latest python 3 base docker image first, if it does not exist on your computer.

After the build is done, you should see an image like this-

1
2
Successfully built 403f70f033e0
Successfully tagged docker-example:latest

The 403f70f033e0 here is the container id that is built. If we had not specified the tag, it would have only displayed the container id and not the tag. In that scenario, tagging could be done by-

1
docker tag 403f70f033e0 docker-example

This means our containerised application is ready and we can now run it by the following command-

1
docker run docker-example

We should be seeing an output similar to this-

1
2
3
Linux
4.9.125-linuxkit
Hello! This message is from a container!

Note that the output says Linux even though my system is actually Darwin(MacOS). This means our application is running from the docker container and we can start availing all the benefits listed in the first paragraph of this article.

And there we have it! A really simple walkthrough to Docker. If you managed to follow all the steps correctly, you have successfully run your first containerised application.

You can find my repository with all the files required for this walkthrough at https://github.com/shreyashag/docker-example

Docker is really much much more than this though, and I hope to follow up this article with more detailed articles on this subject.