Docker Lab ENG v3 020424
Docker Lab ENG v3 020424
Lab materials
You need an invitation for accessing AWS Academy cloud programming course. Check your inbox for the
instructions.
Objectives
• Create a Dockerfile.
• Run a container from a Docker image for a simple script and a web application
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.
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.
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.
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
In the AWS Cloud9 Bash terminal (at the bottom of the IDE), run the following command:
aws --version
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
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
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:
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
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.
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 ps -a
When we finish the tasks, we can stop the container with the stop command:
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?
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?
client = gnewsclient.NewsClient(language='english',
topic='World',
max_results=3)
news_list = client.get_news()
print("")
IMPORTANT:
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.