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.
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.
Before diving into commands, it’s crucial to understand the foundational concepts in Docker:
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.
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.
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.
A repository where Docker images are stored and shared. The most popular public registry is Docker Hub, but you can also create private registries.
A storage mechanism for persisting data generated by and used in Docker containers, even after the container is removed.
Here are some essential Docker commands to get you started with container management:
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.
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.
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.
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.
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:
FROM <base-image>
Specifies the base image to use. It’s usually the first line in any Dockerfile.
RUN <command>
Executes commands during the image build process, often used for installing packages.
COPY <source> <destination>
Copies files or directories from the host system into the image filesystem.
WORKDIR <path>
Sets the working directory for subsequent commands, simplifying relative paths.
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.
EXPOSE <port>
Defines the port on which the container will listen for network connections, though it doesn’t actually publish the port.
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.
Let’s walk through a simple example of building and running a Node.js application in Docker.
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"]
Build the Docker image:
docker build -t my-node-app .
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.
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.
No comments yet. Be the first to share your thoughts!
Tired of traditional video-based courses? Explore Educative's interactive learning platform and dive into coding with hands-on exercises and practical examples.
Learn about the architecture, design decisions, and implementation strategies behind successful open-source projects. A valuable resource for developers.
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.