Create a Container Image and Run the Container
In this lab, we will first install Docker on a Linux box. Then we will create a container image and then run the container.
DevOps MPP Course Source
- This lab is used in course DevOps200.9x: Architecting Distributed Cloud Applications - Module 1.
Pre-requisites:
- There are no pre-requisites
Lab Tasks:
- Install Docker on a Linux box
- Create a Container Image and Run the Container
Estimated Lab Time:
- approx. 40 minutes
Task 1: Install Docker on a Linux box
-
In this lab, we will first install Docker on a Linux box (if you do not already have it installed). If you want to install it on Windows operating systems, please see the documentation.
-
We have a new Ubuntu Desktop 16.04 LTS, and we followed the instructions to install Docker CE at this link
Task 2: Create a Container Image and Run the Container
-
Let’s start with examining the start configuration, by issuing the following command:
sudo docker info
This will display the systemwide information. The result will look something like:
-
And now issue the following command to display all top-level images on this box:
sudo docker images
Assuming this is a brand new installation on this box, the result will not display any images.
-
Most of the images that you will create will be built on top of a base image that can be retrieved from the Docker Hubregistry. Let’s start with pulling the latest Debian image from the repository.
sudo docker pull debian
If no tag is provided, the Docker engine will use the
:latest
as the default. You will see Docker pulling the image, with an output that looks like the following: -
Now, let’s list the images we have, by issuing the command:
sudo docker images
You will see the image you have just pulled, listed as:
-
Docker images consist of multiple layers. To demonstrate, let’s pull the
jessie
, which is the latest release ofdebian
. This image shares layer with thedebian:latest
image.sudo docker pull debian:jessie
-
Notice that nothing is downloaded, because
debian:jessie
shares thedebian:latest
layer. Listing the images will give us: -
To see the history of an image, to see how the image was built, use the following command:
sudo docker history debian
-
Now, let’s start a new container, name it as
debian_bash
(--name
), startbash
in it, while capturing the STDIN on it (--interactive
), and also keeping the terminal open(--tty
), and remove the container automatically once we exit the shell.sudo docker run --name debian_bash --rm --interactive --tty debian bash
-
Notice we are now interacting with the new container, named
docker_bash
. Let’s start another terminal and issue the command:sudo docker ps
-
We can also run commands in a container from the command line of the host. Let’s create a new file by executing this in the background (
--detach
) and detach from the container.sudo docker exec --detach debian_bash touch /tmp/runACommand
When you go back to the first terminal and list the files on /tmp directory, you should see the new file.
-
Now, let’s assume we want to use OpenSSL to generate a self-signed certificate, but we do not want to install it to our main box. Instead, we want to install it on a container. We already have the bash shell open and waiting for us.
-
The first step is to install the openssl package. But to do that, we might need to update the package list first by running:
apt-get update
-
then install openssl,
apt-get install openssl
-
Now we can see openssl installed by checking its version:
openssl version
-
It’s all good, our container is ready to be used now. But wait. If we exit the shell now, the container we had just started with the name
debian_bash
will get deleted because we used--rm
option. However, we want to persist this as an image for later use. To do that, let’s first get the ID of the container with:sudo docker ps
-
We will need to grab the container ID, which is “c3ef9a315c5e” in this case, and use the following command to commit the changes, and create a new container image with the name
debian_openssl
and labelv1.0.1t
to identify the openssl version. Please note that although we can use anything to label the image, it is best practice to be as specific as possible.sudo docker commit --author "Ercenk Keresteci" --message "OpenSSL v1.0.1t" c3ef9a315c5e debian_openssl:v1.0.1t
-
To check the new image, now let’s issue:
sudo docker images
-
We can now exit from the bash shell, and observe the container to be removed automatically by issuing
sudo docker ps
-
However, when we list the images, we will continue to see the image we had just committed. You can also check the history of the image you have just created by seeing the layers, with the command:
sudo docker history debian_openssl:v1.0.1t
Summary
In this lab you completed the following tasks:
- Installed Docker on a Linux box
- Created a Container Image and Ran the Container