Docker Explained in 5 Mins

Docker Explained in 5 Mins

Before Docker

Before Docker, software applications were typically deployed using traditional virtualization technologies or running directly on physical servers. These methods had several limitations and challenges:

  1. Full Machine Virtualization: In traditional virtualization, each application was deployed within a separate virtual machine (VM). This approach required significant resources, as each VM needed its own operating system (OS) installation, libraries, and dependencies. This led to higher overhead in terms of disk space, memory, and CPU usage.

  2. Dependency Issues: Applications often relied on specific versions of libraries, frameworks, or other dependencies. However, different applications might require different versions, leading to conflicts and compatibility issues. Maintaining multiple environments with different dependencies was cumbersome and time-consuming.

Docker, introduced in 2013, revolutionized application deployment by addressing these challenges. It introduced containerization, a lightweight and isolated packaging mechanism for applications and their dependencies.

What is Docker?

Docker is an open-source containerization platform that enables applications to be packaged into lightweight, portable containers. Containers provide an isolated and consistent environment for applications to run across different systems and platforms. Docker simplifies software deployment by eliminating dependency conflicts and ensuring reproducibility. It improves resource utilization, scalability, and collaboration between teams. With Docker, applications can be easily packaged, distributed, and run in a secure and efficient manner, revolutionizing the way software is developed, deployed, and managed.

Installation:

The process of installing Docker may vary slightly depending on the operating system (OS) you are using. Here are brief instructions for installing Docker on different OS:

  1. Linux: Docker has official installation guides for various Linux distributions. You can find detailed instructions on the Docker website. In general, the installation involves adding the Docker repository, updating the package manager, and installing Docker using the package manager. For example, on Ubuntu, you would typically run commands like: sudo apt-get update

    sudo apt-get install docker-ce docker-ce-cli containerd.io

  2. macOS: Docker provides a native application called Docker Desktop for macOS. You can download the installer from the Docker website and follow the installation wizard. Docker Desktop includes all the necessary components, including the Docker Engine, container runtime, and a user-friendly interface to manage Docker.

  3. Windows: Similar to macOS, Docker provides Docker Desktop for Windows. You can download the installer from the Docker website and follow the installation wizard. Docker Desktop includes the Docker Engine, container runtime, and a graphical interface for managing Docker. Note that Windows 10 Pro, Enterprise, or Education editions are required for running Docker Desktop on Windows.

Docker Cheat Sheet

Docker cheat sheet with some commonly used commands and concepts:

Container Management:

docker run IMAGE_NAME: Create and start a new container from an image.

docker start CONTAINER_ID: Start a stopped container.

docker restart CONTAINER_ID: Restart a container.

docker rm CONTAINER_ID: Remove a container.

docker ps: List running containers.

docker ps -a: List all containers (including stopped ones).

Image Management:

docker images: List available images.

docker pull IMAGE_NAME: Download an image from a registry.

docker rmi IMAGE_NAME: Remove an image.

docker build -t IMAGE_NAME PATH_TO_DOCKERFILE: Build a Docker image from a Dockerfile.

Registry & Repository:

docker login: Log in to a Docker registry.

docker logout: Log out from a Docker registry.

docker push IMAGE_NAME: Push an image to a registry.

docker pull IMAGE_NAME: Pull an image from a registry.

Volumes:

docker volume create VOLUME_NAME: Create a new volume.

docker volume ls: List available volumes.

docker volume rm VOLUME_NAME: Remove a volume.

docker run -v HOST_PATH:CONTAINER_PATH IMAGE_NAME: Mount a host directory or volume to a container.

Networks:

docker network create NETWORK_NAME: Create a new network.

docker network ls: List available networks.

docker network rm NETWORK_NAME: Remove a network.

docker run --network=NETWORK_NAME IMAGE_NAME: Attach a container to a network.

Docker Compose:

docker-compose up: Create and start containers defined in a Docker Compose file.

docker-compose down: Stop and remove containers defined in a Docker Compose file.

docker-compose build: Build images defined in a Docker Compose file.

docker-compose logs: View logs of containers defined in a Docker Compose file.

Creating a Docker Image

To create a Docker image, you need to define a Dockerfile that contains instructions for building the image. Here's a general outline of the steps involved:

  1. Create a new directory for your Docker image and navigate to it: mkdir my-docker-image

    cd my-docker-image

  2. Create a Dockerfile in the directory using a text editor. The Dockerfile defines the steps for building the image. Here's an example Dockerfile for a simple Node.js application:

  3. # Use an official Node.js runtime as the base image

    FROM node:14

    # Set the working directory inside the container

    WORKDIR /app

    # Copy package.json and package-lock.json to the working directory

    COPY package*.json ./

    # Install dependencies

    RUN npm install

    # Copy the application code to the container

    COPY . .

    # Specify the command to run when the container starts

    CMD ["npm", "start"]

  4. Save the Dockerfile and build the Docker image using the docker build command. Provide a name and tag for the image using the -t option:

    docker build -t my-docker-image:1.0 . The . at the end indicates the current directory as the build context.

#WeMakeDevs

Did you find this article valuable?

Support Pravesh's blog by becoming a sponsor. Any amount is appreciated!