
This article introduces the basics of Docker, explains how to create a Docker image for a Streamlit application, and uses Docker Compose to create a WordPress site linked to a MySQL database. It also covers the commands for creating, starting, stopping, and removing containers.
The goal of this tutorial is to introduce the basics of Docker. We will walk through the fundamental commands needed to deploy a Streamlit application. Then, we will use Docker Compose to set up a WordPress site by linking it to a MySQL database.
This tutorial is primarily aimed at programmers looking to discover Docker, but it remains accessible to anyone.
Docker is a tool that allows you to launch and share applications easily and quickly on any standard operating system (Windows, Linux, Mac, and servers). It achieves this by using the concepts of images and containers.
The purpose of a container is the same as a virtual machine: to host services (applications) on a single physical server while keeping them isolated from one another. However, a container is more flexible than a virtual machine regarding disk size and allocated resources, though this comes at the cost of slightly less isolation.
Even though each container is isolated, it is possible to make them communicate with each other using virtual networks. This allows you, for example, to link the container housing your application to the container housing your database.
If you would like to install Docker, you can do so from the Docker website. If you cannot install Docker (as it requires administrator privileges) or if you want to do a quick test, the Play with Docker platform allows you to explore this tool online.

To create an image, you must first choose the image that will serve as the foundation for your application. You can browse the available images on Docker Hub. In Data Science, we regularly use the Python image. This is the one we will use for the rest of this section.
We are going to create a Docker image to launch a Streamlit application in Python. Our application will contain the following files:
The Dockerfile is the file that Docker will use for its instructions. For our Streamlit application, we will need to specify the following elements in the Dockerfile:
This results in the following Dockerfile:
FROM python:3.10–alpine
EXPOSE 8501
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUNpip3install –r requirements.txt
COPY . .
CMD streamlit run my_app.py
It is important to understand the difference between RUN and CMD:
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 tag streamlit_app to our image. Pay close attention to the “.” at the end of the command. It specifies that we want to use the current directory for our application.
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 runs the container in detached mode (in the background). The -p option allows you to specify the input and output port. This matches the value we specified in the Dockerfile.
Note: If you want to directly run an image that is published on Docker Hub, you don't need to create a Dockerfile or run a docker build command. For example, if the Go language mascot makes you want to try out the language, you can use the command "docker run golang". This will start a command-line interface.

You can list active containers with the "docker ps" command.
In the example below, we have a docker/getting-started container running, and the output of this command should return something like this:
CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS NAMES
969f65bcecb2 docker/getting-started« nginx -g ‘… »8 seconds agoUp 7 seconds80/tcp…stupefied_davinci
To list containers that have been stopped, you can add the -a argument.
Note: A container's name is defined when it is created using the --name option. If one is not provided, a random name is generated.
To stop a container without removing it, you must use the command docker stop. This takes the container ID or name as an argument. For example, to stop the container from the previous section, you can use either of these two commands:
docker stop 969f65bcecb2docker stop stupefied_davinciOnce a container is stopped, you can delete it using the command docker rm. This works just like docker stop.
If you want to stop and delete a container with a single command, you can use the -f option. For example:
docker rm -f 969f65bcecb2docker rm -f stupefied_davinci

Compose is a tool for defining and running multiple containers at once. The most curious of readers might ask, "But wait, aren't containers supposed to be isolated from one another? How can they communicate?" Well, that is thanks to the principle of networking.
Simply put, if we inform Docker that two containers belong to the same network, they can communicate. Otherwise, they cannot. This allows us to keep our containers completely isolated except when necessary. You can find a more detailed explanation in the Docker tutorial.
It is possible to define and launch multiple containers at once using "standard" Docker commands, but it is more laborious and time-consuming. The getting started tutorial (parts 5 to 7) created by Docker covers this topic.
A faster alternative, which we recommend, is to use Docker Compose, which is based on a file inYAML.
format. Using Compose consists of 3 steps:
Creating a docker-compose.yaml file is simple, but it depends on what you want to do. Generally speaking, a docker-compose file must contain the following elements:
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 file 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: {}
You can find more details regarding the lines in this file in the OpenClassrooms course.
It is important to note that Docker Compose comes pre-installed on Windows, Mac, and Play with Docker, but this is not the case for Linux. If needed, there is an installation guide.

Docker makes it easy to host your images on their hub to share them with others. To do this, you must:
If everything went well, your image is now published on Docker Hub. You can retrieve it using the following command:
docker run -dp 3000:3000 USERNAME/repo_sur_docker_hub
Once you have mastered Docker, there are other tools such as Kubernetes or Ansible that can help you improve your solutions.
If your application becomes a huge success after deployment, a surge in the number of users can quickly become an issue. You may find yourself facing two situations:
Using Kubernetes and/or Ansible can be a solution to address these concerns. In addition to providing flexibility, using these tools effectively will allow you to reduce your infrastructure costs.
Lorem ipsum dolor sit amet consectetur. Ipsum penatibus viverra feugiat amet varius pharetra porta risus lectus. Turpis enim massa non tincidunt in faucibus.