AWS/Azure/OpenShift Proxmox/Hyper-V/VMware

How to Pull your first Nginx Container Image from Docker Hub and deploy it to your local machine

Docker-Guide-1
Docker Image (Nginx)

Docker is a great container tool for building and deploying your application. Docker Hub stores public Docker images. You can use the Docker command-line interface (Docker CLI) for loginpushpull, and other container image operations on your container registry.  Docker simply makes it easy for you to package the image you build locally and deploy it to a server that has a docker runtime environment. Once you build an image and it works on your local environment, it will definitely work well when you push it to a server with docker runtime environment. After we have an understanding of images and containers then we need a way of taking those containers and run it somewhere where they will be orchestrated for us, this is when Kubernetes comes into play.

In a couple of days from today, a guide on how to orchestrate a docker container with Kubernetes will be out. In the meantime, you can refer to my previous articles on Azure DevOps and GitHub integration for Docker and Kubernetes deployment to have pre-knowledge on how Kubernetes work with docker. You can also learn about how to Create an App Service Plan with Continuous Deployment to deploy a .NET Application from GitHub. Without wasting time, let’s quickly move on to explaining what exactly is Docker.

What is Docker? – Docker is a tool for running application in an isolated environment. It is much similar to a Virtual Machine but it’s much faster because it doesn’t require a lot of memory and the entire operating system to operate. The good thing about Docker is that your app runs in the exact same environment that is, if it works on my machine, it will definitely work on your machine, if it works in the staging environment, it will also work in the production environment. So, this is one of the benefits of Docker. Put it simply, Docker just works!

It is a standard for software deployment. Almost all organizations are adopting Docker for their software deployment because it makes it easier for packaging application. With Docker, Organizations don’t have to deal with different Windows OS, different distributions of Linux OS and everything you package in Docker will work in production.

Having taking an overview of what Docker is, it’s time to move on to learn about the differences between Containers and Virtual Machines.

Containers VS Virtual Machines (VMs)

Containers are an abstraction at the application layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated process in user space. From the diagram below, we could see that containers don’t require a full operating system and it simply shares the underlying operating system and run it in isolation with other containers. In contrast with Virtual Machines which are an abstraction of physical hardware turning one server into many servers. The hypervisor allows multiple VMs to run on a single machine. Each VM includes a full copy of an Operating System, the application, necessary binaries and libraries taking up tens of thousands of Gigabytes (GBs). VMs can also be slow to boot. Take a look at the diagram below to further understand the differences between Containers and Virtual Machines.

image-1
Containers VS Virtual Machines

As you can see from the diagram, under the containers component we have multiple apps and underneath we have container engine (Docker) and nothing else so Docker manages all the containers that you spin up using container. Then looking at the diagram again we have a single Operating System before the actual infrastructure. When you look at the VM component, you realize that each server requires an underlying Guest Operating System that allows it to run followed by a hypervisor environment before the actual Infrastructure. So, this is a typical difference between Containers and VMs.

What are the benefits of using Docker?

The following are the benefits of using Docker. They are:

  • Run container in seconds instead of minutes. With this, to spin a complete OS on VM can take a while
  • Less resources results less disk space and memory
  • Doesn’t need full OS to run
  • Deployment – what you deploy in one deployment stage will work on other stages
  • Testing – It makes it easy to carry out local testing before deploying to a real-environment

Now is time to Install Docker in our local machine. Go ahead and navigate to docs.docker.com/get-docker/ to download Docker Desktop. When you are there, you will see that you can download docker for MacOS, Windows or Linux. Installing it on any Operating System of your choice is the exact same thing. Since I am on Windows, let me go ahead and show you how to install it on Windows.

Apply the following steps to install Docker on your local machine:

image-2
Download Docker for Windows
  • Click on the blue button below Download Docker Desktop for Windows and select the WSL 2 backend tab as clearly shown in the screenshot below.  Why do you need WSL 2 as backend?  The reason is because with Docker Desktop running on WSL 2users can leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts. In addition, WSL 2 provides improvements to file system sharing, boot time, and allows access to some cool new features for Docker Desktop users.
