Docker Basics: Understanding Key Components

Ajay Verma
AWS in Plain English
6 min readApr 28, 2024

--

Docker is a containerization platform that allows developers to package, distribute, and run applications in isolated environments called containers. Docker provides a platform for developing, shipping, and running applications in containers. Let’s break down its core components:

1. Docker Engine: The heart of Docker, it’s a client-server application with:

  • A server featuring a daemon process (dockerd)
  • A REST API for interacting with the daemon
  • A command-line interface (CLI) client (docker)

The engine builds and runs your containers based on instructions from the Dockerfile and user commands.

2. Docker Images: A Docker image is a lightweight, standalone, and executable software package that contains everything needed to run a piece of software, including the code, runtime, libraries, dependencies, and configurations. Images are built using a Dockerfile, which defines the instructions for building the image.

  • Read-only templates containing instructions to create containers.
  • Composed of multiple layers, each representing an instruction in the Dockerfile (e.g., installing a software package).
  • Stored in registries like Docker Hub for sharing and reuse.

Example Dockerfile:

FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD [“python”, “app.py”]

3. Dockerfile: A Dockerfile is a text file that contains a series of instructions for building a Docker image. These instructions include commands to set up the base image, copy files into the image, install dependencies, configure the environment, and define the entry point for the application.

  • A text file with instructions to build a Docker image.
  • Specifies the base image, required software, environment variables, and commands to run within the container.

Example Dockerfile commands:

  • FROM: Specifies the base image to build upon.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from the host into the container.
  • RUN: Executes commands inside the container during the build process.
  • CMD or ENTRYPOINT: Defines the command to run when the container starts.

4. Docker Containers: A Docker container is a runtime instance of a Docker image. It encapsulates the application along with its dependencies and provides a consistent environment for running the application across different platforms.

  • Runnable instances of Docker images.
  • Isolated environments with their own file systems, processes, and network interfaces.
  • Ephemeral and can be easily created, stopped, started, or deleted.

Example CLI command to create a container from an image:

docker run -d -p 8080:80 my_image

5. Docker Hub: Docker Hub is a cloud-based repository that allows developers to store, share, and distribute Docker images. It serves as a central registry for Docker images, providing a platform for collaboration and version control.

  • A public registry for hosting and sharing Docker images.
  • Offers both public and private repositories.
  • Allows easy access to pre-built images for various applications.

Example CLI commands for Docker Hub:

Log in to Docker Hub: docker login
Push an image to Docker Hub: docker push my_image

6. Docker Registry: A Docker registry is a server-side application that stores and manages Docker images. While Docker Hub is the official public registry, organizations may also set up private registries to store proprietary or sensitive images internally.

  • A service for storing and distributing Docker images.
  • Can be public (like Docker Hub) or private (for internal use).

7. Docker Volumes: Docker volumes provide a way to persist data generated by and used by Docker containers. They are used to share data between containers or to persist data beyond the lifecycle of a container. Volumes are particularly useful for databases, log files, and other persistent storage needs.

Example CLI command to create a named volume: docker volume create my_volume

Example CLI command to mount a volume when running a container: docker run -d -p 8080:80 -v my_volume:/data my_image

8. Docker Networking: Docker networking allows containers to communicate with each other and with external networks. By default, Docker creates a bridge network for containers to communicate with each other, but users can create custom networks to isolate containers or control traffic flow.

Example CLI command to create a custom network: docker network create my_network

Example CLI command to run a container on a custom network: docker run -d — network=my_network my_image

9. Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. It uses YAML files to define the services, networks, and volumes required for an application and can start, stop, and manage the entire application stack with a single command.

Example docker-compose.yml file:

Example CLI command to start Docker Compose services: docker-compose up -d

10. Docker Swarm: Docker Swarm is a native clustering and orchestration tool for Docker containers. It allows users to create and manage a cluster of Docker hosts, known as nodes, and deploy services across the cluster for high availability and scalability.

