DEV Community

Brian Mungai
Brian Mungai

Posted on • Originally published at mungaibk.hashnode.dev on

What is Docker? - Understanding Docker: A Beginner's Guide

Pre-Requisite

A basic understanding of bash commands.

What is Docker?

Docker is a software platform that enables developers to build, run, test, and deploy applications.

Why Docker?

The development of applications is a collaborative process involving multiple developers. However, in the past, the process of shipping an application from one developer to the other has encountered issues. For instance, a software developer may develop an application on their local machine and then sends it to a tester to run various tests on the application.

Unfortunately, when the tester runs the application on their machine, it fails to run. Consequently, the tester would ask the software developer, "Why is the application not running on my machine?" The software developer's response would be, "But it runs perfectly on my machine." The tester is then left with the task of troubleshooting the application issues, which can be time-consuming and require significant effort to find a solution.

An application may fail to run on one's machine after it is shipped from another person's machine due to the following issues:

  • One or more files might be missing during the shipping process.

  • Software version mismatch, for example, different runtime environment versions on various machines.

  • The difference in application configuration settings.

Docker addresses these issues by introducing the concept of Containers and Images.

What are Containers and Images?

Images

Files with instructions that contain everything an application needs to run. May include the following:

  • Cut down of an operating system

  • A runtime environment required to run the application, for example, Node.js

  • Application files

  • Third-party libraries

  • Environment variables

Containers

Are running environments for Images.

How does Docker work?

Docker allows for the running of applications in isolation from the user's local machine operating system and hardware. From the software developer and tester scenario, the software developer creates a file known as a Dockerfile within the application folder.

A Dockerfile includes all the necessary instructions needed to run the application. A Dockerfile may contain the following:

  • A specific version of the runtime environment

  • A cutdown of an operating system, for example, Ubuntu

  • Environment variables

  • Configuration settings

Using a Docker command, the application is packaged into what is known as an image. The developer then pushes the image into a private Docker registry - many organizations have private image repositories - or a public repository, for example, Docker Hub. A Docker registry is a service that hosts and distributes Docker images.

From the Docker registry, the tester can pull the image and, using Docker, run the application on their local machine. Docker creates a container and executes the image using the instructions defined within the image's Dockerfile. The tester does not need to configure or install anything on their local machine; Using the Dockerfile instructions, Docker handles everything, from pulling the runtime environment to running the image.

Once done testing the application, the tester can stop or entirely delete the container and everything will be deleted from their local machine. The tester can also run as many containers as needed, even run applications with different runtime environment versions. The isolated nature of each container ensures that the applications run without interference.

Figure 1. A figure illustrating an image within a Docker container that is running within a Docker engine.

How to install Docker

To install Docker, follow detailed instructions from the Docker website.

Keep in mind the type of operating system running on your machine.

Starting Docker

Once you have installed Docker on your local machine, you have to start the Docker engine.

Take the following steps to start the Docker engine:

  • On Windows:

  • On Linux (Ubuntu):

Once the Docker engine is running. Copy and run the following command on your terminal:

docker version

Enter fullscreen mode Exit fullscreen mode

docker version displays the version of Docker on your machine.

You should see the following on your terminal:

Figure 2. Terminal displaying Docker version log.

The Docker Engine is up and running. Now, let us learn some basic Docker commands.

Docker Commands

This article covers the following commands:

  • docker images

  • docker pull

  • docker run

  • docker ps

  • docker start

  • docker stop

docker images

docker images list all images available locally on your machine.

Open your terminal and run the following command:

docker images

Enter fullscreen mode Exit fullscreen mode

Since you have no images installed, no images will be listed.

docker pull

docker pull command pulls images from a Docker registry and installs them on your local machine.

Let us pull the Ubuntu image from Docker Hub. Ubuntu is a popular Linux distribution.

Figure 3. Ubuntu image page on the Docker Hub website.

Open your terminal and run the following command:

docker pull ubuntu

Enter fullscreen mode Exit fullscreen mode

The command will pull the latest version of the Ubuntu image from Docker Hub to your local machine, in this case, Ubuntu version 22.04.

You can also specify the version you wish to pull from Docker Hub by adding a version tag, for example, docker pull ubuntu:18.04

Let us also pull Redis version 6.2. Redis is a fast, open-source, in-memory, key-value data store.

If you do not specify from which Docker registry to pull the image, Docker will pull the image automatically from Docker Hub.

Figure 4. The figure shows various versions of Redis images on Docker Hub.

Copy and run the following command on your terminal:

docker pull redis:6.2

Enter fullscreen mode Exit fullscreen mode

Once the command has been executed, run docker images on your terminal to list all images available on your local machine. You should see two images listed on your terminal.

Figure 5. Terminal displays images.

docker run

docker run command starts a container and runs an image inside it. You need to specify an image when using the run command, for example, docker run <image name>

docker run also searches for the specified image available on your local machine and if it fails to find it, it will pull it from Docker Hub and run it. So instead of using docker pull <image name>, you can just use docker run <image name> to quickly pull and run an image.

Let us run the Redis image version 6.2 that we pulled earlier from Docker Hub.

