Skip to main content

Command Palette

Search for a command to run...

A Beginner’s Guide to Docker: Learn How to Containerize a WebApp

Published
10 min read
A Beginner’s Guide to Docker: Learn How to Containerize a WebApp

This guide tells you everything you need to know about Docker, including what it is, the basic commands you’ll come across, how to build an image, and how to containerize your application easily with Docker. Even if you have zero prior experience, this tutorial demystifies Docker and helps you learn enough to get started.

What is Docker?

Docker is an open-source virtualization software that makes it easy to develop and deploy applications. It makes it easy to run different versions of the same app without any conflicts.

Think of Docker as a tool that helps system administrators and developers deploy their applications in a sandbox called containers. Docker allows you to package an application and its dependencies into a standardized unit.

Why Docker?

Docker helps you run each service with its own dependencies in separate containers. It standardizes the process of running any service in local development environments.

Furthermore, Docker allows you to separate your applications from your infrastructure. You can significantly decrease the delay between developing and running applications. So, it helps you deliver software as quickly as possible.

What are Containers?

Containers are completely isolated environments, and each has its own mounts, processes, and networks. They are running instances of images that are isolated and have their own environments. Using the Docker API or CLI, you can create, start, stop, and move containers.

Additionally, containers are lightweight. They provide a more cost-effective alternative to virtual machines. Also, they have everything you need to run an application successfully, so you don’t have to depend on what is installed on your host. Developers can focus on writing the code instead of worrying about the environment within which they’ll deploy the app. Docker containers can run on a laptop, cloud provider, physical machines, or other environments.

Prerequisites

  • A Docker Hub account

  • Comfort using the command line and a text editor

  • A simple website code pushed to Github (optional)

Docker Installation

Getting Docker up and running on your OS is straightforward. Follow the detailed instructions on the official website for installing Docker on Mac, Linux, or Windows.

For this tutorial, I’ll be following the convenience script here for installing the Docker engine on Ubuntu. Keep in mind that this convenience script is not recommended for production environments.

After installing Docker, you can confirm your installation by running the following command:

sudo docker run hello-world

Docker Terminology

Before we go further, we need to clarify some terms you will come across in this guide and the Docker ecosystem.

Images: An image is a package or template, used for creating one or more containers.

Containers: A container is a running instance of an image.

Dockerfile: This is a text-based file that contains a script of instructions and commands. Docker builds an image using this script.

Docker Hub: This is a registry service on the cloud for Docker images. Developers can push and pull images to and from Docker Hub.

Docker Daemon: The Docker Daemon is a persistent process running on the host operating system. It manages Docker objects such as images, containers, networks, and storage volume.

Docker Client: The command line tool that lets you communicate with the Docker Daemon.

Basic Docker Commands

Great, now you are familiar with common terms in the ecosystem. Let’s go further by understanding basic Docker commands. Fortunately, you don’t have to memorize them. You can always refer to this guide, the official Docker website, or Google for help.

Keep in mind that running docker commands require sudo privilege. So you can't run these commands without adding the sudo preface.

Follow this short guide if you want to learn how to add a user to the docker group. This enables you to run docker commands without sudo as we'll be doing here.

Docker - -version: This command gets the current version of Docker you have installed

Docker pull: This command pulls an image and doesn’t run the container

In the example above, Docker pulls the latest version of Alpine. When using the pull command, you can specify the exact version of the image to pull by using tags as we’ll see below.

Docker images: This shows you a list of all the available images

Docker run: This runs a container from an image on the docker host if it exists already. If it doesn’t, this command goes to Docker Hub and pulls the image.

Wait, there’s nothing! Why didn't the run command do anything?

When you enter the docker run command without specifying any task, the container boots up, runs an empty command, and exits immediately.

Now, let’s try something more fun.

Docker ps: This command lists all the running containers and basic information about them, such as the container ID

We don’t have any running containers at the moment.

Docker ps –a: This is a more useful variant of the ps command. It shows all running and stopped containers.

Docker stop: This command stops a running command. But you have to provide the container name or container ID

Docker rm: This command removes a stopped container permanently.

In the example above, we listed the containers using the docker ps -a command. To remove the first container, we use the docker rm command followed by the container ID.

Note that the first six characters can work as the container ID, you don’t have to copy everything.

Docker rmi: This command removes images. However, you must ensure that no container is running off of this image

We get an error while trying to remove the alpine image. Let’s fix that by deleting the containers using the Alpine image before removing the image itself.

Now, we can run the docker rmi command successfully:

