An Introduction to Docker

Rasula Yadithya
Technoid Community
Published in
7 min readJun 22, 2023

Picture this. You join a startup, and start working in a team constantly rolling out new features, and dealing with new technologies and tools. The more complex the project becomes, the more complex it becomes to set it up. And the more complex it is to set up, the higher the chance that something could go wrong.

Enter Docker.

Docker takes the hassle of configuring environments, and managing dependencies off the shoulders of the developer. It bundles an application with its dependencies into a container. This is just one of the advantages that Docker provides. In this guide we’ll take a look at why you might want to start using Docker, and how you can leverage it to make your development and deployment process more efficient.

Why Docker?

  • Takes care of dependencies and configurations

As developers, we don’t need to spend time making sure all the dependencies for the project are installed, the environment is properly configured, or that everything works together. Since Docker bundles everything together in an image, all that is handled for us.

  • Compatible on any device with Docker

As long as the machine you want to run the application on has Docker, you can run an instance of the Docker image without having to worry about what the operating system is, or what was used to create the app.

  • Isolation of services

Because the Docker containers are isolated from each other, developers don’t need to worry about multiple services interfering with each other.

  • Portable

Docker containers are easier and faster to set up and run compared to traditional VMs, and utilize available resources much better.

Docker Concepts

You might come across a few Docker related lingo that you’re unfamiliar with when you first get started, but let’s quickly break them down.

Docker Images

As mentioned, Docker deals with all the application dependencies, configurations, runtimes etc. It does so by packaging everything that should be included with the application into a single bundle. This is what we call a Docker image.

Docker Containers

A container is just a running instance of a Docker image. The same image can be used to create multiple containers.

Docker Registry

There are plenty of preconfigured images for us to use which have been created by various publishers. These images can be found on centralized public libraries called docker registries. Docker Hub is a good example. Private registries can also be created to store and manage images a company might want to keep private.

Dockerfile

A Dockerfile is used to create a new Docker image out of an existing application. It is where we provide the definitions for the image, like the base image to use, commands to carry out etc.

Getting Started

Installing Docker

Now that we’ve familiarized ourselves with the basic terminology, let’s install Docker. Installing Docker can vary slightly depending on the operating system that you are on. For Windows, head over to https://www.docker.com/products/docker-desktop, download the application, and follow the installation process.

Once you are done with the installation process, you can confirm that Docker has installed successfully by running the docker --version command on the terminal.

Running your first Docker container

Once we have successfully installed Docker, we can go about running our first Docker container. A great place to start is with the hello-world container offered by Docker.

Running a Docker container has two main steps.

  1. Pull the Docker image
  2. Run the Docker container

Since we will be using a pre-built Docker image, we can pull it from Docker hub using the docker pull hello-world command.

After pulling the image, we can run it to create a new container using the docker run hello-world command.

This confirms that we installed Docker, and ran our first container successfully.

We can also simply call the docker run hello-world command without pulling. If the image is not available locally, Docker will search for the image and pull it automatically.

Running MongoDB on Docker

Now that we’ve taken a look at running a simple Docker image, let’s tackle something a bit more complex.

We will explore how to run instances of MongoDB and Mongo-Express on Docker, which will help us to quickly setup a database and an accompanying user interface.

MongoDB is an open-source NoSQL database, while Mongo-Express is a simple web-app made using Node.js and Express.

Because MongoDB and Mongo-Express are two separate containers, we need to enable communication between the two for them to work together. Therefore, the first step is to create a Docker network.

Creating a Docker network

To create a Docker network we use the command docker network create mongo-network. Here mongo-network is the name of our container.

To check if the network was created successfully, run docker network ls.

Once we have confirmed that the mongo-network was created, we can run our Docker images.

Start by creating a MongoDB container.

Running the MongoDB container

To run the MongoDB container, use the following command.

docker run -d - network mongo-network - name mongodb -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password mongo

Let’s break down the parts of the above command.

-d runs the container in detached mode. This means the container will run in the background and not rely on the terminal instance.

— network mongo-network is used to specify that the MongoDB container should be connected to the mongo-network.

— name mongodb assigns the name “mongodb” to the container.

-p 27017:27017 maps the port of the MongoDB container to the 27017 port of the machine.

-e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password sets the environment variables for the container.

Next, we can create the Mongo-Express container.

Running the Mongo-Express container

The following command will run the Mongo-Express container.

docker run -d - network mongo-network - name mongo-express -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express

In the above command,

-d runs the container in detached mode.

— network mongo-network specifies the network for the container to use.

— name mongo-express assigns the name “mongo-express” to the container.

-p 8081:8081 maps the port 8081 of the container to the host.

-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express sets the environment variables.

Once both containers are running, navigate to http://localhost:8081 to open the Mongo-Express web-app.

Docker Compose

Even from the two commands that we ran above to create MongoDB and Mongo-Express instances, we can understand that complex and detailed lines of commands are needed to run the instances. Fortunately, there is a better solution, which is Docker Compose.

Docker Compose is a tool that lets you manage multi-container applications. We use a YAML file to define the containers and configure them. The file describes the images to use, the ports to expose, environmental variables that should be declared, networks that should be used etc.

In our example, we can define the MongoDB and Mongo-Express services in a docker-compose.yml file to quickly create the containers.

Here is what the file will look like.

version: '3'
services:
mongodb:
image: mongo
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=mongoadmin
- MONGO_INITDB_ROOT_PASSWORD=secret
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db

mongo-express:
image: mongo-express
container_name: mongo-express
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=mongoadmin
- ME_CONFIG_MONGODB_ADMINPASSWORD=secret
- ME_CONFIG_MONGODB_SERVER=mongodb
ports:
- 8081:8081
depends_on:
- mongodb

volumes:
mongodb_data_container:

In this code,

version: ‘3’ is an indication of the Docker Compose version that we are using.

After defining the version, we indicate the separate services that we should create under services.

Under each service there is an image, which is the Docker image that each service will use.

container_name describes the names given for the created containers.

Under environment, the created environment variables are defined.

ports maps the container’s port to the host port.

volumes are used to persist data across the containers.

And the depends_on tag under mongo-express states that the mongo-express service depends on the mongodb service.

After the docker-compose.yml file is defined, the simple command docker compose up -d will run the two containers in detached mode.

As such, defining a Docker Compose file helps to greatly simplify the process of managing multiple containers.

Conclusion

This article covered the basics of Docker, the main concepts, and how to run your first Docker instance. It also took a look at a more complex example also involving Docker networks, and finally how you can create a Docker Compose file to automate managing multiple containers.

Docker is a really versatile tool which can help to cut down on issues related to setting up environments, and lets developers focus on creating better apps.

--

--