Skip to main content

Command Palette

Search for a command to run...

Docker Basics: Easy Guide to Installation, Creating Images, and Mapping Ports

Published
5 min read
Docker Basics: Easy Guide to Installation, Creating Images, and Mapping Ports
O

Oluwaseun is a versatile Network, Cloud, and DevOps Engineer with over six years of experience. He also possesses DevOps and DevSecOps expertise, ensuring efficient and secure solutions in cloud environments. With over two years of experience as a trainer, he has trained over 200 participants in Cloud and DevOps and manages a YouTube channel dedicated to sharing his knowledge. Oluwaseun has a proven reputation for delivering flexible, scalable, and secure cloud solutions using the AWS Well-Architected Framework. He collaborates seamlessly with business stakeholders to achieve project objectives on time. A visionary professional, he excels in researching and adopting new technologies aligned with strategic business needs. He is meticulous, creative, and adaptable to diverse work cultures.

As modern software grows increasingly complex, the demand for agile, consistent deployment environments has given rise to containerization, with Docker at the forefront. Docker empowers developers to isolate applications, ensuring consistency across various environments. This article delves into essential Docker concepts, detailing the installation process, the anatomy of a Dockerfile, the mechanics of building images, and the practicalities of port mapping.

1. Docker Installation

The Docker Engine, the core component of Docker, can be installed on most operating systems. Docker’s approach ensures a secure and manageable deployment environment where application dependencies and configurations are packaged in isolated containers. Click here for the Installation guide

Installation on Linux

On Linux distributions like Ubuntu, Docker can be installed through a series of commands that set up the necessary packages, add Docker’s repository, and install Docker Engine. This installation begins by updating system packages to ensure compatibility:

#!/bin/bash

# Add Docker's official GPG key:
sudo apt-get update -y
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Next, necessary packages are added, including CA certificates for security and curl for data transfers. Docker’s official GPG key and repository are then added, allowing the user to access Docker’s stable releases

Finally, Docker Engine is installed and can be verified using:

docker --version

This multi-step process highlights the importance of secure and verified package handling, allowing for a reliable installation of Docker’s core components.

Installation on Windows and macOS

On Windows and macOS, Docker Desktop provides an intuitive installation pathway. This graphical interface integrates Docker CLI and Docker Engine, simplifying Docker management. It provides features tailored to each OS, like the Windows Subsystem for Linux (WSL), enhancing Docker’s cross-platform capabilities. Click here to learn more about Docker Desktop

2. Dockerfile Basics: The Blueprint of Docker Images

A Dockerfile is a scripted set of instructions that defines how a Docker image should be constructed. It serves as a blueprint, detailing everything from the base image to the runtime commands, enabling reproducibility and consistency.

Essential Dockerfile Commands

Each Dockerfile begins with the FROM directive, which designates a base image. This foundation is crucial, providing the OS environment and any pre-installed packages needed by the application. For example, for a Node.js application, the Dockerfile might begin with:

FROM node:20

Following FROM, the WORKDIR command establishes a working directory within the container, often enhancing clarity and organization in multi-stage builds:

WORKDIR /app

The COPY command is then used to bring in application files. For example, copying the package.json file allows dependencies to be installed separately, optimizing build cache utilization:

COPY package.json .
RUN npm install
COPY . .

Finally, EXPOSE and CMD define the networking port and the application’s execution command. Exposing ports is fundamental, as it allows inter-container communication and, when mapped, enables external access.

EXPOSE 3000
CMD ["npm", "start"]

Each command in a Dockerfile is layered, meaning modifications to one step require rebuilding subsequent layers. This layer-based architecture is one of Docker’s core strengths, allowing for optimized builds and efficient use of storage.

3. Building Docker Images: Constructing a Portable Environment

Once a Dockerfile is defined, building an image based on this file allows the application and its dependencies to be packaged as a single unit. This process consolidates all necessary software components, from libraries to runtime settings, creating a portable environment that can run consistently across any system with Docker installed.

To build an image, Docker uses the docker build command, where -t allows tagging of the image, enabling easy identification and version control. For example:

docker build -t my-node-app .

Here, Docker interprets each instruction in the Dockerfile, creating a new layer with each command. Each build step is cached, making subsequent builds faster.

Building an image is a pivotal step in Docker's workflow, as it translates the high-level Dockerfile instructions into a concrete, runnable container.

4. Port Mapping: Bridging Containers and the Outside World

Port mapping is essential in Docker to allow network traffic between the host system and the container. By default, containers are isolated, but applications typically require external access, such as a web server that needs to be reachable by users.

When running a container, the -p option in the docker run command maps a host port to a container port:

docker run -d -p 8080:3000 my-node-app

In this command, 8080 is the host port, and 3000 is the container’s internal port. This configuration enables users to access the application by visiting localhost:8080, while Docker routes the traffic to the application running inside the container.

Port mapping is particularly useful in development and production environments, as it provides flexibility. Multiple containers can run on the same host, each with a unique port configuration, minimizing resource conflict and optimizing host utilization.

Conclusion

Docker’s streamlined approach to application deployment relies on its foundational components: secure installation, clear Dockerfile instructions, efficient image building, and versatile port mapping. Each step in the Docker process, from creating Dockerfiles to running containers with custom port configurations, contributes to Docker’s role as a powerful solution for modern software development and deployment.