Getting Started with Docker

Let’s find out how to use Docker to develop, ship, and run an application.

Reshwin Nishith E
The Patr-onus Deployment Blog
4 min readOct 18, 2021

--

Docker architecture

Before we get right into it, let’s first get to know about the Docker architecture.

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers.

The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

Another Docker client is Docker Compose, which lets you work with applications consisting of a set of containers.

Now let’s create our 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’ll see, it’s not very complicated once you understand the process.

Note: You will not need to install Python on your computer. The Docker environment will contain the Python version required to execute your code.

Step 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

Step 2: Create your project

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:

  • 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

Step 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.

Step 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 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

In our case, we will search for ‘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 to 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 in 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” ]

Step 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.

Step 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 normally see “Docker is magic!” displayed in your terminal.

Some helpful commands for 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
  • 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]

You can refer to this post every time you need a simple and concrete example on how to create your first Docker application. For more on deployments & everything devops, check out the Patr-onus blog.

--

--