Docker: Your Code's Ultimate Team Player!

Anushka SinghAnushka Singh
7 min read

Let’s take the example of a sports training facility that prepares athletes for different sports like soccer, basketball, and tennis. Docker in this analogy represents the idea of efficient, flexible, and consistent training environments for different sports within the same facility.

Before Docker: Separate Training Facilities (like Virtual Machines)

Imagine this sports training center used to have separate facilities for each sport — one for soccer, one for basketball, and one for tennis. Each facility has its own equipment, coaches, and space, and they don’t share anything with each other. For example:

  • The soccer facility has a full field, nets, cones, and soccer balls.

  • The basketball facility has its own courts, hoops, and balls.

  • The tennis facility has its own courts, rackets, and balls.

This setup is similar to virtual machines:

  • Each facility (VM) is isolated and includes all the necessary resources, ensuring no interference between sports.

  • However, this setup is costly, and many resources go underutilized. For example, the soccer field might be empty when it’s not practice time.

  • Expanding or changing the setup is difficult because each facility is fully independent.

Enter Docker: Shared Multi-Sport Training Facility (like Containers)

Now, let’s imagine they adopt a Docker-like approach, creating specialized “stations” within a single, shared training facility. Here’s what that looks like:

  • Instead of having separate facilities, the center has one large space that can adapt to different sports.

  • Each sport has its own designated training station (container) with only the specialized equipment needed for its drills.

    • The soccer station has cones, a few soccer balls, and portable goals.

    • The basketball station has hoops that can be moved or adjusted, a few balls, and some cones.

    • The tennis station has nets that can be put up or taken down, and a supply of balls.

  • The stations share common resources like lighting, restrooms, lockers, and seating areas — similar to how Docker containers share the host OS resources.

How Docker Makes a Difference

Docker brings this kind of flexibility and efficiency to software development. Just as this training center can set up and scale sports stations without building entire new facilities, Docker allows developers to set up, run, and scale applications without requiring full virtual machines.

Formal Definition

Are containers and virtual machines the same thing?

Here’s a simple breakdown of the differences between virtual machines (VMs) and containers:

Virtual Machines (VMs):

  • Isolation: VMs run an entire operating system (OS) on top of your computer's hardware. Each VM has its own OS, which means they are completely isolated from each other.

  • Resources: Because they include the full OS, VMs typically use more system resources (like CPU and memory). This can make them slower to start up and use more disk space.

  • Use Case: VMs are great for running different operating systems on the same physical machine or when you need complete isolation.

Containers:

  • Lightweight: Containers share the host OS and only include the necessary parts of the application and its dependencies. This makes them much smaller and quicker to start than VMs.

  • Efficiency: Containers use fewer resources since they don't require a full OS. They can run more instances on the same hardware compared to VMs.

  • Use Case: Containers are ideal for developing, testing, and deploying applications consistently across different environments.

So, what exactly is Docker, and why is there so much buzz surrounding it?

It is a container platform that allows you to build, test, and deploy applications quickly. A developer specifies the application and its dependencies in a Dockerfile, which is then used to create Docker images that define Docker containers.

DockerFile ? Images ?

Let’s take it one by one →

First, look at the installation process :)

Installation in VS Code

Setting up Docker Desktop

  1. Dockerfile Instructions

    • FROM: This initial instruction specifies the base image from which our container will be built.

    • WORKDIR: Sets the working directory path, where the application’s source code and other files will be placed.

    • RUN: Allows execution of commands, similar to how you would run them in the terminal.

    • COPY: Creates a copy of application files from your local machine to the container.

    • ENV: Sets environment variables within the container, such as an API key.

    • EXPOSE: Declares the port the application will listen to, making it accessible to external traffic.

    • CMD: Defines the main command that runs when the container starts (limited to one per container).

    • ENTRYPOINT: Sets an entry point, allowing arguments to be passed when executing the command.

    • HEALTHCHECK: Optionally adds a health check to monitor the container’s status and ensure it’s running as expected.

Sample Dockerfile

