How to Create Docker Images and Troubleshoot Containers: Complete Guide

Table of contents
- Introduction
- Part 1: Creating Docker Images
- A. Using a Dockerfile (Best Practice)
- B. Committing a Container
- Part 2: Troubleshooting Docker Containers
- 1. Check Container Logs
- 2. Inspect the Container
- 3. Access the Container Shell
- 4. Check Dockerfile Build Output
- 5. Troubleshoot Networking
- 6. Restart and Prune Docker Resources
- 7. Update Docker and Images
- Conclusion
Introduction
Docker has transformed how modern applications are built, shipped, and run. Whether you're packaging a web app for deployment or diagnosing container issues, mastering Docker images and troubleshooting containers is are must-have skill for any developer or DevOps engineer. This guide covers the essentials, with step-by-step instructions and practical troubleshooting tips.
Part 1: Creating Docker Images
There are two primary methods for creating Docker images:
A. Using a Dockerfile (Best Practice)
A Dockerfile contains a set of instructions that Docker uses to assemble an image. Here's a simple workflow:
Create a project directory
Example:mkdir myapp && cd myapp
Add your application code and dependencies
Place your code (e.g.,app.py
,requirements.txt
) inside this directory.Write your Dockerfile
Example (Dockerfile
for a Python app):# Use an official Python runtime as a base image FROM python:3.9-slim # Set working directory WORKDIR /app # Copy app code COPY . . # Install dependencies RUN pip install --no-cache-dir -r requirements.txt # Run the application CMD ["python", "app.py"]
Build the Docker image
docker build -t myapp:1.0 .
Run the container
docker run -d -p 8000:8000 myapp:1.0
Adjust ports as required.
This process automates image creation and is version-controlled, making updates and debugging easier.
B. Committing a Container
Alternatively, you can make changes inside a running container and commit it as a new image.
Example:
Run a base container:
docker run -it --name customapp python:3.9 bash
Make your changes (install packages, copy files).
Exit and commit:
docker commit customapp mycustomapp:1.0
However, this method is less reproducible and should be reserved for prototyping.
Part 2: Troubleshooting Docker Containers
Even well-constructed containers can run into issues. Here are common troubleshooting steps:
1. Check Container Logs
Get detailed error output:
docker logs <container_name_or_id>
Very useful for debugging an app or runtime errors.
2. Inspect the Container
View configuration, environment variables, mounted volumes, and networking:
docker inspect <container_name_or_id>
This helps detect misconfigurations or missing dependencies.
3. Access the Container Shell
For direct debugging:
docker exec -it <container_name_or_id> /bin/bash
Now you can manually test, view files, and run commands inside the failing container.
4. Check Dockerfile Build Output
If your image build fails:
Review terminal logs carefully during
docker build
.Temporarily disable BuildKit with:
DOCKER_BUILDKIT=0 docker build -t myapp .
Investigate errors at particular steps and test those commands interactively.
5. Troubleshoot Networking
Inspect Docker networks:
docker network inspect <network_name>
Check connectivity from within the container:
docker exec -it <container_id> ping <target_ip>
or
docker exec -it <container_id> nc -zv <target_ip> <port>
These help pinpoint network isolation issues between containers or with the host.
6. Restart and Prune Docker Resources
Restart Docker Engine (or Docker Desktop)
Prune dangling images, networks, and volumes:
docker system prune
This can clear up stale data or resource conflicts.
7. Update Docker and Images
Make sure both Docker itself and your images are updated to the latest versions to fix known bugs and incompatibilities.
Conclusion
Start with a clear Dockerfile for reproducible images.
For troubleshooting, logs, inspect commands, and network/debugging tools are your best friends.
Keep your Docker environment clean and up-to-date to prevent many common issues.
Mastering these steps means smoother builds, faster fixes, and resilient containerized applications.
Ready to apply your skills? Try building and running a demo Dockerfile, break things on purpose, and practice using these troubleshooting commands!
Subscribe to my newsletter
Read articles from Jyotiraditya Deshmukh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
