What is Docker? - Understanding Docker: A Beginner's Guide
Published: 2023-04-14 16:53:03

---
title: What is Docker? - Understanding Docker: A Beginner's Guide
published: true
date: 2023-04-14 11:04:06 UTC
tags: 
canonical_url: https://mungaibk.hashnode.dev/what-is-docker-understanding-docker-a-beginners-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**](https://hub.docker.com/). 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681468136986/ffe6f647-c893-4cce-b362-090f21fbac79.svg)

**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](https://www.docker.com/get-started/) 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

```

`docker version` displays the version of Docker on your machine.

You should see the following on your terminal:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681320254387/cd4375fb-6a9e-438b-adbf-9250a164ab4c.png)

**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

```

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](https://hub.docker.com/_/ubuntu). Ubuntu is a popular Linux distribution.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681324560173/e01ad41a-7224-44a1-85bd-7a3e9e433d7a.png)

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

Open your terminal and run the following command:

```
docker pull ubuntu

```

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._

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681327368350/ad7ac5c9-8fb3-49b5-9c7f-02a415b56015.png)

**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

```

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681330681799/77230e4c-8eb2-42e5-8988-14636ad61f03.png)

**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

```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681410526662/75762a8e-e68f-49ae-bdf3-67e74b56461f.png)

**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

```

The terminal will display to you the container ID.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681411504657/705165cf-3ee3-4ffc-a0a4-dfe8b5bf69b1.png)

**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

```

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681412094221/a7cc6b2c-ad7a-49a8-b3c7-c94d7d7fefd6.png)

**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

```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681412995854/ec233f2e-7406-448d-8ae2-5f76f07c8212.png)

**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, <mark>Up</mark> or <mark>Exited</mark>.

- 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

```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681414131037/7ddc8a30-88d7-4902-bcc3-c01bef674293.png)

**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 <mark>Exited</mark>.

### 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 <mark>Exited</mark>.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681415059990/e3571e90-6951-41ad-bcd3-8ceb6b59d294.png)

**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 <mark>Up</mark>.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681415474587/4e78283d-50d8-4de1-aa1f-b13e82a6191e.png)

**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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681416375132/dafcf9ed-fbda-4b52-9f59-8f3b2a640bd5.png)

**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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681417789805/2eb3bfb6-8f13-43b8-ab61-2be4b06cd110.png)

**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.