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.
What is Docker?
Docker is 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 their 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.
But, is Docker a virtual machine?
Here is one of the most asked questions 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 to run its environments.
Docker VS Virtual machines (Copyright to Docker blog)
Why use Docker as a developer?
This tool can change a developer’s daily life. To best answer this question, I have written a non-exhaustive list of the benefits you will find:
Docker is fast. Unlike a virtual machine, your application can start in a few seconds and stop just as quickly.
Docker is multi-platform. You can launch your container on any system.
Containers can be built and destroyed faster than a virtual machine.
No more difficulties in setting up your working environment. Once your Docker is configured, you will never have to reinstall your dependencies manually again. If you change computers or if an employee joins your company, you only have to give them your configuration.
You keep your work-space clean, as each of your environments will be isolated and you can delete them at any time without impacting the rest.
It will be easier to deploy your project on your server to put it online.
Now let’s create your first application
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 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 the aim of executing your code.
1. Install Docker on your machine
- For Ubuntu:
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
2. Create your project
To create your first Docker application, I invite you to create a folder on your computer. It must contain the following two files:
A ‘main.py’ file (python file that will contain the code to be executed).
A ‘Dockerfile’ file (Docker file that will contain the necessary instructions to create the environment).
Normally you should have this folder architecture:
.
├── Dockerfile
└── main.py
0 directories, 2 files
3. Edit the Python file
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.
4. Edit the Docker file
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 the 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" ]
5. Create the Docker image
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.
6. Run the Docker image
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 see “Docker is magic!” displayed in your terminal.
Code is available
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.
gaelgthomas / docker-first-application-example
Docker First Application example. Created for "A beginner’s guide to Docker — how to create your first Docker application" article on HereWeCode.
Docker first application example
This project was realized as part of my article "A beginner's guide to Docker - how to create your first Docker application". This project is an example of a first application with Docker.
If you want to know more about Docker and learn how to create your first application, you can read my article.
How to run ?
The project launch is in two parts: build and start.
Build
$ docker build -t [image_name] .
Launch
$ docker run [image_name]
Author
- Gaël THOMAS - gaelgthomas
Useful commands for Docker
Before I leave you, I have prepared a list of commands that may be useful to you on Docker.
- List your images.
$ docker image ls
- Delete a specific image.
$ docker image rm [image name]
- Delete all existing images.
$ docker image rm $(docker images -a -q)
- List all existing containers (running and not running).
$ docker ps -a
- Change a container name at running time.
$ docker run --name [container name] [image name]
- Stop a specific container.
$ docker stop [container name]
- Stop all running containers.
$ docker stop $(docker ps -a -q)
- Delete a specific container (only if stopped).
$ docker rm [container name]
- Delete all containers (only if stopped).
$ docker rm $(docker ps -a -q)
- Display logs of a container.
$ 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!
Top comments (10)
Thanks for the tutorial. Reading the command list, you mention images (what we create during the tutorial right?) but also you talk about containers. In this tutorial what would be the container?
Thanks
Indeed, the image is what we create in the tutorial with the following command:
docker build -t python-test .
To make a small reminder,
docker build
allow you to create an image from a Dockerfile. When you pass the '-t' parameter, you can define a name to your image (here the name of your image is 'python-test').Concerning containers, consider each time you run a Docker, you create a container. In this tutorial, when you did the command
docker run python-test
, you created a container configured with the 'python-test' image.If you want to make a test, you can type the following command to display all containers (docker you already launched):
docker ps -a
I hope my answer has enlightened you!
Now it's clear. Thanks for clarifying! :)
I am having a lot of troubles 😓 with the following command on Ubuntu Mate
I'm sorry, I don't have any Ubuntu distribution to try the command.
Can you try with these:
sudo apt-get update
sudo apt install docker.io
I hope it solves your problem!
Whoaa! It worked
Thanks man
➜ ~ docker -v
Docker version 18.09.7, build 2d0083d
Is this the correct way to do it (it's deprecated on Docker docs I guess)
Great, I just corrected the item with the right order for Ubuntu. Thank you for your feedback!
So where exactly is the difference between docker and docker-compose ?
The subject of my next guide will be precisely on Docker-Compose. Here is a small sample that explains the difference between Docker and Docker-Compose:
Docker is used to manage an individual container (service) for your application.
Docker-Compose is used to manage several containers at the same time for the same application. This tool offers the same features as Docker but allows you to have more complex applications.
Docker (individual container) VS Docker-Compose (several containers)
I hope it helps you to understand the difference between Docker and docker-composer better!
Good article. More confident about docker.
Thanks