Docker Images Basics

This article is a quick tutorial on Docker image management in Docker-ce on Ubuntu 20.


Introduction

After getting Docker installed, I found the best way to learn is to start running containers from prebuilt images. For this post, I am using Docker-ce, running on Ubuntu 20.

We will start with a prebuilt httpd image and get familiar with stopping, creating, and removing containers.

From there, we will clone an existing web repository and configure our httpd container to serve up the content.

Links to the Docker commands, image, and repository are available below:


Prerequisites

We will be cloning a repository from github, so make sure to download and install git. If you are using Ubuntu, you can use the following command.

1
sudo apt-get update && sudo apt-get install git

You can confirm git is installed by running the following command:

1
git --version

Docker Images

To host our web content, I will be using the official httpd image available on the Docker hub. This docker image runs Apache HTTP Server and contains only httpd with default settings.

Using the docker pull command, we will pull the httpd:latest image:

1
2
3
4
5
6
7
8

$ docker pull httpd:latest

latest: Pulling from library/httpd
Digest: sha256:5cc947a200524a822883dc6ce6456d852d7c5629ab177dfbf7e38c1b4a647705
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

By doing this step, we are caching a local copy of the httpd image, using the latest as a filter that only pulls the most recent version.

Now we can create our httpd container using the httpd:latest image.

1
2
3
4
5

$ docker run --name httpd -p 8080:80 -d httpd:latest

88efc101f1625ae80f0a0908e3d038ed02c5a11368afe78f04b3649868e4646c

The docker run command starts our container by first creating a writeable layer on the httpd:latest image. By specifying the -p 8080:80 flag, we associate the Docker host port 8080 to port 80 on the container.

Finally, by setting the -d' flag, we run the container in the background instead of the console session.

Now that we have completed our docker run command, we can check our container status with the docker ps command.

1
2
3
4
5
$ docker ps

CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS          PORTS                                   NAMES
88efc101f162   httpd:latest   "httpd-foreground"   22 seconds ago   Up 22 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd


Since this container has not been configured to host our web content, we will stop and remove the container.

You can use the docker stop and the docker rm` command to stop the running container and remove it from the Docker host.

1
2
3
4
5
6

$ docker stop httpd && docker rm httpd

httpd
httpd

We can confirm that the container has been stopped with the docker ps command. We can add the - a flag to confirm that the stopped container has also been removed.

1
2
3
4
$ docker ps -a

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES


Deploying a Web Server

Now that we have the basics of operating containers, we can begin deploying our website to a new httpd container. The first step will be to use the git clone command to clone our repository into a folder.

For the purposes of my lab, I will be cloning into the /opt directory.

1
2
3
4
5
6
7
8
9
/opt$ sudo git clone https://github.com/jlambert229/forty-html5up

Cloning into 'forty-html5up'...
remote: Enumerating objects: 94, done.
remote: Counting objects: 100% (94/94), done.
remote: Compressing objects: 100% (92/92), done.
remote: Total 94 (delta 4), reused 86 (delta 2), pack-reused 0
Unpacking objects: 100% (94/94), 2.06 MiB | 6.42 MiB/s, done.

Now that the web content has been cloned to the server, we can set up our docker container again. The command is similar to what we have done prior; however, we’ve added the -v flag.

Using the -v flag tells Docker to connect a mount point or volume to the container at a specific location. In our case, we will be mounting the cloned repository to the /usr/local/apache2/htdocs directory in the container.

This command will make the contents of our repository available in the ../htdocs directory.

1
2
3
4
docker run --name httpd -p 8080:80 -v /opt/forty-html5up:/usr/local/apache2/htdocs:ro -d httpd:latest

2fa2f82263407f549cc91cd3d54b84889f37616c9de084edd493e4102c19c4da



Testing our Web Server

Once we have completed our new docker run command, we can test our website in a browser with the URL of http://docker-host-ip:8080

The website should look similar to the image provided below:

docker-image-web-test


Closing Thoughts

This has been a quick run-through of the basics of managing Docker containers. To summarize, we went through adding, removing, and destroying containers and manually pulling down container images.

I hope this post was helpful!

Docker is quite a powerful tool, and I highly recommend taking the time to work with it.



Back to Top