All hail the whale! Running containers and pushing new images to Docker Hub.

Thomas Puntillo
9 min readJul 2, 2023

--

That’s right! This week we’re diving head first into Docker and not looking back! Docker is more popular than ever and companies all over the world are looking for engineers with the skills to build and run their applications. In this article I will show you how to run an Ubuntu container running an Apache server with a custom webpage. Come watch how easy it is to get started with containers. I will be working off of an Ubuntu 22.04 server to complete these steps

But before we get started, let’s get some key terms defined.

TERMINOLOGY:

IMAGE — Essentially, docker images are the files of an application needed for a container runtime (source code, binaries, meta-data, etc…). They are the basis for containers.

CONTAINER — In the simplest terms, a container is an isolated process running an application on a server. They are running instances of images. Since all the cpu and memory resources are present within the runtime, this prevents containers from competing for resources.

DOCKER — Docker is the world’s leading containerization platform. It is split into 3 basic components. The Client, The Docker_Host and the Registry.

DOCKER OFFICIAL IMAGE — An official docker image is an image that has been built by the engineers at Docker. It can be identified by the “Docker Official Image” ribbon. These images can be trusted as stable.

That’s all we have for terminology this week. Now let’s discuss the prerequisites necessary to follow along with this article

PREREQUISITES:

DOCKER HUB ACCOUNT — Docker hub is the registry we will be pulling our official image from. You will need to have an account. If you do not already have one, you can create one for free by navigating to https://hub.docker.com/signup.

LINUX SERVER — I will be using an EC2 Instance with Ubuntu 22.04 installed for this tutorial. You will need a similar setup to follow along.

OBJECTIVES:

1.Run a Docker Ubuntu container running detached and on port 80

2. Using BASH shell update all packages within the container .

3. Using BASH install Apache2 on the Ubuntu container.

4. Add a custom webpage.

5. Verify you can reach the webserver from your browser.

Let’s dive right in!

Step 1:

The first thing we’ll need to do is install docker on our machine. To do that ssh into your linux server and type in the following command:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

This will download and install the latest version of the docker onto your system. The output should look like this:

Now that we have docker installed, we can head over to https://hub.docker.com/ to search for the official docker image for Ubuntu. To find it, simply enter “Ubuntu” into the search bar.

This will take you to a list of Ubuntu images. We are going to select the top option called simply, “ubuntu”, which is a Docker Official Image.

To run the container in detached mode over port 80, run the following command in your terminal:

docker container run -it -d -p 80:80 ubuntu

The output should look like this:

You can check the status with the docker ps command as well.

docker ps

Step 1 complete!!! Now on to step 2.

Step 2:

The next thing we need to do is to enter the container with a BASH shell and upgrade all the packages. We do that with the following command:

docker exec -it <CONTAINER ID> bash

Since this is an Ubuntu image, we’ll use the apt package manager to update all the packages. Enter the following command:

apt update -y

Step 2 is done!!!

Step 3:

Moving right along, the next thing we’re going to do is install Apache. Please enter the following command:

apt install apache2

Step 3 complete!!!

Step 4:

Step 4: The next thing we need to do is to add a custom webpage. Luckily for us, Apache comes with a default index.html page located at /var/www/html/index.html. Let’s open that file in a text editor and make some changes to it.

First, we’ll need to install nano.

apt install nano

Now, using nano, we’ll edit the index.html file to give a custom message. Type the following command:

nano index.html

Scroll down until you reach the paragraph section and enter your custom message.

Step 4 done!!! Only 1 more to go!!!

Step 5:

Before we can test this out in our browser we need to enable and start the apache2 service if it is not already running. To do that, enter the following commands:

service apache2 status
service apache2 start

The final step in our objectives is to test if the Apache server is available from a browser. We’ll need to grab the host machine’s public ip address. I am running mine on an EC2 instance, so I’ll just copy mine from the AWS Console.

And now for the moment of truth…

We did it!!!

So there you have it. I knew we could get through it.

ADVANCED:

Now that we’ve verified that we can access our custom webpage through the browser, let’s go ahead and turn this into a new image!

ADVANCED OBJECTIVES:

1. Push to Docker Hub.

2. Pull your new image and run on a different port than 80 while still mapping to 80 on the container. Verify you can reach the webserver using the updated port.

Step 1:

Before we can push anything we first need to create a new image from our changes. To do that we use the docker commit command and provide a new name. If you’re still inside the container with a BASH shell, type “exit” to exit the container and get back to your terminal. Type the following command:

docker container commit <Container ID> <New_Image_Name>

You should receive a sha256 hash as output.

You can verify the new image has been created by listing the images.

docker image ls

Next, we’ll need to go back to Docker Hub and create a repository to push to. At your Docker Hub homepage, click the button at the top of the page labeled “Repositories”.

Click on “Create repository”.

Leave the namespace as defaulted to your username and provide a name for the new repository. Make sure the visibility is set to “public”. Click “Create”.

The next page displays the newly created repository.

Back in the terminal, list the docker images so the newly created image’s details are displayed on screen.

Next we need to tag our image. To do that we’ll use the docker tag command.

docker tag [Source_Repo]:[Tag] [Target_Repo]:[Tag]

List the containers again to see the tag. Also, notice how the repositories for the image we created as well as the one we tagged share the same Image ID.

Next use the following command to login to Docker Hub. You will be prompted for your username and password. (Note: since this is a linux instance, when you type your password, it will look like nothing is happening. Your password is being typed). If successful, you will see a “Login Succeeded” message.

docker login docker.io

Go back to the repo you created on docker hub and copy the docker push command into your clipboard.

Paste the command into your terminal, replacing “tagname” with whatever tag you gave the new image. Your output should look like this:

Next, go back to your new repository in Docker Hub and refresh the page. You should now see the image you just pushed under “Tags”.

That’s it for step 1!!

Step 2:

Now, let’s try pulling this new image, specifying a different port other than 80.

To do that, we’ll run the same docker run command we used earlier, only this time, we’ll substitute the port number and image name to suit our needs.

docker container run -it -d -p 8080:80 tompuntillo/week-16-repo:latest

Run another docker ps command to verify the container is up and running.

docker ps

Just like last time, we’ll enter the container in a bash terminal to start the Apache service.

docker exec -it ef8c617b7c08 bash

I restarted the EC2 instance my containers are running on during the course of writing this article, so I’ll go back to the AWS console and copy the new public ip address for this instance.

Ok, let’s try putting the public ip into a browser and add port 8080 to the end of the address.

Hmmm. That didn’t work. The Apache server took too long to respond when trying to use port 8080. Let me go check the inbound rules for the security group my EC2 instance belongs to.

Just as I suspected. We’re only allowing inbound HTTP traffic over port 80. We’ll need to edit our rules.

Ok, let’s try that again…

It works! Let’s celebrate!

And that’s all there is to it! We now have 2 separate containers, listening on 2 separate ports and both serving up the same custom webpage.

Hopefully this article illustrates to you how quickly you can get applications up and running when utilizing Docker. I hope you found this engaging and informative and I hope you had as much fun reading it as I had making it. See you next time!

--

--