7 - Creating Your First Docker Image
7 - Creating Your First Docker Image
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 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
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.
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.
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
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:
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