Example CLI command to initialize a Docker Swarm: docker swarm init

Example CLI command to deploy a service in Docker Swarm: docker service create — replicas 3 — name my_service my_image

11. Docker Security: Docker provides various security features to protect containers and the underlying host system. These include container isolation, resource constraints, user namespaces, and security profiles. It’s essential to understand and implement best practices for securing Docker deployments to mitigate potential risks.

Example CLI command to run a container with restricted resources: docker run -d — memory=512m — cpus=0.5 my_image

By mastering these additional concepts and commands, you’ll have a comprehensive understanding of Docker and be equipped to leverage its full potential for developing, deploying, and managing containerized applications in diverse environments.

Dependencies:

  • Base Image: The foundation on which you build your image, often an operating system like Ubuntu or Alpine.
  • Software Packages: Libraries and applications needed for your application to run.

Example: Let’s say you want to build a Docker image for a simple Python web application:

Dockerfile:

FROM python:3.11

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD [“python”, “app.py”]

This Dockerfile: A Dockerfile is a text file that contains a series of instructions for building a Docker image. These instructions include commands to set up the base image, copy files into the image, install dependencies, configure the environment, and define the entry point for the application.

  • Uses the python:3.11 image as the base.
  • Sets the working directory to /app within the container.
  • Copies the requirements.txt file and installs Python dependencies.
  • Copies the application code to the container.
  • Defines the command to run the application (python app.py).

Example Dockerfile commands:

  • FROM: Specifies the base image to build upon.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from the host into the container.
  • RUN: Executes commands inside the container during the build process.
  • CMD or ENTRYPOINT: Defines the command to run when the container starts.

CLI Commands:

  • Build an image: docker build -t my-python-app .
  • Run a container: docker run -d -p 80:80 my-python-app
  • List images: docker images
  • List containers: docker ps
  • Stop a container: docker stop <container_id>
  • Push an image to Docker Hub: docker push my-username/my-python-app
  • Stop a container: docker stop <container_id>
  • Remove a container: docker rm <container_id>
  • Login to Docker Hub: docker login
  • Push an image to Docker Hub: docker push my-username/my-python-app

Important CLI Commands:

docker build: Builds a Docker image from a Dockerfile.
docker build -t my_image .

docker run: Creates and starts a new Docker container from an image.
docker run -d -p 8080:80 my_image

docker pull: Pulls a Docker image from a registry.
docker pull my_image

docker push: Pushes a Docker image to a registry.
docker push my_image

docker ps: Lists running containers.
docker ps

docker stop: Stops a running container.
docker stop container_id

docker rm: Removes a container.
docker rm container_id

docker rmi: Removes an image.
docker rmi image_id

These are some of the fundamental concepts and commands in Docker. Understanding them will enable you to efficiently build, manage, and deploy containerized applications using Docker.

Additional Attributes:

  • Volumes: Mount directories from the host machine into the container for persistent storage.
  • Networks: Connect containers to each other and the outside world.
  • Ports: Expose container ports to the host machine or other containers.

Remember, this is a basic overview. Docker offers a rich set of features and functionalities to explore and leverage in your development workflow.

Key Advantages of Docker:

  • Isolation: Applications run in isolated containers, preventing conflicts and ensuring consistent environments.
  • Portability: Docker containers can run on any system with Docker Engine, regardless of the underlying infrastructure.
  • Scalability: Easily scale applications by creating and managing multiple containers.
  • Version Control: Docker images allow versioning and rollback to previous application states.
  • Efficiency: Docker containers share the host OS kernel, making them lightweight and resource-efficient.

Learning More:

I hope this comprehensive overview provides a solid understanding of Docker’s core concepts. If you have any further questions, feel free to ask!

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

Data Analyst | 6 Sigma Master Black Belt | NLP | GenAI | Data Scientist | Ex-IBM | Ex-Accenture | Ex-Fujitsu. https://www.linkedin.com/in/ajay-verma-1982b97/