Answers to the questions in this statement should be submitted as files named according to the question number in your repository, in the answers
folder.
Each answer will be the subject of a new file in your repository.
Questions requiring an answer in the form of a file will be tagged with the following icon:
⚠️ . These files are corrected automatically by Github Autograde.
Commits are not optional. Commits are not optional Commit as soon as the statement asks you to.
The TP must be submitted individually and will be based on the answers in the files in the answers
folder, as well as on the code in your personal repositories.
- Concepts
- Build
- Ship (Push)
- Run
- Pull
- Docker doc reference
Throughout this tutorial, we'll be basing ourselves on the project located in the app directory of this repository. This application consists of front-end html that calls a REST API (back-end) that returns information.
The application returns various items of information:
- the time of the call
- the environment in which the backend was deployed (environment name)
- the URL called
- the backend hostname
The backend service can be requested on :
- a path starting with /get/{something} (e.g. http://localhost:8080/get/toto)
- or on the /write/{something_to_write} path (eg: http://localhost:8081/write/something_to_write)
A call to the /write path in the backend will write the argument passed as a parameter to a log file located in the back container.
This first part must be done individually.
To carry out the following tutorial, you will need Docker. To do this, I suggest you use the GitPod service, which provides machines with a free tier of 50 hours per month. To set up your account:
- Go to the Gitpod site and log in with your GitHub account (the same as the one you'll be using to do the practical work).
- Then open a new tab and type the following url, modifying it to
https://gitpod.io/#<URL_OF_YOUR_REPO>
. - Your workspace will take a few seconds to be created. When you have access to it, you can run the
docker ps
command to check that everything is working correctly
Warning If you prefer, you can install Docker directly on your computer and do all the tp on it. To do this, follow the official doc However, if you're not sure, you're better off using the Gitpod solution.
Inspect the files in the app directory to understand the structure of the project.
In the app/back directory, create a Dockerfile capable of building a python image containing our back code.
Help:
- Consult the Dockerfile reference documentation
- Use a python image in Docker Hub Docker Hub
- Python uses libraries that must be installed on a host before the program can be started. To install these libraries, run the following command (in the Dockerfile)
pip install -r requirements.txt
- Once the Python libraries have been installed, the command to run in the Docker image is
python3 -m flask run --host=0.0.0.0
- the WORKDIR command may help
Launch a container from the Docker image you have just created.
⚠️ ANSWER: Create a file called1.3
containing the commands you used to launch the container.
Try to access the application at the URL indicated in the logs (:warning:) Why do you think this address is not responding?
Restart the container and open the ports required to access the service.
⚠️ ANSWER: Create a file called1.4
containing the commands you used to start the container with the ports open.
Try to access the application at the URL indicated by the logs. Go back to your terminal and you'll be presented with a nice error stacktrace. This error occurs because the program expects to find the environment variable CURRENT_ENVIRONMENT present in the container.
Restart the container and add the missing environment variable so that the service responds correctly (you can set the value you prefer).
⚠️ ANSWER: Create a file called1.5
containing the command you used to start the container with the environment variable and the open ports.
The application starts up correctly and you can access it by following the URL displayed in the logs.
In order to share your fantastic new creation with the world, we're going to push the image to the official Docker registry called Docker Hub (or Docker Store)
To do this:
- Create an account on Docker Hub
- Create a new repository attached to your account
- Then push your newly created image to your repository.
Your image cannot be pushed as it is (:warning:) Why do you think this is? Change the name of your image and push it back onto your Docker Hub repository.
⚠️ ANSWER: Create a file called1.6
containing the commands you used to push your image to Docker Hub.
Now that your image is shared with the world, you can pull it from any machine on which Docker is installed.
Delete all your images created since the start of the tp on your machine.
⚠️ ANSWER: Create a file called1.7
containing the commands you used to delete all the images created since the start of the tp.
Now relaunch a container based on the image you've just pushed into your Docker Hub repository. (:warning:) What happens before the image starts?
Your container works as expected but if you close your terminal, it stops. Restart the container in detached mode and check that the container is accessible via its web interface.
⚠️ ANSWER: Create a file called1.8
containing the commands you used to launch the container in detached mode.
Your container seems to have started. In detached mode, which command will allow you to check that the container has started? (:warning:) What is the name of your container?
⚠️ ANSWER: Create a file called1.9.1
containing the commands you used to check the state of the container.
Restart your container by renaming it to something meaningful.
⚠️ ANSWER: Create a file called1.9.2
containing the commands you used to name your container.
Your container back now started, open an interactive session to observe the files inside the container. After opening an interactive session, run the following command to retrieve information about the OS used in the container:
cat /etc/*release
What OS is being used in the container?
⚠️ ANSWER: Create a file called1.10
containing the commands you used to start an interactive session.
Go to the app/front directory and carry out all the steps in parts 1.2 to 1.10 to create the front image of our application.
warning: WARNING: Do not create an answer file for the front end. The aim of this question is to have a functional front-end container ready to integrate with the back-end of our application.
warning: WARNING: The frontend is a simple html page. You therefore need to start from a different base image.
Help:
- You need to use a different base image (any web server will do).
- Be careful, you must use an entrypoint in the front directory. See https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example
- The environment variables to set are : WS_BACK_URL
- If both services are started, be careful about exposing ports so as not to conflict.
Don't forget to push your image to your docker hub registry.
Launch your back container at the same time as your front container and modify the front environment variables to make it point to the back.
⚠️ ANSWER: Create a file called1.12
containing the commands you used to start a front container pointing to the back container.
Use the /write
path of the backend URL to write to the log file of the back container.
Check the contents of the log file by connecting to the container back in interactive mode.
Stop all the containers and delete all the local images you have created so far.
⚠️ ANSWER: Create a file called1.14
containing the commands you used to stop all containers.
#2: Docker Compose In this part, we're going to use docker-compose to automate the deployment of the containers we launched in part 1 of the tp.
docker-compose is available in Gitpod.
However, if you have installed Docker on your workstation and want to install docker-compose too, go to the official site and follow the official instructions.
Caution: Check that you have stopped all your containers before starting this part.
At the root of the app directory, create a docker-compose.yml file containing the following code:
version: '3'
services:
my-service:
image: my-image
Edit the file to point to your back image and rename the service as you wish. Then start the back service using the docker-compose commands.
Documentation available on the official website
⚠️ ANSWER: Create a file called2.1
containing the commands you used to start the back service via docker-compose.
Modify the docker-compose.yml file to add a mount on the accessible port of the back service.
Restart the container and test the solution.
Modify the docker-compose.yml file to add the environment variable required to use the back service.
Restart the container and test the solution.
Modify the docker-compose.yml file to add the front-end service. Don't forget to fill in the ports and environment variables.
Restart the container and test the solution.
Restart all the services in detached mode.
⚠️ ANSWER: Create a file called2.6.1
containing the commands you used to start all the services in detached mode.
View the logs of the various services at the same time.
⚠️ ANSWER: Create a file called2.6.2
containing the commands you used to view all the logs for the various services at the same time.
The services are now launched correctly via docker-compose. However, each time they are restarted, the data is flushed.
Modify the docker-compose.yml to add a volume mount in order to persist the data on the disk between restarts.
Caution: After completing this step, comment out the lines relating to the volume mount in the docker-compose.yml.
At any time when you have launched your services in detached mode, you can reload a new version by executing the line below again:
docker-compose up -d
Services must be started in detached mode. Try scaling the back webservice by tripling the number of back containers started.
⚠️ ANSWER: Create a file called2.8
containing the commands you have used to scale the webservice back.
Check that the hostnames change in the UI.
And that's it!