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