0% found this document useful (0 votes)
24 views4 pages

CloudProg W04IST-SI4527G Lab03

The document discusses running Docker containers with different Linux distributions like Debian, Fedora, and Alpine. It covers creating a container image, running containers from images, and orchestrating multiple containers using Docker Compose. The exercises guide the user through starting simple containers, publishing ports, sharing files between host and container, building images, and using Compose to run multi-container applications.

Uploaded by

266619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

CloudProg W04IST-SI4527G Lab03

The document discusses running Docker containers with different Linux distributions like Debian, Fedora, and Alpine. It covers creating a container image, running containers from images, and orchestrating multiple containers using Docker Compose. The exercises guide the user through starting simple containers, publishing ports, sharing files between host and container, building images, and using Compose to run multi-container applications.

Uploaded by

266619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab03. Containers.

Cloud Programming (W04IST-SI4527G)


Wojciech Thomas
Spring 2024

1 Learning goals
1. Running simple containers.
2. Create container image.
3. Run container from the image.
4. Orchestrate multiple containers.

2 Prerequisites
1. Create new or start your Cloud9 environment.
2. Open the EC2 instance related to your Cloud9 environment. Check if Security group assigned
to this instance allows for incoming TCP traffic on ports 8080 and 8081 from any IPv4 address.
If not – readjust the Security group.
3. In Cloud9 open the second Terminal (you can press Alt-T).

3 Exercises
3.1 Running containers
In the first part, you will start containers with different Linux distributions: Debian, Fedora, Alpine.
For each distribution you install stress-ng application. You will also save one of the modified
containers as an image, to create new containers using this modified image.
1. Check the version and configuration of Docker using command: docker version
2. Start the container using Debian image: docker run -it debian /bin/bash . Pay atten-
tion, how long does container start.
3. Execute the following commands and observe the results:

hostname
cat /etc/os-release
apt update
apt install stress-ng
echo "Hello" > myfile.txt
cat myfile.txt

4. In the second terminal execute command: docker ps . Compare the ContainerID and the
result of the hostname . What is the name of the container?

1
5. In the first terminal (debian) type exit
6. Execute again docker ps . Can you see your container? Execute: docker ps -a . Can
you see your container now?
7. Execute: docker start -ai e80f (replace e80f with your ID). When inside container
execute: cat myfile.txt .

8. Type exit to leave the container.


9. Execute command docker commit e80f myimage to save the container image under the
new name.
10. Execute command docker run -it myimage /bin/bash , then inside the container:
cat myfile.txt . Does file exist? What is the content of the file? What is ID and the name
of the second container? Are they the same as previously?
11. Execute command docker run -it fedora /bin/bash . Execute all commands
from step 3. Were you successful? Try the following commands: yum update and
yum install stress-ng . Exit from the container.

12. Execute command docker run -it alpine /bin/sh . Execute all commands from step 3.
Were you successful? Try apk update then apk add stress-ng . Exit from the container.

13. List all container images stored in your machine: docker image ls . Compare sizes of
images.

3.2 Run the container with options


In this exercise you will start container using Bitnami Apache to start Apache web server (httpd) in
container, publish its ports to your host system and share files between container and your computer.
1. Open Cloud9 in your browser. In the terminal, verify if you are in the /home/ec2-user/environment
folder. Create new folder: mkdir my-site

2. In the my-site folder create index.html file, and put at least your name inside.
3. Start new container with Apache webserver:

docker run --name apache -v /home/ec2-user/environment/my-site:/app -p


↪ 8081:8080 bitnami/apache

Option -v mount folder from your system into containers folder. Option -p publishes
containers private port (8080) to port on your machine (8081). Option --name gives your
container a constant name, instead of random name assigned by Docker.
4. Open your browser and connect to port 8081 using Cloud9 / EC2 instance public address.
5. Modify index.html and refresh the browser. Can you see the changes?
Pay attention that every time you reload the page you can see log information in the terminal.
6. In Terminal window press Ctrl-C to stop the Apache container.
7. Now run the container in the background. When you use -d option, container runs in the
background, so you can still use the terminal.

2
docker run --name apache -d -v /home/ec2-user/environment/my-site:/app
↪ -p 8081:8080 bitnami/apache

If you cannot start a new container, because of the name, remove the old one from the
memory: docker rm apache .

3.3 Create a new container image


1. Clone the repository from the previous lab into environment folder.
2. In the left pane expand folder cloud9-demo/backend .

3. Open file Dockerfile and study its content.


4. In terminal change directory to backend
5. Build the container image:

docker build -t mybackend:v1 -t mybackend:latest .

6. Run the container using your image:

docker run --name backend -p 8080:8080 mybackend

Open the backend web page in your browser.


7. Stop your container using command Ctrl-C . Remove image from memory: docker rm backend
8. Open app.py file and insert your surname into <h1> tag.
9. Build a modified container image using command:

docker build -t mybackend:v2 -t mybackend:latest .

10. Run a container again:

docker run --name backend -p 8080:8080 mybackend

List container images. Which image was used?


11. Stop the last container, and remove it from the memory: docker stop backend && docker rm backend .
12. Start the container using command:

docker run --name backend -p 8080:8080 mybackend:v1

What version is visible in the browser now?

3
13. Repeat the same steps to build container for frontend. Compare the content of both
Dockerfile . Especially pay attention to the first line FROM .

Warning: Do not forget to put IP address of your EC2 instance inside src/index.html file.
14. Verify if frontend displays information from backend.

3.4 Orchestrate containers using docker-compose


1. Remove all working and stopped containers from your system:

docker rm -f $(docker ps -a -q)


docker ps -a

2. In bash terminal change directory to cloud9-demo folder.


3. Install compose plugin in Cloud9:

sudo mkdir -p /usr/local/lib/docker/cli-plugins

sudo curl -sL https://github.com/docker/compose/releases/latest/ ⌋


↪ download/docker-compose-linux-$(uname -m) -o
↪ /usr/local/lib/docker/cli-plugins/docker-compose

sudo chown root:root /usr/local/lib/docker/cli-plugins/docker-compose


sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

Warning: The second command curl contains very long URL address. Copy it into text
editor and join it into one long line. Then paste in into bash terminal.
4. Run command docker compose up -d to start both backend and frontend.
5. List all running containers.
6. Open frontend and backend in the browser.
7. Stop all modules with command docker compose down

You might also like