A Gentle Introduction to Docker

A Gentle Introduction to Docker

Smooth Sailing with Docker: A Beginner's Guide to Containerization!

Overview:

As times change and the internet gravitates towards microservices and containerization, tools that can help build and manage such containers play a vital role. This is where Docker comes into the picture.

As a software framework, Docker provides us with a load of tools and utilities we can use to work with containers, configure and build them and optionally, orchestrate them as well.

Scope of the Article:

  • This article will be an introduction to containers and Docker.

  • Further, it will cover the installation of Docker, basic Docker commands and Docker Compose with the help of examples.

What is a Container?

A container can be conceptualized as a highly customizable and lightweight package that contains everything needed to run a piece of software, from binaries to source code.

It’s better to have multiple outlets of your favorite store across the world so it’s easily accessible instead of just a single branch at one location, right?

What is Docker?

Now that we are familiar with containers, let us see how Docker fits into this context.

Container technology basically consists of three elements:
1. Builder: A tool or utility used to create a container.

2. Engine: An application on which a container runs on. Like you need fuel to run a vehicle, a container needs a runtime.

3. Orchestration: Automate and manage multiple containers at once.

Docker is a software that provides multiple tools for all of your containerization requirements. Docker not only features a Dockerfile (a file where you can declare the properties of your container), it also provides us with Docker runtime and the Docker daemon, which acts as an engine for containers, and Docker Swarm which can be used to orchestrate containers.

Therefore, Docker is a software framework using which, we can create, manage and configure containers.

Installing Docker

To install Docker on your machine, you can simply download Docker’s static binaries from this link.

NOTE: For a Windows machine, a WSL(Windows Subsystem for Linux) backend is a necessity since Docker needs a Linux kernel to run its containers.

Once Docker binaries have been installed, we can check the installation by running the following command in your terminal:

docker run hello-world

If everything goes well, we shall see this output:

Key Terms

A Docker container is built using an image. Now you might ask, what is an image? Well, think of it this way: an image is a recipe, and a container is the dish.

  • Docker Image: Contains all the data, commands and configuration details needed to create a container. It is like a blueprint that specifies which files and dependencies are needed.

  • Docker Container: A running instance of the image that executes the instructions provided in the image to run an application.

  • Docker Hub: An online repository of images. We can either pull these images onto our local device and then execute them as containers, or we can create a Dockerfile and build our own images (and even customize images stored on Docker Hub).

Docker Commands

To explore the plethora of commands and utilities Docker has to offer, let’s just start simple and configure Nginx in a Docker container.

NOTE: Nginx is a popular open-source web server and is widely used across the world.

Starting a Container

“Docker run” is used to execute an image. Let’s dive deeper with an example!

In your terminal, run this command:

docker run -d nginx

In the above command, “run” is used to execute a Docker image(nginx). The “-d” flag stands for “detached”. If we execute this command without the “-d” flag, our terminal will be occupied by the Docker Daemon and we will not be able to access it.

This command also pulls the latest available Nginx image from Docker Hub and executes it.

This should be your output:

That’s it! But wait, since we have an Nginx container running, why aren’t we able to see the default Nginx page at our local machine’s port 80? That is because we haven’t connected port 80 of our container to the port of our local machine.

A container is like a very small computer. It needs to be connected to your local host for you to be able to access it.

docker run -d nginx -p 80:80

By using the “-p” flag, we can bind our local host’s 80 port with our container’s 80 port. The first argument is the host’s port and the latter is the container’s port.

After running this command, if we navigate to localhost:80 through the browser of our machine, sure enough, we shall be greeted by the default Nginx page.

Listing Containers and Images

To list all running containers on our machine, we use the

docker ps

command, and to list all images present on our machine, we use the

docker images

command.

For example:

Stopping a Container

We currently have an Nginx server running on our machine. What if we want to stop it? Simple! We use the following command:

docker stop <container name>

Make sure that we use the name of the container in the “docker stop” command, not the image.

Starting a Stopped Container

The following command is used to start a container that has been stopped. Again, the name of the container is used as an argument in this command.

docker start <container name>

NOTE: You can see the names of the images and containers using the “docker images” and “docker ps” commands respectively.

Deleting Containers and Images

We were successfully able to pull an Nginx image from Docker Hub, start it and access it from our browser. If we want to delete the container from our machine, we run:

docker rm <container name>

To delete an image from our machine, we run:

docker rmi <image name>

Docker Compose

NOTE:

  • Docker Compose allows you to define and run multi-container Docker applications as a single unit.

  • It provides a convenient way to manage the entire lifecycle of your application, from creating and starting containers to stopping and removing them.

  • Docker Compose uses a YAML file to define the services, networks, and volumes required for your application and provides a simple and consistent way to start and stop your application.

Running Docker commands can be a pretty cumbersome task. In a lot of scenarios, we need to pass a lot of arguments and their consequent flags (like “-d” and “-p”) for a container to run and execute in the way we want it to.

We use Docker Compose to easily configure and run multi-container Docker applications. It is mostly used to seamlessly develop, deploy and manage multiple containers at the same time, in a very easy-to-execute manner.

With Docker Compose, we can define our containers’ configurations in a YAML file, specifying ports, volumes and all the required parameters. Through a simple command-line interface, Docker Compose easily builds and manages our containers for us using the YAML file for definitions.

For example, to run an Nginx container, we previously used this command:

docker run -d nginx -p 80:80

To accomplish the same task using Docker Compose, we need YAML file named “docker-compose.yml” containing the following contents:

version: '3.8'

services:
  nginx:
    image: nginx
    ports:
      -80:80
    container_name: nginx

Let’s break this down.

This line is used to declare the version of the Docker Compose file format being used, which is 3.8 in this case.

version: '3.8'

This block is used to define all the services (containers) being used for this application. This file defines just one service, nginx.

services:

This block of the YAML file is used to define the various configurations of the nginx container, including the ports, image and the name of the container.

nginx:
    image: nginx
    ports:
      -80:80
    container_name: nginx

Now that we’ve created the docker-compose.yml file, we open up a terminal on the file’s path and run a simple command:

docker-compose up -d

This command builds a container from the provided image and executes it with the given configuration. Also, as we learned earlier, the “-d” flag runs the command in detached mode, i.e., the terminal we run the command in stays unaffected and the command executes in the background.
This should be our output:

We can run the following command to stop the containers defined by our YAML file:

docker-compose down

The execution of this command should produce the following output:

Not only is Docker Compose fairly easy to use, we can also conveniently manage multiple containers at once, and configure our applications and the containers attached to them.

Conclusion

  • Docker is a software framework for managing and configuring containers, providing tools such as Dockerfile, Docker runtime, and Docker daemon.

  • A container is a lightweight package that contains everything needed to run a piece of software, while an image is a recipe used to build the container.

  • Docker features a plethora of commands and tools for us to manage our containers.

  • Basic Docker commands include "docker run" to execute an image, "docker ps" to list running containers, and "docker stop" to stop a container.

  • Docker Compose can be used to automate and manage multiple containers at once.