Note that you can forcefully remove an image without checking for containers using the -f flag. The syntax is docker rmi -f {image-name}

Docker run –d: The –d flag runs the container in the background, so you can have access to your terminal. This flag is useful when starting commands that will run for a long time.

Docker run --name: Docker creates a unique name for each container by default. However, you can specify a name for your container using the --name option.

Now, when we run the docker ps command, we’ll see our container running with the specified name.

Docker run –it: This enables an interactive mode on the container and allows you to connect to a shell-like bash. With the -it option, your container executes /bin/bash, then waits for your input. When you are done, type exit

Docker exec: This helps you execute commands on a running container. We have a running redis container. Let’s ask Docker to run the whoami and date commands in our running container, my-redis.

Docker stop: As the name suggests, this command stops a running container.

In the example above, the docker ps command shows we have a running redis container. We stopped it using the docker stop command, followed by the container ID.

Docker login: This command is used when you want to login to Docker Hub

Docker build: This command creates an image from a specified Dockerfile

We’ll see examples of docker login and docker build commands as we go further in this tutorial.

Deploy a Static WebApp with Docker

We’ve looked at Docker terminology and basic commands. Now, it’s time to explore the real stuff: containerizing applications.

We’re going to deploy a static website with Docker. Before this tutorial, I cloned the Netflix landing page and pushed the code to Github. You can follow along with any single-page website you have.

Clone your Project GitHub Repository

To start, we need to clone the GitHub repository for our website. In my case, I’ll be cloning the code for the Netflix landing page from GitHub to my machine.

Replace with the URL of your GitHub project. You can name your directory anything you like.

After cloning, we’ll cd into the directory we just created.

The next step is to create an image with this web app. We’ve mentioned images before in this guide. But one thing you should note is that there are different images.

We have Base images and Child images. Base images are usually those with an OS, such as Ubuntu, Debian, Alpine, Redis, CentOS, and so on.

We use base images to create container images. On the other hand, child images are those we build based on Base images.

Dockerfile

To package our website in an image, we need to create a simple text file called Dockerfile.

When the file opens, we’ll write our commands. Keep in mind that the content of the Dockerfile varies depending on the application we’re deploying.

For our simple application, we’ll be creating an image based on Nginx.

Here, we’re using the FROM command to base our image on Nginx, then the COPY command copies our cloned files into the new image under the /usr/share/nginx/html directory.

After that, we set our working directory to that path. You can now save this file and exit.

Create an Image

Now that we have our Dockerfile, the next step is to build our image. The docker build command helps to create a Docker image from our Dockerfile.

We’ll build our image using the commands below:

Before you run the command, replace my username with yours. Your username is the one you created when registering on Docker Hub. Also, don’t forget the period.

The docker build command takes an optional tag name and the location of the directory containing your Dockerfile. The period tells Docker that the file is in our current directory.

Below is our output from running the command

If everything went successfully, our image should be ready. Let’s enter Docker images and see if it’s present.

Next, we’ll run this image and see if it works.

From the above command, we’re running our image in the detached mode by using the -d flag. Also, we use the -p flag to expose port 80 inside the container to port 80 outside the container.

Let’s head over to our browser, and paste our IP address where our app should be live. Also, if you’ve been running these commands locally, head over to your localhost URL.

If you don’t know the IP address of your virtual machine, you can use the command below to get it.

The command below shows my public IP address:

dig +short myip.opendns.com @resolver1.opendns.com

I’ll be using the public IP address of my virtual machine to access our live app.

There, we have it!

Congratulations on creating your first Docker image 🎉

Push your Image to Docker Hub

In this section, we’ll push our image to Docker Hub, so that anyone that has Docker installed can pull it and play around with it.

Before we push to Docker Hub, we need to log in. Provide the credentials you use when logging in to your account.

We’ll type docker login and follow the prompt to provide our details.

To publish, type the command below. Remember to replace the name of the image with yours. Note that your username comes first, followed by the image name.

After pushing your image, you should get an output like this:

Now, when I go to my Docker Hub account, I’ll see the published image

Conclusion

You should be proud of yourself if you followed this tutorial from the beginning to the end. This guide introduced Docker as a powerful containerization tool when it comes to building, running, and managing applications. Using Docker ensures that your applications run efficiently regardless of the underlying infrastructure.

Your journey into the world of containerization just got started. I hope this tutorial provides you with enough knowledge to navigate this field.

If you have suggestions or other topics you’d like me to write on, feel free to reach out. I’m on LinkedIn and Twitter. I would love to hear from you.

C

Very insightful🔥 And also an interesting read

P

Thank you so much