๐ณ Getting Started with Docker: A Hands-on Guide for Beginners


If you're new to Docker and looking for a practical, beginner-friendly tutorial to understand the core commands used to run, inspect, and manage containers and images, this guide is exactly what you need.
In this hands-on walkthrough, we'll cover:
How to run Docker containers
How to interact with and inspect them
How to manage Docker images
How to clean up unused containers and images
๐ ๏ธ Prerequisites
Docker must be installed and running on your machine.
Basic knowledge of terminal/command line usage.
You can check if Docker is installed by running:
docker --version
๐ Step 1: Pull an Image
Start by pulling the official nginx image from Docker Hub:
docker pull nginx
This command downloads the nginx image locally so you can use it to run containers.
๐ Step 2: Run a Container in Detached Mode with a Name
Now create and start a container from the nginx image:
docker run -d --name mynginx nginx
Explanation:
-d
: Detached mode (runs in background)--name mynginx
: Assigns a custom name to the containernginx
: Name of the image to use
Verify it's running:
docker ps
This will show running containers with details like Container ID, image, status, and ports.
๐ป Step 3: Access the Container's Shell
To enter an interactive shell inside the running container:
docker exec -it mynginx /bin/bash
If /bin/bash
doesn't work (for Alpine-based images), use:
docker exec -it mynginx /bin/sh
Once inside, try commands like:
ls
hostname
Exit the container shell:
exit
๐ Step 4: Inspect Container Details
To view detailed information about a container (in JSON format):
docker inspect mynginx
To fetch specific info, like the IP address:
docker inspect -f '{{ .NetworkSettings.IPAddress }}' mynginx
This is helpful for debugging or checking network configurations.
๐งฑ Step 5: View Image History
To see the history of commands that built a Docker image:
docker history nginx
This shows each layer added to the image, including commands like FROM, COPY, and RUN.
๐ณ Step 6: Stop and Remove a Container
To stop a running container:
docker stop mynginx
Then remove it:
docker rm mynginx
To remove the nginx image:
docker rmi nginx
๐ฎ Bonus: Clean Up Commands
๐ List Containers and Images
docker ps # running containers
docker ps -a # all containers (including stopped)
docker images # all local images
๐๏ธ Remove All Stopped Containers
docker container prune
Or:
docker rm $(docker ps -a -q)
๐ Remove All Unused Images
docker image prune
Or:
docker rmi $(docker image ls -q)
Use with caution: These will free up space but delete unused content.
๐ผ Recap: Key Commands Summary
Task | Command Example |
Run a container | docker run -d --name name image |
Access container shell | docker exec -it container /bin/bash |
Inspect container details | docker inspect container_id |
Show image layers | docker history image_id |
List running containers | docker ps |
List all containers | docker ps -a |
List images | docker images |
Stop container | docker stop container_id |
Remove container | docker rm container_id |
Remove image | docker rmi image_name |
Prune all stopped containers | docker container prune |
Prune all unused images | docker image prune |
๐ You're Ready to Docker!
Congratulations! Youโve now taken your first steps with Docker containers and images. This hands-on experience will help you understand containerized workflows used in real-world DevOps, development, and deployment pipelines.
If you enjoyed this guide, share it with your fellow developers or leave a comment with what you'd like to learn next!
Happy Dockering! ๐ณ
Subscribe to my newsletter
Read articles from Sonica Sonawane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sonica Sonawane
Sonica Sonawane
Hi, I'm Sonica! ๐ Iโm currently diving into the world of DevOps, focusing on AWS, Docker, Kubernetes, Linux, and GitHub. My passion lies in automating systems, building cloud infrastructure, and optimizing workflows. Iโm committed to continuous learning, hands-on projects, and sharing my journey with others in the tech community. Before shifting to DevOps, I worked in IT Sales, where I gained valuable skills in client communication, requirement gathering, and problem-solving. This experience taught me how to connect technical solutions to business needs, which has been instrumental as I transition into DevOps, where technical expertise and problem-solving go hand in hand. Now, Iโm eager to apply my sales experience alongside my growing technical skills in cloud engineering and DevOps. Join me as I explore the latest trends, challenges, and solutions in the world of cloud computing!