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 -ato 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.
Related Articles
Dockerizing ASP.NET Core: From Basics to Production
A complete guide to containerizing ASP.NET Core applications. Learn standard patterns, security best practices, and how to use Chiseled Ubuntu images.
Docker Mastery Part 5: Advanced Networking, Volumes, and Best Practices
Level up your Docker skills. Deep dive into Volumes for data persistence, advanced Networking types, and Dockerfile optimization.
Full Stack Insights
Software Engineer
Passionate about software development, architecture, and sharing knowledge.
Quick Links
Full Stack Insights
Software Engineer
Passionate about software development, architecture, and sharing knowledge with the community.