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

7 - Creating Your First Docker Image

This document outlines a lab step for creating a Docker image using a Dockerfile on a Linux environment for AWS. It explains the process of setting up a Python Flask web application, including instructions for installing necessary software, creating a Dockerfile, and building and running the Docker container. The lab emphasizes the importance of Dockerfiles for maintaining and creating images efficiently.

Uploaded by

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

7 - Creating Your First Docker Image

This document outlines a lab step for creating a Docker image using a Dockerfile on a Linux environment for AWS. It explains the process of setting up a Python Flask web application, including instructions for installing necessary software, creating a Dockerfile, and building and running the Docker container. The lab emphasizes the importance of Dockerfiles for maintaining and creating images efficiently.

Uploaded by

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

Menu Browse Library Search in our library...

Training Library / Getting Started with Docker on Linux for AWS


1h 40m 50s left
Creating Your First Docker Image

Open Cloud Environment


100%
Setup completed Introduction
Average setup time: 3m 49s
Docker containers run on top of images. You have seen how to use images
on Docker's public registry, the Docker Hub. There are many different
Credentials images available. It is worth trying to find existing images when you can.
Inevitably, you will need to create your own images when making your own
Account ID
applications. In that case, you will still want to invest time into finding the
753705587437 right base layer to add your own layer(s) on top of.

Username
There are a couple of ways to make images. You can use docker commit to
create an image from a container's changes. The changes may come from
student using exec to open a shell in the container like in the previous Lab Step. The
other method is using a Dockerfile. A Dockerfile is easier to maintain, easier
Password to repeatedly create images from, and distributions easier. You will create a
Dockerfile in this Lab Step. Just know that it is possible to create
Ca1_VRjAKmFO equivalent images using commits.
Region Dockerfiles specify a sequence of instructions. Instructions can install
software, expose network ports, set the default command for running a
US West 2 container using the image, and other tasks. Instructions can really handle
anything required to configure the application. Many of the instructions
PEM PPK add layers. It is usually a good idea to keep the number of layers to a
Download Download reasonable number. There is overhead with each layer, and the total number
of layers in an image is limited. When the Dockerfile is ready, you can create
Bridge Connection the image using the docker build command.
100% Completed
You will see how all of this comes together by creating an image of a
Python Flask web app that randomly chooses what type of Cloud Academy
content you should look at next. The choice of Python as the example app
Lab steps is arbitrary. You will not focus on the specifics of the programming
language. You should be able to repeat the process for any other
1 Logging In to the Amazon programming language by following a similar process. Whatever
Web Services Console programming language or framework you are working with, you should
consult the Docker Hub documentation for the image, as it will usually
2 Connecting to the Virtual include advice on how to structure your Dockerfile.
Machine using EC2
Instance Connect Instructions
3 Installing Docker on 1. Install Git:
Amazon Linux Copy code

1 sudo yum -y install git


4 Using Docker without Root
Permission on Linux You will clone a code repository with the Flask app using Git.
5 Getting Docker Help from
2. Clone the code repository to your virtual machine:
the Command Line Copy code
Menu Browse Library
1 git clone https://github.com/cloudacademy/flask-content-advisor.git
6 Running Your First Docker
Container
3. Change to the apps directory:
7 Creating Your First Docker
Image Copy code

1 cd flask-content-advisor
Need help? Contact our support team

4. Create and start editing a Dockerfile using the nano text editor:
Copy code

1 nano Dockerfile

Note: The name of the Dockerfile must be Dockerfile with an uppercase "D"
and all other letters lowercase

5. Enter the following in the file:


Copy code

1 # Python v3 base layer


2 FROM python:3
3 # Set the working directory in the image's file system
4 WORKDIR /usr/src/app
5 # Copy everything in the host working directory to the container's di
6 COPY . .
7 # Install code dependencies in requirements.txt
8 RUN pip install --no-cache-dir -r requirements.txt
9 # Indicate that the server will be listening on port 5000
10 EXPOSE 5000
11 # Set the default command to run the app
12 CMD [ "python", "src/app.py" ]

The lines beginning with # are comments and explain what each instruction
is doing. Make sure you read the comments. Some highlights are:
FROM sets the base layer image
COPY . . copies all of the files in the code repository into the
container's /usr/src/app directory
RUN executes a command in a new layer at the top of the image
EXPOSE only indicates what port the container will be listening on, it
doesn't automatically open the port on the container
CMD sets the default command to run when a container is made from
the image
There are more instruction types, but this lab will only focus on those
mentioned. After completing the lab, you can review all of the instructions
available in Dockerfiles at the Dockerfile reference web page.

6. Once you have all of the instructions in the Dockerfile, press Ctrl+x,
enter Y when prompted to save, and press enter.
You are now back at the shell prompt.

7. Build the image from the Dockerfile:


Copy code
Menu Browse Library
1 docker build -t flask-content-advisor:latest .

The -t tells Docker to tag the image with the name flask-content-
advisor and tag latest. The . at the end tells Docker to look for a
Dockerfile in the current directory. Docker will report what it's doing to
build the image. Each instruction has its own step. Steps one and four take
longer than the others. Step one needs to pull several layers for the Python
3 base layer image and Step four downloads code dependencies for the
Flask web application framework. Notice that each Step ends with a notice
that an intermediate container was removed. Because layers are read-only,
Docker needs to create a container for each instruction. When the
instruction is complete, Docker commits it to a layer in the image and
discards the container.

8. Record your VM's public IP address:


Copy code

1 curl ipecho.net/plain; echo

You will need your IP to test that the web app is available with your
browser. The echo is only to put the shell prompt on a new line.

9. Open a new browser tab and navigate to the public IP address you just
recorded. The browser will fail to load anything since no server is running
yet. Keep the tab open for later.

Note: Your public IP will probably be different from the one in the image.
10. Now you can run a container using the image you just built:
Menu Browse Library
Copy code

1 docker run --name advisor -p 80:5000 flask-content-advisor

This runs a container named advisor and maps the container's port 5000 to
the host's port 80 (http). This time you didn't include -d to run in detached
mode. That is why you see output and you don't have the shell prompt
returned to you. If you did run with -d, you could get the same information
from docker logs.

11. Return to your browser tab with the public IP and refresh the page:

Now the advisor will be in fine form recommending what content to tackle
next after you complete this lab.

12. Return to the shell and notice that some web requests will have been
logged corresponding to your browser requests:

There are two requests because the browser automatically requests a


favicon in addition to the page content.

Summary
In this Lab Step, you created your own image using a Dockerfile. You also
ran a container using the image that was built.

VALIDATION CHECKS

1 Checks Start check

EC2 Instance Serves a Website Through Docker


The public IP of the Lab EC2 instance successfully returns
a 200 status code
Docker
Menu Browse Library
Did you like this End
Back Start check
step? Lab

ABOUT US COMMUNITY HELP


About QA Join Discord Channel Help Center

Legal & Privacy Sitemap System Status Manage your cookies


Copyright © 2024 QA. All rights reserved.

You might also like