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

Sonica SonawaneSonica Sonawane
3 min read

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 container

  • nginx: 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

TaskCommand Example
Run a containerdocker run -d --name name image
Access container shelldocker exec -it container /bin/bash
Inspect container detailsdocker inspect container_id
Show image layersdocker history image_id
List running containersdocker ps
List all containersdocker ps -a
List imagesdocker images
Stop containerdocker stop container_id
Remove containerdocker rm container_id
Remove imagedocker rmi image_name
Prune all stopped containersdocker container prune
Prune all unused imagesdocker 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! ๐Ÿณ

0
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!