Here’s an example Dockerfile that uses each of these instructions:

    dockerfileCopy code# Set the base image
    FROM node:14

    # Set the working directory in the container
    WORKDIR /app

    # Install dependencies
    RUN apt-get update && apt-get install -y \
        curl \
        && rm -rf /var/lib/apt/lists/*

    # Copy application source code to the working directory
    COPY . /app

    # Set environment variables
    ENV API_KEY=your_api_key_here

    # Expose the port the app will run on
    EXPOSE 3000

    # Define the command to run when starting the container
    CMD ["node", "server.js"]

    # Optional: define an entry point for the container, allowing for additional arguments
    ENTRYPOINT ["node"]

    # Optional: add a health check to verify the container is running properly
    HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
        CMD curl -f http://localhost:3000 || exit 1
  1. Images

    When you install Docker Desktop, you also get access to the Docker CLI. Using the docker build command in the terminal, you can convert a Dockerfile into an image.

    But what exactly is an image?

    A Docker image acts as a blueprint or template for containers, specifying everything needed to set up and run an application, including dependencies and configurations.

Some key Docker commands for running images and managing containers:

1. Run a Docker Image as a Container

cssCopy codedocker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • This command creates and runs a new container from the specified image.

  • Example: docker run -d -p 80:80 nginx

    • -d runs the container in detached mode (in the background).

    • -p maps port 80 of the container to port 80 on the host.

    • nginx specifies the image to use (in this case, the official Nginx image).

2. List Running Containers

Copy codedocker ps
  • Shows all currently running containers along with their container ID, name, status, ports, and more.

  • To see all containers, including stopped ones, use:

      cssCopy codedocker ps -a
    

3. Start a Stopped Container

cssCopy codedocker start [CONTAINER_ID or CONTAINER_NAME]
  • Starts a container that has been created but is not currently running.

4. Stop a Running Container

arduinoCopy codedocker stop [CONTAINER_ID or CONTAINER_NAME]
  • Stops a running container.

5. Restart a Container

cssCopy codedocker restart [CONTAINER_ID or CONTAINER_NAME]
  • Restarts a container, useful for applying updates or changes.

6. Run a Command in a Running Container

bashCopy codedocker exec [OPTIONS] CONTAINER COMMAND
  • Runs a command within an already running container.

  • Example: docker exec -it my_container /bin/bash

    • -i keeps STDIN open, and -t allocates a pseudo-TTY, allowing interaction with the container’s shell.

7. Remove a Container

bashCopy codedocker rm [CONTAINER_ID or CONTAINER_NAME]
  • Deletes a stopped container. Use docker rm -f to force-remove a running container.

8. Remove All Stopped Containers

Copy codedocker container prune
  • Removes all containers that are not running to free up resources.

9. Run a Container with a Specific Name

cssCopy codedocker run --name [CONTAINER_NAME] IMAGE
  • Assigns a specific name to the container for easier identification.

10. View Logs of a Running Container

cssCopy codedocker logs [CONTAINER_ID or CONTAINER_NAME]
  • Shows logs generated by the container’s application.

Example Workflow for Running and Managing a Container

bashCopy code# Step 1: Pull an image from Docker Hub
docker pull nginx

# Step 2: Run the image as a container with a custom name and port mapping
docker run -d --name my_nginx -p 8080:80 nginx

# Step 3: List running containers to verify
docker ps

# Step 4: View logs of the running container
docker logs my_nginx

# Step 5: Stop the container
docker stop my_nginx

# Step 6: Remove the container when it's no longer needed
docker rm my_nginx

Thank you for sticking with me this far! I hope you gained valuable insights into Docker and its architecture. Understanding how Docker streamlines application deployment and management can greatly enhance your development workflow <3

2
Subscribe to my newsletter

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

Written by

Anushka Singh
Anushka Singh

👋 Hi, I’m Anushka, a final-year IT student at Bhagwan Parshuram Institute of Technology, Delhi. Currently interning at DFY Graviti, I'm enhancing my skills in GIS, Docker, and Linux. I specialize in full-stack web development with React and Node.js, and I'm certified in Product Management and UI/UX Design, committed to continuous learning and crafting impactful digital solutions.