Docker Cheat-sheet for beginners
Docker Cheat-sheet for beginners

Docker Cheat-sheet for beginners

The Ultimate Docker Cheat Sheet. docker_cheatsheet. A cheatsheet is a concise summary of important information that is meant to be used as a quick reference.

Docker has become an essential tool for developers, especially when managing applications across different environments. Here’s a quick cheat-sheet to get started with Docker, explained in first-person and active voice. I’ll break down what each command does so that you’re always in control. Let’s dive right in!

Basic Docker Commands

Start Docker

  • Linuxsystemctl start docker
  • macOSopen -a Docker
  • Why: This command starts the Docker service on your machine, so you can run containers and images.

Check Docker Version
docker --version

  • Why: Displays the current Docker version installed on your machine. Handy for compatibility checks.

Working with Containers

List Running Containers
docker ps

  • Why: Shows a list of all containers that are currently running on your system.

List All Containers (Running + Stopped)
docker ps -a

  • Why: Lists both active and inactive (stopped) containers.

Run a Container
docker run <image_name>

  • Why: Downloads the image (if not available locally) and starts the container interactively.

Run in Detached Mode
docker run -d <image_name>

  • Why: Starts a container in the background (detached), freeing up your terminal for other commands.

Run with Port Mapping
docker run -p <host_port>:<container_port> <image_name>

  • Why: Maps the container’s port to your machine’s port so that you can access it through a browser or another client.

Stop a Running Container
docker stop <container_id>

  • Why: Gracefully stops a running container.

Start a Stopped Container
docker start <container_id>

  • Why: Restarts a previously stopped container without running a new one.

Remove a Stopped Container
docker rm <container_id>

  • Why: Deletes the stopped container to free up space.

Managing Docker Images

List Docker Images
docker images

  • Why: Displays all downloaded images on your system.

Pull an Image from Docker Hub
docker pull <image_name>

  • Why: Downloads an image from Docker Hub for local use.

Build an Image from a Dockerfile
docker build -t <image_name> .

  • Why: Creates a Docker image from a Dockerfile, allowing you to package your application with all dependencies.

Tag an Image
docker tag <image_id> <new_image_name>:<tag>

  • Why: Tags an image with a specific name and version (tag), useful for version control.

Remove an Image
docker rmi <image_id>

  • Why: Deletes a Docker image to free up disk space.

Container Management

View Logs of a Container
docker logs <container_id>

  • Why: Displays the logs of a running container to troubleshoot issues.

Access a Running Container (Interactive Shell)
docker exec -it <container_id> /bin/bash

  • Why: Opens an interactive shell inside a running container, allowing you to interact directly.

Copy Files from Container to Host
docker cp <container_id>:<path_inside_container> <host_path>

  • Why: Copies files from a container to your host machine.

Networking with Docker

List Networks
docker network ls

  • Why: Lists all available Docker networks.

Create a Network
docker network create <network_name>

  • Why: Creates a custom network for containers to connect to.

Connect a Running Container to a Network
docker network connect <network_name> <container_id>

  • Why: Connects an existing running container to a specified network.

Docker Compose

Start Services in Detached Mode
docker-compose up -d

  • Why: Starts services defined in your docker-compose.yml file in the background.

Stop Services
docker-compose down

  • Why: Stops and removes all services and containers created by Docker Compose.

Build and Start Containers
docker-compose up --build

  • Why: Builds the images and starts the containers defined in the docker-compose.yml.

Inspecting and Monitoring

Inspect Container Details
docker inspect <container_id>

  • Why: Displays detailed information about a container’s configuration and status.

Display Resource Usage (CPU, Memory)
docker stats

  • Why: Shows real-time CPU, memory, and network usage of running containers.

Working with Volumes

List Volumes
docker volume ls

  • Why: Lists all Docker volumes available on your system.

Create a Volume
docker volume create <volume_name>

  • Why: Creates a new volume to store persistent data.

Mount a Volume
docker run -v <volume_name>:<path_inside_container> <image_name>

  • Why: Mounts a volume to your container, ensuring data persists even after the container is stopped or removed.

That’s it! You now have a solid reference to start managing your Docker environment like a pro. Remember, practice makes perfect, so don’t hesitate to explore and experiment with these commands. Happy Dockering!

A Note From the Author

Thank you so much for taking the time to read the story. If you found my article helpful and interesting, please share your thoughts in the comment section, and don’t forget to share and clap

Let’s Get in Touch!

Show 2 Comments

2 Comments

  1. wonderful submit, very informative. I ponder why the opposite specialists of this sector do not realize this. You must proceed your writing. I’m sure, you’ve a great readers’ base already!

  2. I almost never comment, however i did some searching and
    wound up hedre Docker Cheat-sheet for beginners – 🌟Code
    with MrCoder7️⃣0️⃣1️⃣
    . And I actuallyy do have some questions foor you if you do not mind.
    Could it be only me or does it appear like a few of thnese comments
    appear as if they are left by brain dead people?
    😛 And, if you are writing on other online social sites, I would like to keep up with anything fresh you have to post.
    Would yoou make a liszt of the coomplete urls of your community pages like
    your linkedin profile, Facebook page or twitter feed? http://Boyarka-Inform.com/

Leave a Reply

Your email address will not be published. Required fields are marked *