image-3
Select WSL 2 Backend

Note! Docker only supports Docker Desktop on Windows for those versions of Windows 10 that are still within Microsoft’s servicing timeline.

What’s included in the installer

These are the things included in the Docker Desktop installation. They’re Docker Engine, Docker CLI client, Docker ComposeDocker Content TrustKubernetes, and Credential Helper.

I have already had Docker Desktop installed on my system so I won’t need to do a fresh installation again. If you have it as well installed already, open to your terminal. Here, I am using Git for Windows as my terminal. If you don’t have it installed, download it here. You can use your Windows command line tool (cmd).  After installing Git for Windows, go ahead and run the docker ps command to see that docker is installed and it is running.

image-4
Check the list of containers

From the screenshot, you will see the Container ID, Image, Command, Created, Status, Ports and Names listed out. These are the details that will display when you pull your first Docker Image from Docker Hub.

Now go ahead and quite/close Docker on your Window and try to run the docker ps command again on your terminal. You will see that you cannot connect to Docker daemon again. See the screenshot below.

image-5
Docker Daemon Stopped Screen

If you do not have docker installed already on your local machine, below are the steps to Install after downloading it

  • Double-click Docker Desktop Installer.exe to run the installer. If you haven’t already downloaded the installer (Docker Desktop Installer.exe), you can get it from Docker Hub or Docker Registry. It typically downloads to your Downloads folder, or you can run it from the recent downloads bar at the bottom of your web browser.
  • When prompted, ensure the Enable Hyper-V Windows Features or the Install required Windows components for WSL 2 option is selected on the Configuration page.
  • Follow the instructions on the installation wizard to authorize the installer and proceed with the install.
  • When the installation is successful, click Close to complete the installation process.
  • If your admin account is different to your user account, you must add the user to the docker-users group. Run Computer Management as an administrator and navigate to Local Users and Groups > Groups > docker-users. Right-click to add the user to the group. Log out and log back in for the changes to take effect.

After the Installation, double click on the Docker Desktop Icon on your Desktop or the tray menu icon to open Docker for Windows Runtime Environment to inspect it before pulling a docker image into it.

image-6
Docker Runtime Environment on the Local System

From the screenshot above you can see that we do not have any container running yet.

The next thing is to click on the setting gear icon to check the settings option based on your preference. Here I left everything at default. You can do the same as well. See the screenshot below:

image-7
Setting options

Note, you have to sign into Docker Hub with your Docker ID and password to have the image created published to the official docker hub that is, if you’re publishing a locally created container image to Docker hub. In our case today, we are only going to pull a container image and deploy same to our local system. To sign into docker hub, do so by clicking on the sign in option as shown in the screenshot above if you have already had an account on Docker Hub if not, click here to sign up for an account. The below screenshot shows the login interface.

image-8
Docker Hub Sign in interface

Before we begin pulling our first Nginx image from docker hub, let me show you some important commands in Docker.

  • Running docker on your terminal displays other commands
  • Run docker --version to see the current version of Docker you are running on your system. The version you have might be different but the command is the same.
  • Run docker pull {image} – Pulls an image from Docker hub
  • Run docker images – To see a list of all images in your Docker Desktop environment.
  • Run docker run – To start an image.
  • Run docker ps or docker conatiner ls – To see a list of running containers.

There are many other commands you can use in Docker. Navigate here to see the complete list.

What container image are we pulling? In this guide I will show you how to pull (download) a public Nginx image to your local system, add a tag, map the TCP port 8080 to TCP port 80 for Nginx container Image to have your Image run locally from your system using Docker Desktop.

Let’s go ahead and learn about how Docker Image is different from Docker Container.

Docker Image – An image is a template for creating an environment of your choice. It contains everything your app needs to run that’s the OS, Software and the Application codes. In contrast, Container comes into play after you have created your image. So, a Container is simply a running instance of an image. What you need to do is to have an image first and then from that image you run a container. See the diagram below for more information:
image-9
Image VS Container

