Enoncé TP 0

/images/k8s.jpeg

TP0 : Docker hands on

🇫🇷 Version Française

Pour ce TP nous allons construire une image utilisant alpine, nginx et une page HTML custom comme ça il s’aggissait de notre application.

Le but va être de rendre accessible cette page HTML sur le port 8001 de notre machine.

Construire l’image

Il va nous falloir deux fichiers :

  • Dockerfile
  • index.html

Pour construire l’image on va devoir utiliser plusieurs layers dans notre Dockerfile, le premier sera la base sur laquelle on va construire notre image.

On pourrait construire complétement une image from scratch mais pour ne pas perdre de temps on va utiliser une image sur laquelle est déjà installé nginx depuis une distribution Unix minimalise : alpine

On va donc utiliser l’image officielle nginx:alpine3.18

(bien souvent il n’est pas nécessaire de recréer complétement une image autant utiliser une image existante en regardant qu’elle soit bien souvent maintenue).

A cette image nous allons donner un fichier HTML contenant le bout de code suivant :

1<!DOCTYPE html>
2<html>
3  <head>
4    <title>K8S training!</title>
5  </head>
6  <body>
7    <h1>It works !</h1>
8  </body>
9</html>

Nous copierons ce fichier dans notre image via la commande COPY.

Et nous dirons à notre image qu’elle doit lancer la commande nginx -g daemon off; lors de son lancement via la commande CMD du Dockerfile.

Il ne reste plus qu’à lancer la commande docker build avec les bons paramètres pour générer notre image.

Voir l’image

Pour voir l’image générée nous pourrons utiliser les différentes options de la commande docker image.

Lancer l’image (en faire un container)

Maintenant que notre image est correctement généré, nous allons pouvoir la lancer et la tester dans notre navigateur.

Pour cela il faut utiliser la commande docker run avec les options et les paramètres qui vont bien pour exposer le port 8001 de la machine avec le port 80 du container.

clean up

Vous pouvez supprimer les images créées dans ce TP (attention supprimez uniquement les containers et les images que vous avez créées les autres images et container seront utilisés dans la suite des TPS et ne doivent pas être supprimés).

🇬🇧 English version

For this TP we are going to build an image using alpine, nginx and a custom HTML page.

The goal will be to make this HTML page accessible on port 8001 of our machine.

Build the image

We will need two files:

  • Dockerfile
  • index.html

To build the image we will have to use several layers in our Dockerfile, the first will be the base on which we will build our image.

We could completely build an image from scratch but to avoid wasting time we will use an image on which nginx is already installed from a minimalized Unix distribution: alpine

We will therefore use the official image nginx:alpine3.18

(very often it is not necessary to completely recreate an image as much as use an existing image while ensuring that it is often maintained).

To this image we will give an HTML file containing the following piece of code:

1<!DOCTYPE html>
2<html>
3  <head>
4    <title>K8S training!</title>
5  </head>
6  <body>
7    <h1>It works!</h1>
8  </body>
9</html>

We will copy this file into our image via the COPY command.

And we will tell our image that it must launch the nginx -g daemon off; command when it is launched via the CMD keyword from the Dockerfile.

All that remains is to launch the docker build command with the correct parameters to generate our image.

See the image

To see the generated image we can use the different options of the docker image command.

Launch the image (make it a container)

Now that our image is correctly generated, we will be able to launch and test it in our browser.

To do this, you must use the docker run command with the options and parameters that go well to expose port 8001 of the host machine with port 80 of the container.

clean up

You can remove the images created in this TP (be careful to remove only container and images that you created; other container and images will be used in the next TP and must be here)

Latest Posts