This article introduces the basics of Docker, explain how create an image Docker for a Streamlit application, and uses Docker Compose to create a WordPress site linked to a MySQL database. It also shows commands for creating, starting, stopping, and deleting containers.

Introduction

The objective of this tutorial is to introduce the basics of Docker. To do this, we will discover step by step the basic commands that allow us to put a Streamlit application into production. Then we will use Docker Compose to create a WordPress site by linking it to a MySQL database.
This tutorial is mainly intended for programmers who want to discover Docker but remains accessible to ordinary people.

Docker is a tool that allows you to launch and share an application easily, quickly and on any standard operating system (Windows, Linux, Mac and servers). To do this, it is based on the principle of image and container.
The objective of a container is the same as for a virtual machine: to host services (applications) on the same physical server while isolating them from each other. However, a container is less rigid than a virtual machine in terms of disk size and allocated resources, but this at the expense of slightly less isolation.

Even if each container is isolated, it is possible to make them communicate with each other using virtual networks. For example, this allows you to link the container containing your application to the container containing your database.
If you want to install Docker, you can do it from the site Docker. If you can't install Docker (you need administrator rights) or if you want to do a quick test, the platform Play with Docker allows you to discover this online tool.

Create and launch an image

To create an image, you must first choose the image on which our application will be based. You can explore the images available on Docker Hub. In Data Science, we regularly use The Python image. This is the one we will use for the rest of this part.
We are going to create a docker image to launch an application Streamlit in Python. Our application will contain the following files:

  • requirements.txt -> This should contain the necessary packages. Here, at least Streamlit.
  • my_app.py -> Our Streamlit application. If you need a sample application, you can deploy the Streamlit cheatsheet.
  • Dockerfile -> Docker configuration file. We are going to create it.

Dockerfile

The Dockerfile is the file that Docker will use to get instructions. In the case of our Streamlit application, you will need to specify the following elements in the Dockerfile:

  • The base image to use. In our case we will be using Python 3.10 alpine
  • The port that the container will listen to when it is running. This is port 8501
  • Workdir which allows you to modify the current directory. The command is equivalent to a “cd” command on the command line. All the commands that follow will all be executed from the defined directory.
  • We copy the requirements.txt present in our application into the container with the “COPY” command
  • Once copied into the container, we execute the command “pip install — r requirements.txt” which allows you to install all the libraries present in the requirements.txt file. To do this we use the “RUN” command
  • Once the libraries are installed, the rest of the application is copied into the container using “COPY”.
  • Finally, we execute the command “streamlit run my_app.py” with “CMD”. This allows us to start our Streamlit application in the container.

This results in the following Dockerfile:

FROM python:3.10—alpine
EXPOSE 8501
WORKDIR /app
COPY requirements.txt. /requirements.txt
RUN
Pip3 install —r requirements.txt
COPY..
CMD streamlit run my_app.py

It is important to understand the difference between RUN and CMD:

  • RUN is an image construction step. The status of the container after a RUN command will be integrated into the container image. A Docker file can have numerous RUN steps that are superimposed on top of each other to build the image.
  • CMD is the command that the container executes by default when you launch the built image. A Dockerfile will only use the last defined CMD.

Create your image with Docker Build

Once our Dockerfile is created, we need to build the image. To do this, simply use the following command:

docker build -t streamlit_app.

The “-t” option allows you to add the streamlit_app tag to our image. Pay close attention to the “.” at the end of the order. It allows you to specify that we want to use the current directory for our application

Start a container with Docker run

Now that our image has been built, we can start it. To do this, we use the following command:

Docker run -dp 8501:8501 streamlit_app

The -d option allows you to launch the container in detached mode (in the background). The -p option allows you to specify the input and output port. We find the value we specified in the Dockerfile.

NB: If you want to directly launch an image that is published on Docker Hub, you do not need to create a Dockerfile and run a Docker build command. For example, if the Go language mascot makes you want to try this language, you can use the “docker run golang” command. This will start a command interface.

List, stop, and delete images

List containers with Docker PS

You can list active containers with the “docker ps” command.

In the example below, we have a container docker/getting-started in progress and the output of this command should return something like this:

CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS names969f65bcecb2 docker/getting-started “nginx -g '...” 8 seconds AGO or 7 seconds80/tcp... stupefied_davinci

To list containers that have been stopped, you can add the -a argument.

NB: The name of a container is defined when it is created with the —name option. If this is not given, a random name is generated.

Stop a container running with Docker stop

To stop a container but without deleting it, you must use the command Docker stop. This takes the ID or the name of the container as an argument. For example, to stop the container from the previous section, you can use one of these two commands:

  • Docker stop 969f65bcecb2
  • Docker stop stupefied_davinci

