Getting Started with Docker: A Beginner’s Guide

Docker has revolutionized the way we develop, deploy, and manage applications. With its powerful containerization technology, Docker simplifies the process of packaging and running applications in isolated environments. In this post, we’ll walk through the basics of Docker, including its architecture, benefits, and key commands.

What is Docker?

Docker is an open-source containerization platform that enables developers to package applications and their dependencies into containers. Containers are lightweight, standalone, and executable units that encapsulate everything needed to run an application—code, runtime, libraries, and configuration files. This ensures that applications run consistently across different environments, from development to production.

Docker Architecture

Docker’s architecture consists of several key components:

  • Docker Engine: The core component that includes the Docker Daemon, Docker Client, and REST APIs. The Docker Daemon runs in the background, managing Docker objects like images, containers, networks, and volumes. The Docker Client interacts with the Daemon through commands, while REST APIs allow external applications to communicate with the Docker Daemon.

  • Docker Daemon: Listens for Docker API requests and manages containers. It handles pulling images from registries, creating containers, and managing their lifecycle.

  • Docker Client: The command-line tool or graphical interface used to interact with Docker. It sends commands to the Docker Daemon and receives responses.

Docker Images and Containers

  • Docker Images: These are lightweight, standalone packages that include everything needed to run an application. Images are stored in Docker registries and can be used to create containers. They contain the application code, runtime, libraries, and configuration files.

  • Docker Containers: A container is a running instance of an image. It encapsulates the application and its dependencies in an isolated environment. Containers share the host operating system’s kernel but operate independently, ensuring consistent behavior across different systems.

How Docker Runs a Container

Here’s the flow of events when you run a Docker container:

  1. Command Execution: When you execute a command like docker run image_name, the Docker Client sends this request to the Docker Daemon.

  2. Image Check: The Docker Daemon checks if the required image is available locally on your machine. If it’s not, the Daemon pulls the image from a Docker registry.

  3. Container Creation: Once the image is available, the Docker Daemon creates a new container from this image.

  4. Container Start: The Docker Daemon starts the container, running the application in the isolated environment specified by the image.

  5. Port Mapping (if applicable): If port mapping is specified, the Docker Daemon maps the container’s ports to the host’s ports, allowing external access to the containerized application.

Port Mapping

Containers run in isolated environments, and port mapping allows you to expose specific ports to the outside world. By mapping a port on your host machine to a port on the container, you make your application accessible externally. Use the -p option with the docker run command for port mapping:

docker run -p host_port:container_port image_name

For example: docker run -p 27017:27017 mongo

This maps port 27017 on your host machine to port 27017 on the container.

Any request coming to your host machine on port 27017 will be forwarded to the port 27017 on the container , thereby exposing the MongoDb service running on the container

Basic Docker Commands

  • Run a Container: To start a container from an image, use:

    docker run -d image_name

    The -d flag runs the container in detached mode, allowing it to run in the background.

  • List Running Containers: To see a list of running containers, use:

    docker ps

  • Stop a Container: To stop a specific container, use:

    docker stop container_id

  • Kill a container: To forcefully kill a container, use:

    docker kill container_id

  • Remove a Container: To remove a stopped container, use:

    docker rm container-id

  • Remove an Image: To remove an image, use:

    docker rmi image_name

    • Use the --force flag if the image is being used by containers:

docker rmi image_name --force

Conclusion

Docker simplifies application deployment by providing a consistent environment through containerization. Understanding Docker’s architecture, images, containers, and key commands is essential for effective use. With Docker, you can easily package, deploy, and manage your applications, ensuring they run seamlessly across different environments.

0
Subscribe to my newsletter

Read articles from Frepin Gonsalvese directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Frepin Gonsalvese
Frepin Gonsalvese