Now let’s go again and pull a Nginx Image and run a container from it. To do so, visit docker hub to pull the image.  While on Docker Hub, you will see a list of tons of other public images. Search for a Nginx Image as shown in the screenshot below:

image-10
Nginx Image on Docker Hub

Run the command docker pull nginx on your terminal. Remember our docker runtime environment is currently empty.

image-11
Pulling Nginx Image

From the screenshot above, you can see that image has been pulled successfully. Let’s go ahead to check it in our docker runtime environment. The screenshot below shows that the Nginx Image has been pulled into my local machine and the status is not running.

image-12
Pulled Nginx Image

Let’s run the command docker images on the terminal as well to see the image available. See the screenshot below.

image-13
Image in Terminal

From the screenshot, the name of the image is Nginx, tag is latest which shows that the image is the latest version, the image Id, the date it was created which is 2 days ago and the Image size. Next, let’s go ahead and run a container from the Nginx Image we have just pulled. To do so, run the command docker run nginx:latest on your terminal. The latest added to the command simply means the latest version of the container will run.

Next, run docker container ls to see a list of all running containers. After that go back to the Docker Desktop environment to see that your container is running. The screenshot below shows that my container image.

image-14
A Container Image

From the screenshot, the four icons highlighted at the right corner of the screen specifies Docker CLI, stopping containers, restarting containers and deleting containers. Note, before you can delete a container, you must first stop the container from running. You can run as many containers as you want. See the screenshot of the terminal showing a running container.

image-15
Running Multiple containers

The above screenshot shows that my container is still running on the background and I ran the docker container ls command in a new terminal tab to display details for the running containers. This shows that the container running process is running on the background. The only way to see running process is by running it in a detached mode. So, let’s go ahead and run docker run -d nginx:latest. See the screenshot below for details.

image-16
Running Container in detached mode.

Now you will see that the process is no longer hanging and you got the Id. Now you see the list of all the running containers. By simply running docker container ls or docker ps commands. We have successfully managed to run our first containers. Checking the docker runtime environment now will show us that we have two container images with different names with both created from a Nginx Container Image. See the screenshot below.

image-17
List of Containers

Next let’s go ahead and learn how we are going to be able to use the container.

Exposing Ports: So far, we have our host which has docker running on our local computer. Right now, we have two containers running Nginx. These containers are running on TCP Port 80 as shown in the screenshot below.

image-18
Show TCP Ports

So, what we are going to do is from the host we will issue a request to our containers running port 80 so we will be able to access it on our local web browser through localhost: 8080. This port 8080 on our host should be mapped to the TCP port 80 on Nginx container and the only way to do this is by adding -p to TCP port 8080 before running the container. See the diagram below to understand better.

image-19
Map TCP port 8080 on host to TCP port 80 on Nginx

Now, let’s go ahead and open up our terminal or command line. Before we start, we will need to stop all running containers. To do that simply run docker stop and pass the container ids. From the screenshot below my container ids are 29001d8793b3 and eb7b9f5467a2, and I have them stopped by running docker stop 29001d8793b3 eb7b9f5467a2. Notice the containers shutting down gracefully from the screenshot below?

image-20
Containers shutting down

Now, we are going to map TCP port 8080 on our host to the container TCP port 80. To do so, let’s run docker run -d -p 8080:80 nginx:latest. Here, we are simply saying map port 8080 on local host to port 80 on the container.

image-21
Mapping TCP Ports

Finally, let’s go and browse the container web interface on our browser throughlocalhost:8080.

image-22
Container Web Interface.

Congratulations! You have successfully pulled a Nginx public image from Docker Hub, installed Docker Desktop, mapped TCP port 8080 on your host to TCP port 80 on the Nginx container to run the first Docker Container Image on our local machine. On a final note, you can map any port number which could be 5000, 3000, or any number at all.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x