G: Are you ready to truly master Docker? Whether you’re just starting your containerization journey or looking to deepen your practical skills for real-world scenarios, this guide is for you! Docker has revolutionized how we develop, ship, and run applications, making it an indispensable tool for every modern developer and DevOps engineer.
This post will walk you through the most essential Docker commands, from setting up your first container to managing complex multi-service applications and cleaning up your system. Get ready to transform your workflow! π³
Table of Contents
- Why Docker? A Quick Overview
- Getting Started: The Absolute Basics
- Installation & Verification
- Understanding Docker’s Core Components
- Image Management: Your Application Blueprints
docker pull
: Downloading Imagesdocker images
: Listing Imagesdocker build
: Creating Custom Imagesdocker tag
: Tagging Imagesdocker rmi
: Removing Imagesdocker push
: Sharing Your Images
- Container Management: Running Your Applications
docker run
: The Core Command!docker ps
: Listing Running Containersdocker stop
,start
,restart
: Managing Container Lifecycledocker exec
: Interacting Inside Containersdocker logs
: Viewing Container Outputdocker rm
: Removing Containersdocker attach
: Attaching to Container Processes
- Data Management: Persisting Your Data (Volumes)
docker volume create
: Creating Volumesdocker volume ls
: Listing Volumesdocker volume inspect
: Inspecting Volumesdocker volume rm
: Removing Volumes- Understanding Bind Mounts vs. Named Volumes
- Networking: Connecting Your Services
docker network ls
: Listing Networksdocker network create
: Creating Custom Networksdocker network inspect
: Inspecting Networksdocker network connect
/disconnect
: Attaching Containers
- Multi-Container Applications with Docker Compose
- Why Docker Compose?
docker compose up
: Starting Servicesdocker compose down
: Stopping Servicesdocker compose ps
,logs
,exec
: Managing Services
- Cleaning Up: Keeping Your System Tidy
docker system prune
: The All-in-One Cleanup- Targeted Removal Commands
- Advanced Tips & Troubleshooting
docker inspect
: Deep Dive into Docker Objectsdocker stats
: Monitoring Container Resources- Getting Help (
--help
) - Troubleshooting Common Issues
1. Why Docker? A Quick Overview π‘
Docker solves the “it works on my machine” problem. It packages your application and all its dependencies into a standardized unit called a container. This ensures that your application runs consistently across different environments β from your local machine to a testing server, and all the way to production.
Key Benefits:
- Consistency: Predictable environments for development, testing, and production.
- Isolation: Applications run in isolated containers, preventing conflicts.
- Portability: Containers can be easily moved and run anywhere Docker is installed.
- Efficiency: Lightweight and fast startup times compared to traditional VMs.
- Scalability: Easier to scale applications by spinning up more containers.
2. Getting Started: The Absolute Basics β¨
Before we dive into commands, make sure Docker is installed on your system. You can find official installation guides for Windows, macOS, and Linux on the Docker official website.
Installation & Verification
Once installed, open your terminal or command prompt and run:
docker --version
You should see something like: Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1
(or a similar version). If you see this, you’re good to go! π
Understanding Docker’s Core Components
At its heart, Docker operates with two main components:
- Docker Daemon (Server): The background service that manages Docker objects like images, containers, networks, and volumes.
- Docker Client (CLI): The command-line interface you use to interact with the Docker Daemon. When you type
docker run
, the client sends this command to the daemon.
3. Image Management: Your Application Blueprints ποΈ
Think of a Docker Image as a read-only template that contains an application and all its dependencies, libraries, and configuration files. It’s like a blueprint for creating containers.
docker pull
: Downloading Images
This command downloads an image from a Docker registry (like Docker Hub) to your local machine.
- Syntax: `docker pull
: ` (If tag is omitted, `latest` is assumed) - Example: Download the latest Ubuntu image.
docker pull ubuntu:latest
Output might look like:
Using default tag: latest latest: Pulling from library/ubuntu ... Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
- Pro Tip: Always specify a tag (e.g.,
ubuntu:22.04
) for reproducibility, especially in production environments! π·οΈ
docker images
: Listing Images
To see all the images you have downloaded or built locally:
- Syntax:
docker images
ordocker image ls
- Example:
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest f7ae619323de 3 weeks ago 77.8MB nginx latest 0ad3f0e0c1f6 2 months ago 187MB
docker build
: Creating Custom Images
This is where you start customizing! docker build
creates a new image from a Dockerfile
. A Dockerfile
is a text file that contains instructions for building a Docker image.
- Syntax: `docker build -t
: ` -
Example: Let’s create a simple web app image.
First, create a
Dockerfile
andindex.html
in a new directory (e.g.,my-web-app
):my-web-app/Dockerfile
:# Use an official Node.js runtime as a parent image FROM node:alpine # Set the working directory in the container WORKDIR /app # Copy package.json and package-lock.json (if available) COPY package*.json ./ # Install any dependencies RUN npm install # Copy the rest of your application code COPY . . # Expose port 3000 EXPOSE 3000 # Define the command to run your app CMD ["node", "app.js"]
my-web-app/app.js
:const http = require('http'); const hostname = '0.0.0.0'; // Listen on all network interfaces const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello from Docker! π³\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
Now, build the image from the
my-web-app
directory:cd my-web-app docker build -t my-custom-web-app:1.0 . # The '.' means the Dockerfile is in the current directory
Output will show the build process, layer by layer. On success, you’ll see something like:
Successfully built <IMAGE_ID> Successfully tagged my-custom-web-app:1.0
Now, run
docker images
and you’ll see your new image! β¨
docker tag
: Tagging Images
This command creates an additional tag for an existing image. Useful for versioning or preparing an image for pushing to a different registry.
- Syntax: `docker tag
: : ` - Example: Tag our custom web app for a “production” release.
docker tag my-custom-web-app:1.0 my-custom-web-app:prod docker images # You'll see two tags for the same IMAGE ID
docker rmi
: Removing Images
Cleans up unused images to free up disk space. Be careful, you can’t remove an image if a container is currently using it!
- Syntax: `docker rmi
: ` or `docker rmi ` - Example:
docker rmi my-custom-web-app:prod # Removes the 'prod' tag reference docker rmi my-custom-web-app:1.0 # Removes the '1.0' tag reference and the image if no other tags exist docker rmi ubuntu # Removes the latest ubuntu image if not in use
- Force Removal: `docker rmi -f
` will force removal even if containers are based on it (but not running). Use with caution! β οΈ
docker push
: Sharing Your Images
Once you’ve built a custom image, you can push it to a Docker registry (like Docker Hub, GitLab Registry, AWS ECR, etc.) so others can pull and use it. You’ll need to docker login
first.
- Syntax: `docker push
/ : ` - Example: (Assuming you logged in to Docker Hub as
your_username
)docker tag my-custom-web-app:1.0 your_username/my-custom-web-app:1.0 docker push your_username/my-custom-web-app:1.0
This makes your image publicly (or privately, depending on your repo settings) available. π
4. Container Management: Running Your Applications πββοΈ
A Docker Container is a runnable instance of a Docker image. You can create, start, stop, move, or delete a container using the Docker CLI. Think of it as a lightweight, isolated process running your application.
docker run
: The Core Command! π―
This is arguably the most frequently used command. It creates and starts a new container from a specified image.
- Syntax: `docker run [OPTIONS]
: [COMMAND] [ARG…]` -
Key Options:
-d
(detached mode): Runs the container in the background. Your terminal won’t be blocked.- Example:
docker run -d nginx
(runs Nginx in the background)
- Example:
- `-p
: ` (port mapping): Maps a port on your host machine to a port inside the container. Essential for accessing web apps. * **Example:** `docker run -d -p 8080:80 nginx` (access Nginx at `http://localhost:8080` on your host) - `–name
`: Assigns a custom name to your container. Makes it easier to refer to later. * **Example:** `docker run -d -p 8080:80 –name my-nginx-app nginx` - `-v
: ` (volume mount): Mounts a file or directory from your host machine into the container. Crucial for persisting data or sharing code. * **Example:** `docker run -d -p 8080:80 -v /my/html:/usr/share/nginx/html –name my-nginx-volume nginx` (Nginx serves files from your local `/my/html` folder) --rm
: Automatically removes the container when it exits. Great for temporary or test runs.- Example:
docker run --rm ubuntu echo "Hello from a temporary container!"
- Example:
-it
(interactive TTY): Allows you to interact with the container via your terminal (e.g., run shell commands inside it). Often used for debugging or one-off tasks.- Example:
docker run -it ubuntu bash
(opens a bash shell inside an Ubuntu container)
- Example:
- `–network
`: Connects the container to a specific Docker network (more on this later). - `–env
= ` or `-e = `: Sets environment variables inside the container. * **Example:** `docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysecret mysql`
docker ps
: Listing Running Containers
Shows you a list of all currently running containers.
- Syntax:
docker ps
ordocker container ls
- Example:
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3e4b5c6d7e8 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my-nginx-app
- All Containers (including stopped):
docker ps -a
ordocker ps --all
- Just IDs:
docker ps -aq
(quiet mode, only shows IDs)
docker stop
, start
, restart
: Managing Container Lifecycle
These commands control the state of your containers.
- **`docker stop
`:** Gracefully stops a running container. * **Example:** `docker stop my-nginx-app` - **`docker start
`:** Starts a stopped container. * **Example:** `docker start my-nginx-app` - **`docker restart
`:** Stops and then starts a container. * **Example:** `docker restart my-nginx-app`
docker exec
: Interacting Inside Containers π£οΈ
Runs a command inside a running container. Extremely useful for debugging or managing processes within a container.
- Syntax: `docker exec [OPTIONS]
[ARG…]` - Example: Get a bash shell inside our Nginx container:
docker exec -it my-nginx-app bash
Now you’re inside the container’s shell! You can explore files, install packages (if the image allows), etc. Type
exit
to leave. - Example: Run a simple command:
docker exec my-nginx-app ls /etc/nginx/
docker logs
: Viewing Container Output π
Retrieves the logs (stdout/stderr) from a container. Indispensable for debugging!
- Syntax: `docker logs [OPTIONS]
` - Key Options:
-f
(follow): Streams new logs as they appear (liketail -f
).- `–tail
`: Shows only the last N lines of logs. - `–since
`: Shows logs since a specific time.
- Example:
docker logs my-nginx-app docker logs -f my-nginx-app # Follow live logs docker logs --tail 10 my-nginx-app # Show last 10 lines
docker rm
: Removing Containers
Deletes one or more stopped containers.
- Syntax: `docker rm [OPTIONS]
[container_id_or_name…]` - Example:
docker rm my-nginx-app
- Force Removal: `docker rm -f
` will force removal even if the container is running. Use with caution! - Remove All Stopped Containers:
docker container prune
(ordocker rm $(docker ps -aq)
) is a common pattern to clean up.
docker attach
: Attaching to Container Processes
Attaches your terminal’s standard input, output, and error streams to a running container’s main process. Unlike docker exec
, attach
connects to the primary process of the container. If that process exits, your terminal will detach.
- Syntax: `docker attach
` - Example: If you ran
docker run -it ubuntu bash
and then detached (Ctrl+P, Ctrl+Q), you can re-attach:docker attach <ubuntu_container_id>
5. Data Management: Persisting Your Data (Volumes) πΎ
By default, data inside a container is ephemeral β it’s lost when the container is removed. To store data persistently, you use Volumes. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
docker volume create
: Creating Volumes
Creates a named volume that Docker manages.
- Syntax: `docker volume create
` - Example:
docker volume create my-app-data
docker volume ls
: Listing Volumes
Shows all named volumes on your system.
- Syntax:
docker volume ls
- Example:
docker volume ls
Output:
DRIVER VOLUME NAME local my-app-data
docker volume inspect
: Inspecting Volumes
Provides detailed information about a specific volume, including its mount point on the host.
- Syntax: `docker volume inspect
` - Example:
docker volume inspect my-app-data
docker volume rm
: Removing Volumes
Deletes one or more named volumes. Data within the volume will be lost!
- Syntax: `docker volume rm
` - Example:
docker volume rm my-app-data
- Remove All Unused Volumes:
docker volume prune
Understanding Bind Mounts vs. Named Volumes
- Named Volumes (Preferred): Managed by Docker. Best for persisting database files or application data. Created with
docker volume create
, then used with-v my-volume:/path/in/container
. - Bind Mounts: You directly mount a file or directory from the host filesystem into the container. Excellent for local development (e.g., mounting source code for hot-reloading) or sharing configuration files. Used with
-v /host/path:/container/path
.
6. Networking: Connecting Your Services π
Containers need to communicate with each other and the outside world. Docker provides various networking options. The default is bridge
network, where containers on the same bridge can communicate by IP, but custom networks offer better service discovery (by container name).
docker network ls
: Listing Networks
Shows all Docker networks available on your system.
- Syntax:
docker network ls
- Example:
docker network ls
Output:
NETWORK ID NAME DRIVER SCOPE ... bridge bridge bridge local host host host local none none null local
docker network create
: Creating Custom Networks
Creates a user-defined bridge network. Containers on this network can communicate with each other using their container names as hostnames, which is super convenient for multi-service applications.
- Syntax: `docker network create
` - Example:
docker network create my-app-network
docker network inspect
: Inspecting Networks
Provides detailed information about a network, including which containers are connected to it.
- Syntax: `docker network inspect
` - Example:
docker network inspect my-app-network
docker network connect
/disconnect
: Attaching Containers
Connects or disconnects a running container to/from a network.
- Syntax:
- `docker network connect
` - `docker network disconnect
`
- `docker network connect
-
Example: Run a database and an application container, then connect them.
docker run -d --name my-db mongo docker run -d --name my-app my-custom-web-app:1.0 # (Our app from earlier) docker network create app-net docker network connect app-net my-db docker network connect app-net my-app
Now,
my-app
can reachmy-db
using the hostnamemy-db
. π₯
7. Multi-Container Applications with Docker Compose π¦
When your application consists of multiple services (e.g., a web app, a database, a cache), managing them with individual docker run
commands becomes cumbersome. Enter Docker Compose!
Docker Compose is a tool for defining and running multi-container Docker applications. You define your application’s services, networks, and volumes in a docker-compose.yml
file, and then use a single command to bring everything up or down.
Why Docker Compose?
- Simplicity: Define your entire application stack in a single YAML file.
- Reproducibility: Ensures consistent environments across teams and stages.
- Orchestration: Simplifies starting, stopping, and scaling multi-service apps.
Basic docker-compose.yml
Structure
Create a docker-compose.yml
file in your project root:
version: '3.8' # Specify the Compose file format version
services:
web:
build: . # Build the image from the current directory (where Dockerfile is)
ports:
- "80:3000" # Map host port 80 to container port 3000
volumes:
- .:/app # Mount current directory into container for live changes
environment:
NODE_ENV: development
depends_on:
- db # Ensure 'db' service starts before 'web'
networks:
- app-network
db:
image: postgres:13
volumes:
- db_data:/var/lib/postgresql/data # Persist database data
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: user
POSTGRES_PASSWORD: password
networks:
- app-network
volumes:
db_data: # Define the named volume for database persistence
networks:
app-network: # Define a custom network for services to communicate
Note: The actual commands for Docker Compose depend on your Docker Desktop version. If you have a newer version, docker compose
(without the hyphen) is built-in. For older versions, it might be docker-compose
(separate installation).
docker compose up
: Starting Services
Builds (if necessary), creates, starts, and attaches to containers for all services defined in your docker-compose.yml
.
- Syntax:
docker compose up [OPTIONS]
- Key Options:
-d
(detached): Runs services in the background.--build
: Forces rebuilding of images.
- Example: From the directory containing your
docker-compose.yml
:docker compose up -d # Start all services in detached mode
docker compose down
: Stopping Services
Stops and removes containers, networks, and volumes created by up
.
- Syntax:
docker compose down [OPTIONS]
- Key Options:
-v
: Also removes named volumes (caution, this deletes data!).
- Example:
docker compose down # Stop and remove containers and networks docker compose down -v # Stop and remove everything, including volumes
docker compose ps
, logs
, exec
: Managing Services
These commands work similarly to their docker
counterparts but operate on the services defined in your Compose file.
docker compose ps
: Lists containers for your services.docker compose logs [service_name]
: Shows logs for all or a specific service. Use-f
to follow.-
docker compose exec [service_name] <command>
: Runs a command inside a specific service’s container.docker compose ps docker compose logs web -f docker compose exec db psql -U user mydatabase # Run psql inside the db container
8. Cleaning Up: Keeping Your System Tidy π§Ή
Docker can consume a lot of disk space over time with unused images, stopped containers, and dangling volumes/networks. Regular cleanup is essential!
docker system prune
: The All-in-One Cleanup π₯
This powerful command removes all stopped containers, all dangling images, and all unused networks. You can also extend it to clean volumes.
- Syntax:
docker system prune [OPTIONS]
- Key Options:
-a
(all): Removes all unused images, not just dangling ones (be careful!).--volumes
: Also removes all unused volumes (caution, this deletes data!).
- Example:
docker system prune # Removes stopped containers, dangling images, unused networks docker system prune -a # Removes all stopped containers, ALL unused images, unused networks docker system prune --volumes # Adds removal of unused volumes
You’ll be prompted to confirm. This is a lifesaver for disk space!
Targeted Removal Commands
You can also remove specific types of Docker objects:
- Containers:
docker container prune
(removes all stopped containers) - Images:
docker image prune
(removes dangling images);docker image prune -a
(removes all unused images) - Volumes:
docker volume prune
(removes all unused volumes) - Networks:
docker network prune
(removes all unused networks)
9. Advanced Tips & Troubleshooting π΅οΈββοΈ
docker inspect
: Deep Dive into Docker Objects
Provides detailed low-level information about Docker objects (images, containers, volumes, networks) in JSON format. Invaluable for debugging and understanding configurations.
- Syntax: `docker inspect
` - Example:
docker inspect my-nginx-app # See details of a running container docker inspect nginx # See details of the Nginx image
docker stats
: Monitoring Container Resources
Shows a live stream of resource usage (CPU, RAM, Network I/O, Disk I/O) for running containers.
- Syntax:
docker stats
- Example:
docker stats
Output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS a3e4b5c6d7e8 my-nginx-app 0.00% 4.23MiB / 7.69GiB 0.05% 1.51kB / 0B 0B / 0B 6
Getting Help (--help
)
Almost every Docker command has a --help
option that provides a quick reference for its syntax and available options.
- Example:
docker run --help docker build --help
Troubleshooting Common Issues
- Port already in use: “Bind for 0.0.0.0:8080 failed: port is already allocated.”
- Solution: Change the host port in your
-p
mapping (e.g.,-p 8081:80
) or stop the process currently using that port on your host.
- Solution: Change the host port in your
- Container exits immediately:
- Solution: Check `docker logs
`. The application inside the container might have crashed or finished its task. Ensure your `CMD` in the `Dockerfile` keeps the process running in the foreground.
- Solution: Check `docker logs
- Image not found: “Unable to find image ‘
: ‘ locally” * **Solution:** Double-check the image name and tag. Try `docker pull : ` first. - Permission denied (volumes):
- Solution: Ensure the Docker user has permissions to the host directory you’re trying to mount. On Linux, this can sometimes be an issue with user IDs.
Conclusion: Your Docker Journey Continues! π
Congratulations! You’ve now got a solid understanding of the most critical Docker commands, from managing individual images and containers to orchestrating multi-service applications with Docker Compose and keeping your system clean.
Docker is a powerful tool, and this guide is just the beginning. The more you use these commands in your daily workflow, the more intuitive they will become. Don’t be afraid to experiment, read the official Docker documentation, and explore more advanced topics like Docker Swarm, Kubernetes, and continuous integration/delivery with Docker.
What’s next?
- Practice! Try building and running your own simple applications.
- Explore Docker Hub: Discover thousands of ready-to-use images.
- Read the Official Docs: Docker’s documentation is excellent and comprehensive.
Happy containerizing! π