Docker for Beginners: Build, Run, and Troubleshoot Your First Container
Docker is one of the most useful tools for developers and IT professionals who want to run applications in consistent, isolated environments.
In this tutorial, we'll create a basic container, build our own Docker image, and explore a few essential troubleshooting commands.
1. Check Your Docker Installation
First, confirm Docker is installed and available:
docker --version
docker info
If the Docker daemon isn't running, docker info may display a connection error.
2. Run Your First Container
Start an Nginx web server:
docker run -d --name my-nginx -p 8080:80 nginx:alpine
What this command does:
-
-druns the container in the background. -
--nameassigns a readable name. -
-p 8080:80maps your computer's port 8080 to port 80 inside the container. -
nginx:alpinespecifies the image to use.
Open http://localhost:8080 in your browser. You should see the Nginx welcome page.
3. Check Running Containers
To see active containers:
docker ps
To see both running and stopped containers:
docker ps -a
4. Build Your Own Docker Image
Create a directory containing these two files.
index.html
<h1>Hello from my Docker container!</h1>
Dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Build the image:
docker build -t my-first-image .
Run it:
docker run -d --name custom-nginx -p 8081:80 my-first-image
Visit http://localhost:8081 to see your custom page.
5. Troubleshoot Common Docker Problems
Inspect a container's logs:
docker logs custom-nginx
View container details:
docker inspect custom-nginx
Check resource usage:
docker stats
Common problems include port conflicts, missing files, incorrect image names, and container startup failures.
6. Clean Up
Stop and remove the containers:
docker stop my-nginx custom-nginx
docker rm my-nginx custom-nginx
Final Thoughts
Docker becomes easier to understand when you experiment with real containers instead of relying only on theory.
Start with simple applications, learn how images and containers work, and gradually explore networking, volumes, Docker Compose, and security.
About the author: I'm Piyush Pandit, author of Docker and Containers for IT Professionals, a practical guide to building, running, securing, and troubleshooting containers with Docker and Compose.
My Kindle book is available through Amazon, with a limited-time free promotion when active.
Top comments (0)