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-
- Download docker community edition for your system from the official docker download link.
- Install docker on your system.
- Run Docker on the system.
Now if you open up the terminal and type-
|
|
You should see something like this-
|
|
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-
|
|
Suppose we have a program called application.py, a Python3 application that we need to containerise.
|
|
|
|
The output when the program is run from the computer directly looks like this-
|
|
Open up your favorite text editor and create a file called Dockerfile. You can read more about Dockerfiles here.
|
|
Now edit the contents of the file to this-
|
|
Once the file is saved, drop back into the terminal and type this command-
|
|
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-
|
|
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-
|
|
This means our containerised application is ready and we can now run it by the following command-
|
|
We should be seeing an output similar to this-
|
|
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.