Copy and run the following command on your terminal:

docker run redis:6.2

Enter fullscreen mode Exit fullscreen mode

Once you run the command, Redis logs will be displayed on your terminal.

Figure 5. A terminal showing logs after running Redis.

A new container will be created, and the Redis image is run within it. When the container is running, you'll notice that you can not use the terminal to run other docker commands. A solution to this is to run the image in a detached mode by adding a -d flag. This will run the image in the background and leave the terminal free.

Take the following steps to run the image in detached mode:

  1. Press ctrl + c on Windows, or cmd + c on Mac to exit the container.

  2. Copy and run the following command on your terminal.

docker run -d redis:6.2

Enter fullscreen mode Exit fullscreen mode

The terminal will display to you the container ID.

Figure 6. A Terminal displaying Container ID.

On the other hand, when you run the Ubuntu image using the docker run ubuntu command, the image will fail to run, why? Since Ubuntu is a Linux distribution, we need to run it in interactive mode by adding a -it flag.

Copy and run the following command on your terminal:

docker run -it ubuntu

Enter fullscreen mode Exit fullscreen mode

Once you run the command, you will get to interact with the Ubuntu terminal. You can even run bash commands as you would do on your local machine.

Figure 7. A terminal displaying an Ubuntu terminal

On the terminal, you can execute bash commands such as pwd, and cd. You can create folders and files using mkdir and touch commands, respectively. All this runs within the container without installing Ubuntu on your local machine.

To exit the Ubuntu terminal and return to your local machine terminal, type exit on the Ubuntu terminal.

docker ps

docker ps command lists all running containers.

Copy and run the following command on your terminal:

docker ps

Enter fullscreen mode Exit fullscreen mode

Since we ran the Redis image, you will see the following on your terminal.

Figure 8. A terminal displaying a container running the Redis image.

Running docker run <image name> will create a container and run the specified image on it. Let us break down the terminal display:

  • CONTAINER ID - Docker issues unique IDs to every container created, this is to easily identify them. The ID displayed is a short version of the ID we received when we ran the image in detached mode earlier on.

  • IMAGE - States the image that is currently running within the container.

  • COMMAND - States the entry point that Docker used to access the container.

  • CREATED - States at what time the container was created.

  • STATUS - States the status of the container, Up or Exited.

  • PORTS - States the port number that the image is listening to. Since Redis is a database it requires a port number to connect and listen to incoming requests.

  • NAMES - A unique name generated by Docker to identify the container.

You can also list all containers that are running and those that have been terminated by adding a -a flag.

Copy and run the following command on your terminal:

docker ps -a

Enter fullscreen mode Exit fullscreen mode

You will see all containers that are currently running and those that are terminated.

Figure 9. Terminal displaying two containers, one of which is running and the other has been stopped

Since we terminated the Ubuntu container, the status was changed to Exited.

docker stop

docker stop command stops a running container. You need to get the container's ID or the container's name.

Take the following steps to stop a running container:

  1. Run docker ps on your terminal to list all running containers.

  2. Copy the container ID or name using the ctrl + c on Windows or cmd + c on Mac.

  3. Copy and run docker stop <container id\ container name>. Replace <container id \ container name> with the copied container ID or name.

  4. Run docker ps -a to list all containers.

You should see the status of the container changed to Exited.

Figure 10. Terminal displaying container with status "Exited".

docker start

docker start command restarts stopped containers. You need to get the container's ID or the container's name.

Take the following steps to start a running container:

  1. Run docker ps -a on your terminal to list all containers.

  2. Copy the container's ID or name using the ctrl + c on Windows or cmd + c on Mac.

  3. Run docker start <container's id\ container's name>. Replace <container id \ container name> with the copied container ID or name.

  4. Run docker ps to list all running containers.

You should see the status of the container changed to Up.

Figure 11. Terminal displaying container with status "Up".

docker rm

docker rm command deletes a container.

Take the following steps to delete a container:

  1. Run docker ps -a to list all the containers.

  2. Check if the container is running (up) or not. If it is running:

  3. Run the following command to delete the container, docker rm <container id\container name>.

  4. Run docker ps -a to confirm if the container is deleted.

Figure 12. A terminal displaying a container named "upbeat_brahmagu" has been deleted.

docker rmi

docker rmi command deletes installed images.

Take the following steps to delete an image:

  1. Run docker ps -a to list all containers. Ensure that no container created contains the image you want to delete. If a container references the image, you need to delete the container by running docker rm <container id\container name>.

  2. Run docker images to list all images.

  3. Copy the image's name or ID.

  4. Run docker rmi <image name\image ID>. Replacing <image id \ image name> with the copied image ID or name.

  5. Run docker images to check that the image is deleted.

Figure 13. A terminal displaying that Ubuntu image has been deleted.

Conclusion

In conclusion, Docker is a powerful tool for managing and deploying applications in a consistent and portable way. By containerizing your applications, you can ensure that they run the same way in any environment, whether it's your local machine or a development server.

In this article, we've covered the basics of Docker, including how to set it up on your computer, how to run your first container, and some common Docker commands that you'll need to know.

Thank you for reading.

Top comments (0)