The Ultimate Docker Cheat Sheet for Developers

Docker

Discover the ultimate Docker cheat sheet for developers. Learn essential Docker commands, best practices, and tips for optimizing Docker usage. This comprehensive guide covers everything from basic commands to advanced techniques, Docker Compose, and security best practices to streamline your development workflow.


NGNishan Giri
2024-06-10
The Ultimate Docker Cheat Sheet for Developers

Table of Contents

Introduction

In the ever-evolving world of web development, Docker has become an indispensable tool for developers. Whether you're working on a small project or a large-scale application, Docker simplifies the process of creating, deploying, and managing applications using containerization. This cheat sheet provides a comprehensive guide to essential Docker commands and tips, making it easier for you to work efficiently with Docker.

What is Docker?

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications. It allows developers to package applications and their dependencies into containers—lightweight, portable units that can run consistently across different environments.

Advantages of Docker:

Common use cases for Docker in web development include creating isolated development environments, running microservices, and simplifying CI/CD pipelines.

Basic Docker Commands


Check Docker Version:

docker --version

Displays the installed Docker version.


Pull an Image from Docker Hub:

docker pull [image]

Fetches an image from Docker Hub.


Run a Container:

docker run [image]

Creates and starts a container from the specified image.


List Running Containers:

docker ps

Displays a list of all running containers.


Stop a Running Container:

docker stop [container]

Stops the specified container.


Remove a Container:

docker rm [container]

Deletes the specified container.


Working with Docker Images

Build an Image:

docker build -t [name] .

Builds an image from a Dockerfile in the current directory.


List Local Images:

docker images

Displays all images stored locally.


Remove an Image:

docker rmi [image]

Deletes the specified image.


Example Dockerfile:

# Use an official Node.js runtime as a parent image
FROM node:14
 
# Set the working directory in the container
WORKDIR /app
 
# Copy the current directory contents into the container at /app
COPY . /app
 
# Install any needed packages specified in package.json
RUN npm install
 
# Make port 8080 available to the world outside this container
EXPOSE 8080
 
# Run app.js when the container launches
CMD ["node", "app.js"]

Managing Docker Containers

Start a Stopped Container:

docker start [container]

Starts a container that was previously stopped.


Execute a Command in a Running Container:

docker exec -it [container] /bin/bash

Opens a bash shell inside the running container.


View Container Logs:

docker logs [container]

Displays the logs of the specified container.


Inspect Container Details:

docker inspect [container]

Shows detailed information about the specified container.


Docker Networks and Volumes

List Networks:

docker network ls

Displays all Docker networks.


Create a Network:

docker network create [network_name]

Creates a new Docker network.


List Volumes:

docker volume ls

Displays all Docker volumes.


Create a Volume:

docker volume create [volume_name]

Creates a new Docker volume.


Example of Using Volumes:

docker run -d -v myvolume:/app/data myimage

Mounts the myvolume volume to the /app/data directory in the container.


Docker Compose Basics

Introduction to Docker Compose:

Docker Compose is a tool for defining and running multi-container Docker applications. With a simple docker-compose.yml file, you can configure your application’s services, networks, and volumes.


Example docker-compose.yml File:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  app:
    image: node:14
    volumes:
      - .:/app
    command: npm start
    depends_on:
      - web

Common Docker Compose Commands:

Start Services:

docker-compose up

Starts all services defined in the docker-compose.yml file.


Stop Services:

docker-compose down

Stops all running services and removes containers, networks, volumes, and images created by up.


View Logs:

docker-compose logs

Displays logs from all services.


Advanced Docker Commands

Pause and Unpause a Container:

docker pause [container]
docker unpause [container]

Pauses and unpauses a running container respectively.


Commit Changes to a New Image:

docker commit -m "commit message" -a "author" [container] [username/image_name:tag]

Saves the changes in a container to a new image.


Cleanup Unused Resources:

docker system prune

Removes all stopped containers, unused networks, dangling images, and build cache.


Tips and Tricks for Efficient Docker Usage

Optimize Dockerfile for Smaller Image Sizes:

Using .dockerignore File:*

Similar to .gitignore, a .dockerignore file can be used to exclude files and directories from being copied into the Docker image, reducing build context size and speeding up the build process.

Example .dockerignore File:

node_modules
.git
Dockerfile
README.md

Docker Hub vs. Private Registries:

Docker Hub: Publicly accessible, easy to use, and integrates with Docker CLI. However, it's not ideal for private images.

Private Registries: Provide better security and control over your images. Consider using solutions like Docker Trusted Registry or third-party services like AWS ECR or Azure Container Registry.

Security Best Practices:

Conclusion

Docker has revolutionized the way we develop, ship, and run applications. By mastering the essential Docker commands and best practices outlined in this cheat sheet, you can streamline your development workflow and boost productivity. Bookmark this page and refer to it whenever you need a quick Docker reference. Don't forget to share your own Docker tips and tricks in the comments below!

Frequently Asked Questions (FAQs)

1. What is Docker and why is it used?

Docker is a platform designed to simplify the creation, deployment, and running of applications using containers. Containers allow developers to package an application with all its dependencies and run it in any environment, ensuring consistency across different stages of development and deployment.

2. How do I install Docker on my system?

To install Docker, visit the Docker website and download Docker Desktop for your operating system (Windows, Mac, or Linux). Follow the installation instructions provided for your specific OS.

3. What are the basic Docker commands every developer should know?

Some of the basic Docker commands include:

4. How do I create a Dockerfile?

A Dockerfile is a text file that contains instructions on how to build a Docker image. Here’s a simple example:

# Use an official Node.js runtime as a parent image
FROM node:14
 
# Set the working directory in the container
WORKDIR /app
 
# Copy the current directory contents into the container at /app
COPY . /app
 
# Install any needed packages specified in package.json
RUN npm install
 
# Make port 8080 available to the world outside this container
EXPOSE 8080
 
# Run app.js when the container launches
CMD ["node", "app.js"]

To build an image from this Dockerfile, use the command:

docker build -t my-node-app .

5. How do I clean up unused Docker resources?

To free up space and keep your system clean, you can use the following commands:

Happy Coding! 😊