TP Docker

Daniel Hagimont hagimont@enseeiht.fr

The goal of this labwork is to discover Docker and to automate the deployment/undeployment of an architecture composed of several tomcat/servlets behind a HAProxy load balancer.

1. Connection

You must be connected to N7 with the VPN and be connected to a N7 machine with your studenID. Use the following commands to access your server :

Type your enseeiht password.

Where XX=01-40. This will give you acces to a VM with IP address 192.168.27.(XX+10) and the password is "toto"

2. Installation

Docker installation with a simple command

Test your installation

3. Using docker

Download a Ubuntu image from the hub

We will use this image in the following. List the images you have locally, each image has a name, tag and ID

You can remove an image with

you can start a container with

Type « exit » to exit from a container

you can list all your containers (from another terminal) with

and get containerIDs. To restart a container use :

To connect back to a container console do

You can customize your container

Then, save an image of the container (from another terminal) with

You can stop your container (from another terminal) with

You can remove your container (from another terminal) with

For all these commands, you can use names or IDs as well

4. Dockerfile

You can create a Docker image by describing it in a Dockerfile. In a directory "tomcat", create a file Dockerfile.

You have to download the Hello.war which is a little webapp

start-tc.sh is a script for starting tomcat .

Add Hello.war and start-tc.sh in the tomcat directory.

You can then build the container image with the following command (in the tomcat directory)

You can start an instance of this image.

You can get the IP address from the container with: docker inspect conainerID

You can access the webapp with the URL

5. Implementing an architecture

image-20201006170634824

The figure presents the target architecture (final goal).

5.1 First step:

Create two containers

  1. one for Tomcat (as in Section 3)
  2. one for HAProxy (the image is already available on the hub)

HAProxy is configured by a file /usr/local/etc/haproxy/haproxy.cfg

haprox_backup.cfg is a template of that file (given) where you can add lines

server server1 <ip-of-tomcat>:8080 maxconn 32

you can test that it works with one HAProxy and one Tomcat. You can implement a script which creates these two images Start the Haproxy container

Test your configuration with

5.2 Second step:

create a script which deploys an architecture with one HAProxy and 2 Tomcat

You can create a network (bridge) for your instances

You can start your Tomcat instances in that network with

You can add you Tomcat instances in the HAProxy configuration file with

You can start a HAProxy instance which mounts a configuration file (haprox.cfg) which is local to the host system.

6. Docker compose

Reproduce the previous architecture using docker compose

Install docker-compose

Create the docker-compose.yml file (you can start from this content)