The Educative

Core Docker Commands and Concepts: A Quick Reference Cheat Sheet
Image by www.freepik.com

Core Docker Commands and Concepts: A Quick Reference Cheat Sheet

This quick reference guide provides an overview of core Docker commands, concepts, and Dockerfile instructions for managing containers. It’s perfect for developers and DevOps teams looking to simplify their Docker workflows.

03-Nov-2024
ByThe Educative
DockerContainerizationDocker CommandsDockerfileDocker ConceptsContainer Management

Mastering Docker: Essential Commands, Core Concepts, and Key Dockerfile Instructions for Container Management

Docker has revolutionized software deployment, making it easier than ever to package applications and their dependencies into containers, ensuring consistent behavior across various environments. This guide provides a comprehensive overview of essential Docker commands, concepts, and Dockerfile instructions. Whether you’re a newcomer to containerization or an experienced developer, this cheat sheet offers a quick reference to streamline your Docker workflow.


Understanding Docker Concepts

Before diving into commands, it’s crucial to understand the foundational concepts in Docker:

1. Container

  • A lightweight, standalone, executable software package that includes everything needed to run an application.

  • Containers isolate the software from the environment, enabling it to run uniformly across different systems.

2. Image

  • A read-only template with instructions to create containers. Images contain the application code, libraries, dependencies, and runtime.

  • Docker images are often built using Dockerfiles and stored in registries for easy sharing and reuse.

3. Dockerfile

  • A script with a set of instructions for building a Docker image. It specifies the base image, dependencies, and commands for configuring the application within the container.

4. Registry

  • A repository where Docker images are stored and shared. The most popular public registry is Docker Hub, but you can also create private registries.

5. Volume

  • A storage mechanism for persisting data generated by and used in Docker containers, even after the container is removed.


Core Docker Commands

Here are some essential Docker commands to get you started with container management:

1. Starting with Images

  • Pull an image from a registry

    docker pull <image-name>:<tag>
    

    Downloads an image from Docker Hub or another registry. If no tag is specified, it defaults to latest.

  • List all images

    docker images
    

    Shows all downloaded images.

  • Remove an image

    docker rmi <image-id>
    

    Deletes an image by its ID. Add -f to force removal if needed.

2. Working with Containers

  • Run a container

    docker run <image-name>
    

    Runs a container from an image. Add -it to run in interactive mode and --name <name> to assign a custom name.

  • List all running containers

    docker ps
    

    Displays all currently running containers.

  • Stop a running container

    docker stop <container-id>
    

    Stops the container with the specified ID.

  • Remove a container

    docker rm <container-id>
    

    Deletes a stopped container. Add -f to force-remove a running container.

  • Execute a command inside a running container

    docker exec -it <container-id> <command>
    

    Useful for accessing a shell inside a running container (e.g., docker exec -it <container-id> /bin/bash).

  • Check container logs

    docker logs <container-id>
    

    Outputs the logs from a specific container, helping with debugging.

3. Building and Managing Images with Dockerfiles

  • Build an image from a Dockerfile

    docker build -t <image-name>:<tag> <path>
    

    Creates a Docker image based on the Dockerfile located at <path>. The -t flag tags the image with a name and tag.

  • Push an image to a registry

    docker push <image-name>:<tag>
    

    Uploads the specified image to a registry, such as Docker Hub.

4. Networking and Volumes

  • List all networks

    docker network ls
    

    Lists all Docker networks, including default ones (bridge, host, and none).

  • Create a new network

    docker network create <network-name>
    

    Creates a custom network, which is often useful for linking containers.

  • Inspect a container’s details (including network)

    docker inspect <container-id>
    

    Provides detailed configuration and state information about a container.

  • Creating a volume

    docker volume create <volume-name>
    

    Establishes a new Docker volume.

  • List all volumes

    docker volume ls
    

    Shows all available Docker volumes.


Essential Dockerfile Instructions

A Dockerfile is the blueprint for creating Docker images, containing a sequence of instructions Docker reads to assemble an image. Here are some of the key instructions:

1. FROM

FROM <base-image>

Specifies the base image to use. It’s usually the first line in any Dockerfile.

2. RUN

RUN <command>

Executes commands during the image build process, often used for installing packages.

3. COPY

COPY <source> <destination>

Copies files or directories from the host system into the image filesystem.

4. WORKDIR

WORKDIR <path>

Sets the working directory for subsequent commands, simplifying relative paths.

5. CMD

CMD ["executable", "param1", "param2"]

Specifies the default command to run when a container starts. Only one CMD instruction is allowed; if multiple are present, only the last one runs.

6. EXPOSE

EXPOSE <port>

Defines the port on which the container will listen for network connections, though it doesn’t actually publish the port.

7. ENTRYPOINT

ENTRYPOINT ["executable", "param1", "param2"]

Sets the main command for the container. Unlike CMD, it cannot be easily overridden. It’s often used alongside CMD to specify default arguments.


Docker Workflow Example: Building and Running a Node.js Application

Let’s walk through a simple example of building and running a Node.js application in Docker.

  1. Create a Dockerfile for the Node.js application:

    # Use the official Node.js image
    FROM node:14
    
    # Set the working directory
    WORKDIR /app
    
    # Copy package.json and install dependencies
    COPY package.json .
    RUN npm install
    
    # Copy the rest of the application code
    COPY . .
    
    # Expose the port the app runs on
    EXPOSE 3000
    
    # Start the app
    CMD ["npm", "start"]
    
  2. Build the Docker image:

    docker build -t my-node-app .
    
  3. Run the Docker container:

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

This example shows the basic Docker workflow for creating an image, building it from a Dockerfile, and running a containerized application.


Conclusion

Mastering Docker commands, concepts, and Dockerfile instructions empowers you to manage containerized applications with ease. This cheat sheet covers essential commands and foundational Dockerfile instructions to help you get started or level up your Docker skills.

Comments

No comments yet. Be the first to share your thoughts!

You may also like

Interactive Learning for Developers: A Hands-On Approach

Interactive Learning for Developers: A Hands-On Approach

Tired of traditional video-based courses? Explore Educative's interactive learning platform and dive into coding with hands-on exercises and practical examples.

The Educative
2024-09-13
Deep Dive into Open Source Software Design

Deep Dive into Open Source Software Design

Learn about the architecture, design decisions, and implementation strategies behind successful open-source projects. A valuable resource for developers.

The Educative
2025-01-09
Understanding the Internet of Things (IoT): Architecture, Smart Objects, Protocols, Benefits, and Sensors

Understanding the Internet of Things (IoT): Architecture, Smart Objects, Protocols, Benefits, and Sensors

Discover the core components of the Internet of Things (IoT) architecture, from smart objects to communication protocols. This guide also covers the advantages, challenges, and key sensors powering IoT applications in fields like healthcare, industry, and smart homes.

Saikat Roy
2024-11-03