Docker Mastery Part 2: Essential CLI Commands for Developers

Master the command line. Learn the essential Docker commands for building, running, debugging, and managing your containers.

FSI
Full Stack Insights
2 min read...

Introduction

Now that we understand the concepts, it's time to interact with the Docker daemon. This guide covers the essential commands you need for your daily workflow.

1. Building and Running

docker build

Builds an image from a Dockerfile.

# Build an image named 'my-app' from the current directory (.)
docker build -t my-app .

docker run

Creates and starts a container from an image.

# Run 'my-app' in detached mode (-d) mapping host port 8080 to container port 3000
docker run -d -p 8080:3000 --name my-running-app my-app

2. Managing Containers

docker ps

Lists running containers.

  • Use docker ps -a to see all containers, including stopped ones.

docker stop & docker start

Stops and starts existing containers.

docker stop my-running-app
docker start my-running-app

docker rm

Removes a stopped container.

# Remove a specific container
docker rm my-running-app

# Force remove a running container (use with caution)
docker rm -f my-running-app

3. Debugging and Inspection

docker logs

View the output (stdout/stderr) of a container.

# Follow the logs in real-time
docker logs -f my-running-app

docker exec

Run a command inside a running container. This is often used to open a shell.

# Open an interactive bash shell inside the container
docker exec -it my-running-app /bin/sh

Summary

These commands form the backbone of Docker interaction. Practice them until they become muscle memory. Next, in Part 3: Orchestrating Services, we will simplify managing multi-container applications with Docker Compose.

Share this article:

Related Articles

FSI

Full Stack Insights

Software Engineer

Passionate about software development, architecture, and sharing knowledge with the community.