11:00 - 17:00

Mon - Fri

Article Page

Master Docker Like a Pro: The Ultimate CLI Cheat Sheet

Master Docker Like a Pro: The Ultimate CLI Cheat Sheet

by Arnab Posted on August 10, 2024 | 5 minutes read



Master Docker Like a Pro: The Ultimate CLI Cheat Sheet

The Ultimate Docker CLI Cheat Sheet

If you're diving into Docker and feeling overwhelmed by the plethora of commands, fret not! I've crafted the ultimate CLI cheat sheet to simplify your Docker experience. Whether you're building, running, or managing containers, this guide will keep you organized and efficient. Ready to transform your Docker workflow? Let's dive in!

Docker CLI Commands Simplified

Navigating Docker's command-line interface (CLI) can seem like deciphering a complex puzzle. But with this cheat sheet, you'll master Docker commands like a pro. From building and managing images to running and inspecting containers, I’ve got you covered. Here’s a comprehensive guide to streamline your Docker workflow.

1. Building Docker Images

Build an Image from a Dockerfile:

docker build -t <image_name> .

This command creates a Docker image using the Dockerfile in your current directory.

Build an Image Without Cache:

docker build -t <image_name> . --no-cache

Use this when you want a fresh build, ignoring any cached layers from previous builds.

2. Managing Docker Images

List Local Images:

docker images

This will list all images available on your local machine.

Delete an Image:

docker rmi <image_name>

This command removes a specified Docker image.

Remove All Unused Images:

docker image prune

Cleans up unused images to free up space.

3. Docker Hub and Container Management

Login into Docker Hub:

docker login -u <username>

Authenticate with Docker Hub to push and pull images.

Publish an Image to Docker Hub:

docker push <username>/<image_name>

Upload your image to Docker Hub for sharing.

Search Docker Hub for an Image:

docker search <image_name>

Find images on Docker Hub that match your search criteria.

Pull an Image from Docker Hub:

docker pull <image_name>

Download an image from Docker Hub to your local machine.

4. Working with Containers

Create and Run a Container with a Custom Name:

docker run --name <container_name> <image_name>

Instantiate a container from the specified image with a name you choose.

Run a Container with Port Mapping:

docker run -p <host_port>:<container_port> <image_name>

Map container ports to host ports for external access.

Run a Container in the Background:

docker run -d <image_name>

Start a container in detached mode.

Start or Stop an Existing Container:

docker start|stop <container_name> (or <container-id>)

Control the running state of a container.

Remove a Stopped Container:

docker rm <container_name>

Clean up stopped containers to maintain system hygiene.

Open a Shell Inside a Running Container:

docker exec -it <container_name> sh

Access the container’s shell for debugging or modifications.

Fetch and Follow Container Logs:

docker logs -f <container_name>

View real-time logs of a running container.

Inspect a Running Container:

docker inspect <container_name> (or <container_id>)

Retrieve detailed information about a container’s configuration.

List Currently Running Containers:

docker ps

View active containers on your system.

List All Containers (Running and Stopped):

docker ps --all

Display all containers, not just the running ones.

View Resource Usage Stats:

docker container stats

Monitor resource usage metrics for your containers.

5. General Docker Commands

Start Docker Daemon:

docker -d

Initiate Docker’s background service.

Get Help with Docker:

docker --help

Display help information for Docker commands.

Display System-Wide Information:

docker info

Provide detailed system information about Docker’s configuration and status.

FAQ: Your Docker Questions Answered

What is Docker?

Docker is a platform that allows you to build, ship, and run applications in containers. Containers package your app and its dependencies together, ensuring it runs consistently across different environments.

What is the difference between Docker images and containers?

An image is a blueprint for a container; it contains the application code and dependencies. A container is a running instance of an image.

How do I update a Docker image?

Update your Dockerfile and rebuild the image using docker build. If necessary, use docker image prune to remove old images.

Can I run multiple containers from the same image?

Yes, you can start multiple containers from the same image, each with its own configuration and data.

How can I access a container’s logs?

Use docker logs -f <container_name> to stream the logs of a running container.

What does the docker exec command do?

It allows you to run commands inside a running container, similar to opening a terminal within the container.

How can I see which ports are being used by a container?

Use docker ps to view port mappings for running containers.

How do I delete unused Docker images?

Run docker image prune to remove all unused images and free up space.

What should I do if a container isn’t starting?

Check the container logs with docker logs and inspect its configuration with docker inspect.

Where can I find more Docker tutorials and resources?

Visit the official Docker documentation or explore example projects on GitHub.

Mastering Docker CLI commands can drastically improve your development workflow. With this cheat sheet, you’re well on your way to becoming a Docker aficionado. Happy containerizing! 🚀


Leave a Comment:



Topics to Explore: