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

Docker Lab ENG v3 020424

The document provides instructions for completing a lab on Docker containers. It guides the user through creating a Dockerfile, building an image, and running a container. Key steps include writing a Python script, creating a Dockerfile to package the script, building an image tagged 'showtime:v1', and running a container from the image to continuously run the script.

Uploaded by

temporal034
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)
78 views

Docker Lab ENG v3 020424

The document provides instructions for completing a lab on Docker containers. It guides the user through creating a Dockerfile, building an image, and running a container. Key steps include writing a Python script, creating a Dockerfile to package the script, building an image tagged 'showtime:v1', and running a container from the image to continuously run the script.

Uploaded by

temporal034
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/ 10

Universitat Autònoma de Barcelona

Bachelor's Degree in Artificial Intelligence


Course 2023-2024
Distributed Programming
Containers with Docker LAB

Lab materials

You need an invitation for accessing AWS Academy cloud programming course. Check your inbox for the
instructions.

Objectives

• Create a Dockerfile.

• Create a Docker image by using a Dockerfile.

• Run a container from a Docker image for a simple script and a web application

Environment and delivery


Login into AWS Academy with your @autonoma.cat e-mail address:
https://www.awsacademy.com/vforcesite/LMS_Login

Look for AWS Learner Lab course, then go to Modules to find AWS Academy Learner Lab
Accessing the AWS Management Console

1. At the top of these instructions, choose Start Lab to launch your lab.

A Start Lab panel opens, and it displays the lab status.

2. Wait until you see the message Lab status: ready, then close the Start Lab panel by choosing the X.

3. At the top of the instructions, click on AWS when the ball color turns green

This opens the AWS Management Console in a new browser tab. The system will automatically log
you in.

Tip: If a new browser tab does not open, a banner or icon is usually at the top of your browser with a
message that your browser is preventing the site from opening pop-up windows. Choose the banner or
icon and then choose Allow pop ups.

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 1/10


5. In the search box, look for Cloud9. Then connect to the AWS Cloud9 integrated development environment
(IDE)

From the Services menu, search for and select Cloud9.


Look for New AWS Cloud9 environment
Click on Create environment.
Provide a name to the new environment and select Secure Shell (SSH) in network settings
Press CREATE

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 2/10


6. In the same terminal, run the following command:

wget https://aws-tc-largeobjects.s3.us-west-
2.amazonaws.com/CUR-TF-200-ACCDEV-2/lab-06-containers/code.zip
-P /home/ec2-user/environment

cd /home/ec2-user/environment
The code.zip file is downloaded to the AWS Cloud9 instance. The file is listed in the left navigation pane.

7. Extract the file:

unzip code.zip

8. Run a script that ensures you can get full credit when you choose to submit this lab

Set permissions on the script so that you can run it, then run it:
chmod +x ./resources/setup.sh && ./resources/setup.sh

Verify that the script completed without error.

9.Verify the version of AWS CLI installed.

In the AWS Cloud9 Bash terminal (at the bottom of the IDE), run the following command:
aws --version

The output should indicate that version 2 is installed.

10. Verify that the SDK for Python is installed.

Run the following command:


pip show boto3

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 3/10


Exercise 1: Dockerfile configuration

The objective of the first task is to use AWS Cloud9 environment to create Docker instances during the lab.
For convenience, you will run the container on the same EC2 instance that hosts the AWS Cloud9 IDE that
you are using. You will use this IDE to build the Docker image and launch the Docker container.
1. Create a working directory for the node application and move the source code into the new directory.
In the AWS Cloud9 IDE, to create and navigate to a directory to store your Docker container code, run
the following commands:
mkdir containers
cd containers
2. Upload script showtime.py to /home/ec2-user/environment/containers folder in your IDE
import time
import datetime
print("Hello Docker")
for i in range(100)
current_time = datetime.datetime.now()
print("Current time:", current_time)
time.sleep(5)
3. Let's create our first Dockerfile:
A Dockerfile is where you provide instructions to Docker to build an image. A Docker image is a template
that has instructions to create a container.
To create a new Dockerfile named Dockerfile, run the following commands:
cd ~/environment/containers/
touch Dockerfile
In the left navigation pane, browse to and open the empty Dockerfile that you just created.
Copy and paste the following code into the Dockerfile (it is available in Virtual Campus as a text file)
# Start from a minimal Linux version: Alpine
FROM node:11-alpine

#install python3
RUN apk add --update --no-cache python3

# create a directory where we will install our application


RUN mkdir -p /opt/app

# copy our application into the directory


COPY showtime.py /opt/app

# Provide execution permissions to the script


RUN chmod 744 /opt/app/showtime.py

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 4/10


# Indicate where will the working directory for the application
WORKDIR /opt/app

# Indications of which process will be started


ENTRYPOINT [ "python3","-u","/opt/app/showtime.py"]

Save the changes.

Dockerfile is a simple text file used to define the steps needed to generate a new Linux image to create the
container. It's the "recipe" of the container. It can receive many names, but by default should be called
Dockerfile.
In our case, this Dockerfile generates a container that executes a script that shows the time every 5 seconds in
an infinite loop. Base image is from Alpine Linux and it defines a directory where the application can execute.
In this case, the application is our showtime.py script. We must provide the instructions to copy the application
to this directory and give it permissions for execution. Finally, we provide a working directory for storing
output data and the first operation that the container must do as an entrypoint.
Docker commands used in our example are: RUN, COPY, WORKDIR, ENTRYPOINT

We want to provide the name "showtime:v1" to this container image. We request the creation of the image
of the container with docker build command:
docker build --tag showtime:v1 /home/ec2-user/environment/containers

With this name (tag) we have defined the image name and its version (tag showtime, version v1)
Container building process will generate this output:
Sending build context to Docker daemon 3.584kB
Step 1/7 : ARG CODE_VERSION=latest
Step 2/7 : FROM alpine:${CODE_VERSION}
---> 961769676411
Step 3/7 : RUN mkdir -p /opt/app
---> Running in c5759762cbe7
Removing intermediate container c5759762cbe7
---> d190e0b94987
Step 4/7 : COPY showtime.sh /opt/app
---> 8227714669b7
Step 5/7 : RUN chmod 744 /opt/app/showtime.sh
---> Running in 155aee1c5058
Removing intermediate container 155aee1c5058
---> a5152876bb97
Step 6/7 : WORKDIR /opt/app
---> Running in b389093b6e63
Removing intermediate container b389093b6e63
---> 5e9247fbceaf
Step 7/7 : ENTRYPOINT [ "showtime.sh", "5" ]
---> Running in d3f1f5e623da
Removing intermediate container d3f1f5e623da
---> 0175fdf7a35f
Successfully built 0175fdf7a35f
Successfully tagged showtime:v1

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 5/10


We can verify that the Docker image was created listing the Docker images that your Docker client is aware
of, run the following command:

docker images

Now our objective is to create and run a Docker container based on the previous Docker image. To create and
run a Docker container from the image, run the following command:

docker run --name time -it showtime:v1

Output of your container running will be something like:

This command launches a container with the name time, using the showtime image that you created as the
template. To stop the execution of the container press control+C.

We have stopped the execution of the container, but it still is using resources from our system. If we try to run
it again, the system will complain that it is already existing. We can remove the container from our machine
by using:

docker rm time

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 6/10


Finally, we check that the container is no longer in our system.

docker ps -a

The most common use of docker to run a container is to use some parameters so that the execution is done in
the background and when we stop the container it's removed from the system.

docker run --rm -d --name time showtime:v1

--rm remove container when it stops

-d execute container in background and print container ID

--name identify container by name

The terminal returns the container ID. It is a long string of letters and numbers.

To view the Docker containers that are currently running on the host, run the following command:

docker container ls

The container is running in the background, its output is redirected to a log file, not to the screen as it was
before. If we want to check that it is working well, we must use logs command:

docker logs time

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 7/10


So, it seems that our container is alive and running well. We can check its running status by using ps command:

docker ps -a

We check that our container with showtime:v1image is running normally

When we finish the tasks, we can stop the container with the stop command:

docker stop time

That tries to stop the execution of the container during 10 seconds and removes it from the system as we
requested in the previous docker run command.

Question 1: Which are the layers of this container you have created? Can you make a list of them?

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 8/10


Exercise 2: container web applications

Consider the following Dockerfile:

FROM node:11-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "start"]

Question 2: Which are the layers of this container you have created? Can you make a list of them?

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 9/10


Exercise 3: create your own Container for your applications
Create a new container image that executes the api_example.py script provided in the virtual campus.

from gnewsclient import gnewsclient

client = gnewsclient.NewsClient(language='english',

location='United States (English)',

topic='World',

max_results=3)

news_list = client.get_news()

for item in news_list:

print("Title : ", item['title'])

print("Link : ", item['link'])

print("")

Design a new Dockerfile so you do these steps:


• install python 3
• install python package gnewsclient
• execute Python example application
3. Which are the steps of your Dockerfile?
4. Which are the operations needed in your Dockerfile to copy and execute the python code?
5. Which are the instructions to create, run and monitorize the output of your Docker container?
6. How can you stop the execution of your Docker container?
7. Which are the layers of this container you have created? Can you make a list of them?

IMPORTANT:

Remember to delete AWS Cloud9 environment from the AWS Console.

When you finish your lab, go to Academy tab, choose End Lab at the top of this page, and then select Yes to
confirm that you want to end the lab.

A panel will indicate that DELETE has been initiated... You may close this message box now.

Select the X in the top-right corner to close the panel.

2024, Departamento de Arquitectura y Sistemas Operativos DACSO / UAB 10/10

You might also like