DEV Community

Piyush Pandit
Piyush Pandit

Posted on

Docker for Beginners: Build, Run, and Troubleshoot Your First Container

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

What this command does:

  • -d runs the container in the background.
  • --name assigns a readable name.
  • -p 8080:80 maps your computer's port 8080 to port 80 inside the container.
  • nginx:alpine specifies 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
Enter fullscreen mode Exit fullscreen mode

To see both running and stopped containers:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

4. Build Your Own Docker Image

Create a directory containing these two files.

index.html

<h1>Hello from my Docker container!</h1>
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Enter fullscreen mode Exit fullscreen mode

Build the image:

docker build -t my-first-image .
Enter fullscreen mode Exit fullscreen mode

Run it:

docker run -d --name custom-nginx -p 8081:80 my-first-image
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:8081 to see your custom page.

5. Troubleshoot Common Docker Problems

Inspect a container's logs:

docker logs custom-nginx
Enter fullscreen mode Exit fullscreen mode

View container details:

docker inspect custom-nginx
Enter fullscreen mode Exit fullscreen mode

Check resource usage:

docker stats
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

View the book on Amazon

Top comments (0)