0% found this document useful (0 votes)
8 views

LabWeek4 AssignmentSolution-DockerBasics 1

Uploaded by

Trupti Jogi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

LabWeek4 AssignmentSolution-DockerBasics 1

Uploaded by

Trupti Jogi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SA Lab Week 4: Solution for Graded Assignments -

Docker Basics
Table of Contents
Exercise 1 Solution
Part 1. Create a custom container image with the ubuntu/apache2 + PHP stack in two layers
Part 2. Push the custom image to your DockerHub
Exercise 2
Part 1. Create a Docker network
Part 2: Deploy the database container
Part 3. Deploy the ToDo app frontend
Part 4: Test the app on local browser

Exercise 1 Solution

Part 1. Create a custom container image with the ubuntu/apache2 + PHP stack in two layers
Pull ubuntu/apache2 image
docker pull ubuntu/apache2

docker images

Run the bare container with Apache2 server. Also, expose the appropriate port of the container to be able to access it
locally.
docker run -itd --name app-dev -p 80:80 ubuntu/apache2
Now, visit http://127.0.0.1:80, you should see the Apache default page similar to below.

Enable the Bash terminal for the running container


docker exec -it app-dev bash

Install required packages within the container


apt update
apt install php

Exit the container by typing exit.


Now create a custom image:
docker commit app-dev ubuntu/apache2-php

docker images

Part 2. Push the custom image to your DockerHub


Create an account on DockerHub and a new repository for your custom image.

Login to DockerHub using the terminal.


docker login

Enter your username and password when prompted.


Now rename your custom image to match the name of your repo, or create a new custom image with the repo name.
docker tag ubuntu/apache2-php tsdevopsacp/php-apache

or
docker commit app-dev tsdevopsacp/php-apache

Now, push to DockerHub repository:


docker push tsdevopsacp/php-apache

You should be able to see an image uploaded on your DockerHub now under Tags.
Stop and remove the container:
docker stop app-dev
docker rm app-dev

Exercise 2

Part 1. Create a Docker network


Create a docker network, say mynetwork, to allow the web server and db server containers to connect with each other
docker network create mynetwork

docker network ls

Part 2: Deploy the database container


Use tsdevopsacp/todo-db docker image from Dockerhub to configure the database container with the same network.
Run the database container with name db such that the hostname for this container becomes db:
docker run -d --name db --network mynetwork -e MYSQL_ROOT_PASSWORD=tsdevopsacp -p 3306:3306
tsdevopsacp/todo-db
The container should start running successfully.

Part 3. Deploy the ToDo app frontend


Now as the final step, start the frontend container which will connect to the db server through the configured network.
Ensure that you are publishing the app hosted on port 80 so that it can be accessed through your local browser.
docker run -d --name todoapp --network mynetwork -p 80:80 tsdevopsacp/todo-webapp

You should be able to see the web app container running successfully.
Part 4: Test the app on local browser
Connect to the app on your local browser at localhost or 127.0.0.1 at the port you have published the app. (In this case it
is forwarded it to port 80 which is the default HTTP port.)

Click on the /info.php link to verify if the page loads properly.


Finally try accessing the todo.php page. You should be able to see a To Do list displayed on the page, for which the data is
populated from the database. This verifies that your web application is able to successfully connect to and fetch data
from the database container.

Note: In case you are getting an SQL error on the todo.php page as shown in the screenshot below, check if your db
container is running successfully with the required configurations and environment variables passed to it.

You might also like