You are a developer and you want to start with Docker? This article is made for you. After a short introduction on what Docker is and why to use it, you will be able to create your first application with Docker.
Docker is a free software developed by Docker Inc. It was presented to the general public on March 13, 2013 and has become since that day a must in the world of IT development.
It allows users to create independent and isolated environments to launch and deploy its applications. These environments are then called containers.
This will let the developer run a container on any machine.
As you can see, with Docker, there are no more dependency or compilation problems. All you have to do is launch your container and your application will launch immediately.
Here is one of the most asked question about Docker. The answer is: actually, not quite.
It may look like a virtual machine at first but the functionality is not the same.
Unlike Docker, a virtual machine will include a complete operating system. It will work independently and act like a computer.
Docker will only share the resources of the host machine in order to run its environments.
This tool can really change a developer’s daily life. In order to best answer this question, I have written a non-exhaustive list of the benefits you will find:
Now that you know what Docker is, it’s time to create your first application!
The purpose of this short tutorial is to create a Python program that displays a sentence. This program will have to be launched through a Dockerfile.
You will see, it’s not very complicated once you understand the process.
Note: You will not need to install Python on your computer. It will be up to the Docker environment to contain Python in order to execute your code.
First, update your packages:
$ sudo apt update
Next, install docker with apt-get:
$ sudo apt install docker.io
Finally, verify that Docker is installed correctly:
$ sudo docker run hello-world
In order to create your first Docker application, I invite you to create a folder on your computer. It must contain the following two files:
Normally you should have this folder architecture:
. ├── Dockerfile └── main.py 0 directories, 2 files
You can add the following code to the ‘main.py’ file:
#!/usr/bin/env python3 print("Docker is magic!")
Nothing exceptional, but once you see “Docker is magic!” displayed in your terminal you will know that your Docker is working.
Some theory: the first thing to do when you want to create your Dockerfile is to ask yourself what you want to do. Our goal here is to launch Python code.
To do this, our Docker must contain all the dependencies necessary to launch Python. A linux (Ubuntu) with Python installed on it should be enough.
The first step to take when you create a Docker file is to access the DockerHub website. This site contains many pre-designed images to save your time (for example: all images for linux or code languages).
In our case, we will type ‘Python’ in the search bar. The first result is the official image created to execute Python. Perfect, we’ll use it!
# A dockerfile must always start by importing the base image. # We use the keyword 'FROM' to do that. # In our example, we want import the python image. # So we write 'python' for the image name and 'latest' for the version. FROM python:latest # In order to launch our python code, we must import it into our image. # We use the keyword 'COPY' to do that. # The first parameter 'main.py' is the name of the file on the host. # The second parameter '/' is the path where to put the file on the image. # Here we put the file at the image root folder. COPY main.py / # We need to define the command to launch when we are going to run the image. # We use the keyword 'CMD' to do that. # The following command will execute "python ./main.py". CMD [ "python", "./main.py" ]
Once your code is ready and the Dockerfile is written, all you have to do is create your image to contain your application.
$ docker build -t python-test .
The ’-t’ option allows you to define the name of your image. In our case we have chosen ’python-test’ but you can put what you want.
Once the image is created, your code is ready to be launched.
$ docker run python-test
You need to put the name of your image after ‘docker run’.
There you go, that’s it. You should normally see “Docker is magic!” displayed in your terminal.
If you want to retrieve the complete code to discover it easily or to execute it, I have put it at your disposal on my GitHub.
Before I leave you, I have prepared a list of commands that may be useful to you on Docker.
$ docker image ls
$ docker image rm [image name]
$ docker image rm $(docker images -a -q)
$ docker ps -a
$ docker stop [container name]
$ docker stop $(docker ps -a -q)
$ docker rm [container name]
$ docker rm $(docker ps -a -q)
$ docker logs [container name]
➡️ If you want more content like this, you can follow me on Twitter, where I tweet about web development, self-improvement, and my journey as a fullstack developer!
Subscribe to the newsletter.
Receive the latest articles, tips, tutorials, and exclusive content (1.700+ learners are already part of the community).