Data Engineering
Paul

Deploying your applications with Docker

Partager l’article :

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.

Introduction

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.

Create and launch an image

‍

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:

  • requirements.txt -> This must contain the necessary packages. Here, at a minimum, Streamlit.
  • my_app.py -> Our Streamlit application. If you need an example 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 for its instructions. For our Streamlit application, we will need to specify the following elements in the Dockerfile:

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

This results in the following Dockerfile:

FROM python:3.10–alpine
EXPOSE 8501
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN
pip3install –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 build step. The state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of each other to build the image.
  • CMD is the command the container executes by default when you launch the built image. A Dockerfile will only use the last CMD defined.

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

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

Listing, stopping, and removing images

‍

List containers with docker ps

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.

Stopping a running container with docker stop

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 969f65bcecb2
  • docker stop stupefied_davinci

Delete a container with docker rm

Once 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 969f65bcecb2
  • docker rm -f stupefied_davinci

Docker Compose

‍

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:

  1. Create the Dockerfile as seen earlier
  2. Create the docker-compose.yaml file
  3. Launch the containers with `docker compose up`

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:

  • The docker-compose version. In the example below, we use version "3"
  • The list of services (or list of containers). In our case, we have the "db" service and the "wordpress" service. Within each service, we define all the elements that might be useful, such as the image, the port, the environment variables for that container, the volumes it will use, etc. Obviously, this varies depending on the image you want to use.
  • The volumes to 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 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.

Sharing your image on Docker Hub

Docker makes it easy to host your images on their hub to share them with others. To do this, you must:

  1. Sign up / log in to the Docker Hub website
  2. Click the "Create Repository" button
  3. Give your image a name and leave the visibility set to "Public." For the rest of this example, we will use the name "repo_sur_docker_hub." In its free version, Docker allows you to have one private repository
  4. Click the "Create" button
  5. In the terminal containing the image to be published (here "mon_image"), use the following commands:
      • docker tag mon_image USERNAME/repo_sur_docker_hub
      • docker push USERNAME/repo_sur_docker_hub

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

Going further...

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:

  • When updating your application, you don't want any service interruptions. Imagine if your email inbox were inaccessible during an update.
  • If your application averages 1,000 visits per hour but hits a peak of 10,000 visitors at 8:00 PM, this could be a problem because the architecture wouldn't be able to handle such demand.

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.

Un accompagnement de A à Z par nos experts dans le but d’une intégration de solutions IA sur-mesure.

Lorem ipsum dolor sit amet consectetur. Ipsum penatibus viverra feugiat amet varius pharetra porta risus lectus. Turpis enim massa non tincidunt in faucibus.

Découvrez plus d’articles

No items found.
Bird with red, yellow, and black feathers in flight, wings spread, against a blurred background of greenery.

Can you chat with your company database just like you do with ChatGPT?

Lire l’article
No items found.

TPOT vs. Auto-sklearn: Comparing Two AutoML Libraries

Lire l’article
Deep Learning

Strengthening convolutional neural networks (CNNs) using capsules

Lire l’article