Skip to content

skouadio225/Mini-projet-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

student-list

This repo is a simple application to list student with a webserver (PHP) and API (Flask)

project


Author

First name : Konan Junior Aime Stephane

Surname : KOUADIO

For Eazytraining 17th Bootcamp

Period : January-February-March

LinkedIn: https://www.linkedin.com/in/kouadio-konan-junior-aime-stephane/

email : [email protected]

Goals

The goal of this project is to create et deploy some microservices with the source code given to containerize a student list application of 2 modules :

  • Module 1: It's a REST API written in Flask (with basic authentication needed) who send the desire list of the student based on JSON file
  • Module 2 : Its's a web app written in HTML + PHP who enable end-user to get a list of students

The process

My work was to :

  1. Write the Dockerfile of api
  2. Write the docker-compose
  3. build one container for each module
  4. make them interact with each other
  5. provide a private registry

Resolution of the problems

0) The files' role

In my delivery you can find three main files : Dockerfile and docker-compose.yml

  • docker-compose.yml: to launch the application (API and web app)
  • simple_api/student_age.py: contains the source code of the API in python
  • simple_api/Dockerfile: to build the API image with the source code in it
  • simple_api/student_age.json: contains student name with age on JSON format
  • index.php: PHP  page where end-user will be connected to interact with the service to list students with their age.

2) Build and test

  1. Clone the code:
git clone https://github.com/diranetafen/student-list.git
  1. Change directory and edit of the Dockerfile for api with the instructions informations in ./simple_api/
cd ./student-list/simple_api
  1. Build the api container image :
docker build . -t api.student_list.img
docker images

1-docker images

  1. Create a bridge-type network for the two containers to be able to contact each other by their names thanks to dns functions :
docker network create student_list.network --driver=bridge
docker network ls

2-docker network ls

  1. Move back to the root dir of the project and run the backend api container with those arguments :
cd ..
docker run --rm -d --name=api.student_list --network=student_list.network -v ./simple_api/:/data/ api.student_list.img
docker ps

3-docker ps

As you can see, the api backend container is listening to the 5000 port. This internal port can be reached by another container from the same network so I chose not to expose it.

I also had to mount the ./simple_api/ local directory in the /data/ internal container directory so the api can use the student_age.json list

4-./simple_api/:/data/

  1. Update the index.php file :

You need to update the following line before running the website container to make api_ip_or_name and port fit your deployment $url = 'http://<api_ip_or_name:port>/pozos/api/v1.0/get_student_ages';

Thanks to our bridge-type network's dns functions, we can easyly use the api container name with the port we saw just before to adapt our website

sed -i s\<api_ip_or_name:port>\api.student_list:5000\g ./website/index.php

5-api.student_list:5000

  1. Run the frontend webapp container :

Username and password are provided in the source code .simple_api/student_age.py

6-id/passwd

docker run --rm -d --name=webapp.student_list -p 8081:80 --network=student_list.network -v ./website/:/var/www/html -e USERNAME=toto -e PASSWORD=python php:apache
docker ps

7-docker ps

  1. Test the api through the frontend :

7a) Using command line :

The next command will ask the frontend container to request the backend api and show you the output back. The goal is to test both if the api works and if frontend can get the student list from it.

docker exec webapp.student_list curl -u toto:python -X GET http://api.student_list:5000/pozos/api/v1.0/get_student_ages

8-docker exec

7b) Using a web browser IP:80 :

  • If you're running the app into a remote server or a virtual machine (e.g provisionned by eazytraining's vagrant file), please find your ip address typing hostname -I

9-hostname -I

  • If you are working on PlayWithDocker, just open the 80 port on the gui
  • If not, type localhost:80

Click the button

10-check webpage

  1. Clean the workspace :

Thanks to the --rm argument we used while starting our containers, they will be removed as they stop. Remove the network previously created.

docker stop api.student_list
docker stop webapp.student_list
docker network rm student_list.network
docker network ls
docker ps

11-clean-up

3) Infrastructure

4) Docker-registry and Deployment

As the tests passed we can now 'composerize' our infrastructure by putting the docker run parameters in infrastructure as code format into a docker-compose.yml file.

  1. Run the application (api + webapp) :

As we've already created the application image, now you just have to run :

docker-compose up -d

Docker-compose permits to chose which container must start first. The api container will be first as I specified that the webapp depends_on: it.

12-depends on

And the application works :

13-check app

  1. Create a registry and its frontend

I used registry:2 image for the registry, and joxit/docker-registry-ui:static for its frontend gui and passed some environment variables :

14-gui registry env var

E.g we'll be able to delete images from the registry via the gui.

docker-compose -f docker-compose.registry.yml up -d

15-check gui reg

  1. Push an image on the registry and test the gui

You have to rename it before (:latest is optional) :

NB: for this exercise, I have left the credentials in the .yml file.

docker login
docker image tag api.student_list.img:latest localhost:5000/pozos/api.student_list.img:latest
docker images
docker image push localhost:5000/pozos/api.student_list.img:latest

16-push image to registry

17-full reg

18-full reg details


This concludes my Docker mini-project run report.

Throughout this project, I had the opportunity to create a custom Docker image, configure networks and volumes, and deploy applications using docker-compose. Overall, this project has been a rewarding experience that has allowed me to strengthen my technical skills and gain a better understanding of microservices principles. I am now better equipped to tackle similar projects in the future and contribute to improving containerization and deployment processes within my team and organization.

octocat

About

Mini projet de mise en pratique des connaissances en Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •