Professional Documents
Culture Documents
Qe 2
Qe 2
Firstly run the Postgresql database container using following command and set the value of
postgresql configuration variable POSTGRES_PASSWORD according to your convenience in
docker run -d --name postgres -e POSTGRES_USER=postgres -e
POSTGRES_PASSWORD=<your_paasword> -e
PGDATA=/var/lib/postgresql/data/pgdata -v
/tmp:/var/lib/postgresql/data -p 5432:5432 -it postgres:14.1-alpine
Now let us clone the modified code to store messages in the postgresql container. You use
the clone as git clone https://github.com/cloud-and-smart-labs/DDPC-2024-ex2.git
lab2app.
In this code, we used SQLAlchemy with Flask framework to interact with Postgresql.
Change the current directory to lab2app and copy the Dockerfile of exercise 1 in this
directory. Make relevant changes in the Dockerfile
Modify the home.html to display your name and Exercise/task number
Rebuild the image with name <flask_task2_lastname>
Now run the container with following options:
o detached mode (-dit)
o Container image: <flask_task2_lastname>
o Name of container: <task2_your_lastname>
o port mapping : host(80), container(5000)
o Set environment variable for linking the postgres database (-e):
DATABASE_URL=postgresql://postgres:<your_paasword>@<postgresCon
tainer_IP>:5432/postgres
o The whole command for example should look like: docker run -dit -p
80:5000 --name flask -e
DATABASE_URL=postgresql://postgres:4y7sV96vA9wv46VR@172.17.67.1
19:5432/postgres flask_task2_srirama
NB! make sure to change the password and the IP address in this command
to match the information of your postgres container.
By now, you should see the application running at http://VM_IP:80 and add few messages.
NB!! Take the screenshot of running containers and output of web page (1 point)
Stop and remove the postgresql container
Again create postgresql container and still you can see the previous messages in the flask
application. The volumes are important to persistent your data in the host even though the
container is deleted or killed.
The compose has commands for managing the whole lifecycle of your application:
Task 2.1
Working with the docker-compose commands for deploying Message-Board application that
stores the data in directory.
version: "3.7"
services:
flask_app:
build: .
ports:
- "80:5000"
volumes:
- .:/lab1app
Now, run the docker-compose docker-compose up, this will build the image and
create the application container in non-detached mode. When you cancel the
docker-compose task it automatically deletes the container.
You can try to run in detached mode using docker-compose up -d and check the
running container at http://VM_IP:80 and add a few messages. You can also check
the contents of data.json in your host machine.
Check the list of docker-compose services docker-compose ps
Stop the services using command docker-compose stop
Try at least two other commands from this list
NB! Take the screenshot of the output of the commands that you tried. (2 points)
Task 2.2
Working with the docker-compose commands for deploying Message-Board flask application
that linked to Postgresql.
This task is to be carried out by yourself with the knowledge of the previous task and reading
docker-compose documentation.
version: "3.7"
services:
flask_app:
postgresql:
volumes:
db:
For flask_app:
o Build path should be the location of Dockerfile
o You need to use tag depends_on to link the flask_app application to
postgresql service (Example)
o Set environment variables, here
DATABASE_URL=postgresql://postgres:<postgresql_password>@postgresql:
5432/postgres (Example)
Make sure to change the PASSWORD to match the PostgreSQL
password
o Set port mapping to map host port 80 to container port 5000
For postgresql:
o Set image as postgres:14.1-alpine
o Set environment variables POSTGRES_USER=postgres and
POSTGRES_PASSWORD=postgres
o We do not need to map ports, because the database container does not
have to be accessible externally through the host machine ports.
o Assign a Volume where the database files will be stored
db:/var/lib/postgresql/data
Define which volumes should be created:
o Define the db volume as a separate entry at the end of the docker-
compose.yml file (outside and after the "services:" entry)
volumes:
db:
name: postgres-db-vol
Create and run the services using docker-compose up (do NOT use -d).
o As a result, you get 2 service containers running, plus 1 data volume, all
from a single command!
NB! Take the screenshot of docker-compose up command output (2 points)
3. Discuss any three different cloud services, which you are using/used. Try to discuss
the following aspects for each of the service. (5 points)
1. Put a pdf file with your answers and screenshots for the questions and name it as your
studentid_QE2
2. The screenshots are meant to show that the work is done and by you. So, have enough
proofs in your answer sheet.
3. Note: Do not blindly copy the commands. Try to understand them. The provided commands
are to guide you with the initial process.
4. Do not copy code from internet. If you copy, please leave a comment with the URL on that
code/answer sheet
5. Plagiarism will not be tolerated and dealt through the formal channels of the university