You’re a programmer and you want to learn Docker? This article is made for you.

After a brief introduction to what Docker is and why you want to use it, you’ll be able to create your first application using Docker.

What is a Docker?

Docker is free software developed by Docker Company. IT was launched on March 13, 2013, and has been a must-have tool for IT development since that day.

It allows users to create separate and isolated environments to launch and deploy their applications. These environments are called containers.

With Docker, developers can run containers on any machine.

As you can see, with Docker, there are no dependency or compilation issues. All you have to do is start the container, and your application will start immediately.

But is Docker equivalent to a virtual machine?

This is one of the most common questions about Docker. The answer is: not really.

It may look like a virtual machine at first, but it doesn’t function quite the same.

Unlike Docker, virtual machines contain a full operating system. Virtual machines stand alone and act like computers.

Docker simply shares the resources of the host to run the environment it needs.

Docker VS Virtual Machine (Copyright Docker Blog)

Why should I use Docker as a developer?

This tool can really change a developer’s daily life. To best answer this question, I’ve put together a list of its benefits:

  • Docker soon. Unlike a virtual machine, your application can start and stop quickly in seconds.
  • Docker is cross-platform. You can start containers on any system.
  • Containers can be built and destroyed faster than virtual machines
  • Setting up your work environment is no longer difficult. After Docker is configured, you will never have to manually reinstall dependencies again. If you change computers or have new employees join your company, you only need to provide them with your configuration.
  • Have a clean work environment because each container’s environment is isolated and you can delete them at any time without affecting the other environments.
  • You can easily deploy projects to go live on your server.

Now let’s create your first application

Now that you know what a Docker is, it’s time to create your first Docker application!

The purpose of this short tutorial is to create a Python program that will be launched from Dockerfile. Eventually the program outputs a statement.

If you understand all the steps, Docker isn’t complicated.

Note: You do not need to have Python installed on your computer. The Docker environment will include Python to execute your code.

**1. Install Docker on your computer **

For Ubuntu:

First, update your package:

$ sudo apt update 
Copy the code

Next, install docker with apt-get:

$ sudo apt install docker.io 
Copy the code

Finally, verify that Docker is properly installed:

$ sudo docker run hello-world 
Copy the code

For MacOSX users: you can follow this link.

For Windows users: You can follow this link.

2. Create projects

Before creating your first Docker application, you need to create a folder on your computer. It must contain the following two files:

  • A ‘main.py’ file (the Python file that will contain the code to execute).
  • A ‘Dockerfile’ file (a Dockerfile that contains the instructions necessary to create the container environment).

Normally your folder structure looks like this:

.├ ─ Dockerfile ├─ 0 directories, 2 filesCopy the code

3. Edit the Python file

You can add the following code to the ‘main.py’ file:

! /usr/bin/env python3

print(“Docker is magic!” )

Once you see a terminal that says “Docker is Magic!” , your Docker is working.

4. Edit the Docker file

Tip: The first thing you need to do when creating a Dockerfile is ask yourself what you want. Now our goal is to run the start Python code.

To do this, our Docker must contain all the dependencies needed to start Python. A Linux system with Python installed (Ubuntu) is sufficient.

When creating a Docker file, the first step is to visit the DockerHub website. The site contains many images designed to save you time (for example, all images for Linux or some code language).

In our example, we will type “Python” in the search bar. The first result is an official image created to execute Python. Perfect, we use it!

` ` `

The beginning of a dockerfile must always import the base image

# we use the keyword ‘FROM’ to import

# In this example, we import the Python image

So we’ll use Python as the image name, with latest as the version

FROM python:latest

In order to run our Python code, we have to import it into our image

# We can use the keyword ‘COPY’ to do this

# The first argument ‘main.py’ is the name of the Python file on the host

# The second argument ‘/’ is the path to the file on the image

# We put python files in the root directory of the image

COPY main.py /

We need to define the commands that the container needs to execute when it runs

# Use the keyword ‘CMD’ to do this

Python./main.py

CMD [ “python”, “./main.py” ]

` ` `

5. Create a Docker image

Once your code is ready and Dockerfile is written, all you have to do is create your image and include your application.

$ docker build -t python-test . 
Copy the code

The ‘-t’ option allows you to define the name of the mirror. We used ‘python-test’, you can change it to anything you want.

6. Run the Docker image

Once the image is created, your code is ready to run.

$ docker run python-test 
Copy the code

You need to add your image name after ‘docker run’.

You should see “Docker is Magic!” on the terminal. .

The code address

If you want to see the full code, you can click on the following link:

-> GitHub

Common Docker commands

– Mirror list

$ docker image ls
Copy the code

– Deleting a mirror

$docker image rmCopy the code

– Delete all existing mirrors

$ docker image rm $(docker images -a -q)
Copy the code

– List of containers (both running and stopped)

$ docker ps -a
Copy the code

– Stop the container

$ docker stop [container name]
Copy the code

– Stop all running containers

$ docker stop $(docker ps -a -q)
Copy the code

– Delete a container (stopped)

$docker rm [container name]Copy the code

– Delete all containers (stopped)

$ docker rm $(docker ps -a -q)
Copy the code

– Displays logs for a container

$ docker logs [container name]Copy the code