Docker Tutorial
Estimated reading: 6 minutes 38 views

🐳 Docker Containers – The Complete Beginner’s Guide

🧲 Introduction – What Are Docker Containers?

Docker containers are a revolutionary solution for packaging, deploying, and running applications in a lightweight and portable environment. Unlike traditional virtual machines, containers share the host operating system’s kernel while keeping applications isolated and consistent across development, staging, and production.

This guide covers everything from running and managing containers to inspecting, accessing, and deleting them—all with simple commands and practical examples.


📋 Topics Covered

🧩 Topic📖 Description
Docker ContainersA foundational look at what Docker containers are and how they work.
Docker Container ExplainedUnderstanding how containers differ from images and VMs.
How to run Docker Images as containersInstructions to run images into active containers.
How to See All Running Docker ContainersCommands to list active or historical container sessions.
How to check the history of all containersView logs and metadata of container activity.
How to Stop a Running ContainerGracefully or forcefully terminate running containers.
How to Get Inside a Running Docker ContainerInteract directly with a live container terminal.
How to Exit from a Docker ContainerExit methods without stopping the container.
How to Delete or Remove Containers in DockerCommands to clean up stopped or unused containers.
Docker Container Port MappingMap internal container ports to host machine ports for external access.

🐋 Docker Containers

A Docker container is a lightweight, executable package that includes the software and everything it needs to run—such as code, runtime, libraries, and environment variables. Containers make applications portable and ensure they behave the same regardless of where they’re run.

Containers are created from images and can be started, stopped, modified, or deleted as needed. They isolate software processes but share the underlying OS kernel for efficiency.


🧠 Docker Container Explained

While Docker images act as read-only templates or blueprints, containers are the live, running instances of these images. Think of it like an object created from a class in programming. Containers start from images, but once launched, they run independently and can have their own state and file system changes.

This allows multiple containers to be spun up from the same image for testing, development, and production—all with different configurations or data.


▶️ How to Run Docker Images as Containers

To launch a new container from an image:

docker run -it ubuntu
  • -it lets you interact with the container’s shell.
  • ubuntu is the image name.

You can also run containers in the background using detached mode:

docker run -d nginx

This starts the nginx container in the background and outputs its container ID.


📋 How to See All Running Docker Containers

Use this command to list currently running containers:

docker ps

To view all containers, including those that have stopped:

docker ps -a

This displays:

  • Container ID
  • Image used
  • Command
  • Status (Up, Exited)
  • Port mappings
  • Names

🕓 How to Check the History of All Containers

You can check the command history and activity of a container using:

docker logs <container_id>

To see the lifecycle history of containers (started, exited, etc.):

docker ps -a

For detailed metadata, try:

docker inspect <container_id>

🛑 How to Stop a Running Container

To stop a container gracefully:

docker stop <container_id>

To forcefully stop it:

docker kill <container_id>

Stopping containers is essential before removing or updating them.


🚪 How to Get Inside a Running Docker Container

You can enter a running container’s shell using:

docker exec -it <container_id> /bin/bash

For Alpine or minimal images that don’t have bash, use:

docker exec -it <container_id> /bin/sh

This is helpful for debugging or directly interacting with container processes.


🔚 How to Exit from a Docker Container

There are two ways to exit a container session:

  • Type exit to leave and stop the container (if started with -it)
  • Press Ctrl + P followed by Ctrl + Q to detach from the terminal and leave the container running in the background

🗑️ How to Delete or Remove Containers in Docker

To remove a specific container:

docker rm <container_id>

To delete all stopped containers:

docker container prune

Add -f to skip confirmation:

docker container prune -f

⚠️ Ensure the container is not running before deleting it, or add the -f flag to force removal.


🌐 Docker Container Port Mapping

To expose a container’s internal port to your local machine, use the -p flag:

docker run -d -p 8080:80 nginx

This maps port 80 inside the container to port 8080 on your host, making the app accessible via http://localhost:8080.

Use this to expose web servers, APIs, or other networked applications running inside containers.


📌 Summary – Docker Containers

Docker containers are a powerful tool for software deployment, offering isolated, lightweight environments that ensure application consistency. From running and managing containers to interacting with and cleaning them up, mastering these commands boosts your DevOps productivity and system hygiene.

🔍 Key Takeaways:

  • Containers are live environments created from images.
  • docker run, docker ps, docker stop, docker exec, and docker rm are essential commands.
  • Port mapping lets you expose container apps to the outside world.
  • Managing containers effectively leads to cleaner and more scalable development pipelines.

⚙️ Whether you’re working on microservices, testing frameworks, or full-stack apps, containers will play a key role in your modern development workflow.


❓ Frequently Asked Questions (FAQs)

❓ What is the difference between Docker images and containers?
✅ Images are templates; containers are the running instances of those templates.

❓ How do I view logs of a Docker container?
✅ Use docker logs <container_id> to view output logs from the container’s primary process.

❓ How can I delete all stopped containers at once?
✅ Use docker container prune to remove them in a single command.

❓ Can I run multiple containers from the same image?
✅ Yes, you can launch as many containers as needed from a single image.

❓ How can I access a running container’s shell?
✅ Use docker exec -it <container_id> /bin/bash or /bin/sh depending on the base image.

❓ How do I expose container ports to the host system?
✅ Use the -p flag like -p 8080:80 to map internal to external ports.

❓ Can a single container have multiple port mappings?
✅ Yes, you can define multiple -p flags in a single docker run command.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Docker Containers

Or Copy Link

CONTENTS
Scroll to Top