Delete a container with Docker RM

Once a container has been stopped, it can be removed with the command Docker RM. This one works like Docker stop.

If you want to stop and remove a container with a single command, you can use the -f option. For example:

  • Docker rm -f 969f65bcecb2
  • Docker rm -f stupefied_davinci

Docker Compose

Compose is a tool for defining and launching multiple containers at the same time. The most sorcerous of readers might ask “But tell me Jamy, the containers are supposed to be isolated from each other. So how can they communicate? ”. Well my dear reader, it is thanks to the principle of Networking (or networking).

To put it simply, if Docker has been informed that two containers belong to the same network, then they can communicate. If not, they can't. This allows us to keep our containers completely insulated except when necessary. A more detailed explanation can be found on Docker tutorial.

It is possible to define and launch several containers at the same time with the “classic” Docker commands but this is more laborious and takes more time. The tutorial Getting Started (part 5 to 7) created by Docker covers this topic.

A faster alternative, and one that we recommend, is to use Docker Compose, which is based on a file in the format YAML.

The use of Compose consists of 3 steps:

  1. Creation of the Dockerfile as seen earlier
  2. Creating the docker-compose.yaml file
  3. Start containers with `docker compose up`

Creating a docker-compose.yaml file is easy but it depends on what you want to do.. In general, a docker-compose file should contain the following elements:

  • The docker-compose version. In the example below we use version “3”
  • The list of services (or container list). In our case, we have the “db” service and the “wordpress” service. In each of the services, we will define all the elements that can be useful such as the image, the port, the environment variables for this container, the volumes that it will use, etc. Obviously this varies according to the image we want to make.
  • The volumes that should be used (the database). Here db_data refers to the MySQL database.

After the theory, let's move on to practice with an example that would allow you to deploy a WordPress site using a MySQL database. To achieve this, a possible docker-compose is as follows:

version: '3'

services:

db:

image: mysql:5.7

volumes:

— db_data: /var/lib/mysql

restart: always

environment:

MYSQL_ROOT_PASSWORD: somewordpress

MYSQL_DATABASE: wordpress

MYSQL_USER: wordpress

MYSQL_PASSWORD: WordPress

Wordpress:

depends_on:

— db

image: wordpress:latest

ports:

— “8000:80"

restart: always

environment:

WORDPRESS_DB_HOST: db:3306

WORDPRESS_DB_USER: wordpress

WORDPRESS_DB_PASSWORD: wordpress

WORDPRESS_DB_NAME: wordpress

volumes:

db_data: {}

More details about the lines in this file can be found on the OpenClassrooms training.

It's important to note that docker compose comes pre-installed on Windows, Mac, and Play with Docker but that's not the case on Linux. If necessary, there is a installation manual.

Share your image on Docker Hub

Docker makes it easy to host your images on its hub in order to share them with other people. To do this, you need to:

  1. Register/log in to the site Docker Hub
  2. Press the “Create Repository” button
  3. Give your image a name and leave visibility in “Public”. For the rest of the example, we will give the name “repo_sur_docker_hub”. In its free version, Docker allows you to have a private directory
  4. Press the “Create” button
  5. On the terminal containing the image to be published (here “my_image”), use the following commands:
      • docker tag my_image username/repo_on_docker_hub
      • docker push username/repo_on_docker_hub

If all went well, your image is published to Docker Hub. You can retrieve it using the following command:

docker run -dp 3000:3000 username/repo_on_docker_hub

To go further...

Once Docker is mastered, there are other tools such as Kubernetes or Ansible that can allow you to improve your solutions.

Indeed, if once deployed, your application is very successful, the increase in the number of users can quickly become a problem. Indeed, you are likely to face two situations:

  • When updating your application, you do not want to have service interruptions. Imagine if your inbox was inaccessible during the update.
  • If your application has an average of 1000 visits per hour but a peak of 10,000 visitors at 8 pm, this could be a problem because the architecture could not meet such a demand.

The use of Kubernetes and/or Ansible may be a solution to address these concerns. In addition to offering you flexibility, a good use of these tools will allow you to reduce the cost of your architecture.

Latest blog posts

Discover our articles on the latest trends, advances, or applications of AI today.

Caroline
Data Scientist
Aqsone
Squad Com'
Technical

Introduction to Retrieval Augmented Generation (RAG)

Learn more
Louis
Data Scientist
Aqsone
Squad Com'
Technical

Interpretability of LLMs: The Role of Sparse Autoencoders

Learn more
Diane
Business Developer
Aqsone
Squad Com'
Innovation

Artificial Intelligence in Industrial Procurement

Learn more