Managing Docker Containers – Essential Commands

Pawan LuhanaPawan Luhana
2 min read

1. Introduction

Docker containers are the heart of Dockerβ€”they run your applications in an isolated, lightweight environment.

Think of a container like a running app on your phone πŸ“±. You can start it, stop it, and remove it when you no longer need it.

In this guide, you'll learn how to run, manage, and inspect Docker containers using essential commands.


2. What Are Docker Containers?

A Docker container is a lightweight, standalone unit that runs an application along with all its dependencies.

Example:

Running an Nginx web server inside a container:

docker run -d -p 8080:80 nginx

Now, open http://localhost:8080 in your browser to see the Nginx welcome page! πŸŽ‰


3. Running Containers Using docker run

The docker run command creates and starts a container.

Example: Running a Basic Ubuntu Container

docker run -it ubuntu

This starts an interactive Ubuntu container where you can run Linux commands.

Common docker run Options:

OptionDescription
-dRun in detached mode (in the background)
-p 8080:80Expose container port 80 to host port 8080
-itInteractive mode (useful for debugging)

4. Listing Running & Stopped Containers (docker ps)

To see active containers:

docker ps

To see all containers (including stopped ones):

docker ps -a

Example Output:

CONTAINER ID   IMAGE    STATUS          PORTS
123abc456def   nginx    Up 2 minutes    0.0.0.0:8080->80/tcp

5. Stopping & Removing Containers

Stopping a Container

docker stop <container_id>

Example:

docker stop 123abc456def

Removing a Container

docker rm <container_id>

πŸ’‘ Tip: Remove all stopped containers in one command:

docker rm $(docker ps -aq)

6. Running a Container in Detached Mode (-d)

Detached mode allows a container to run in the background.

Example:

docker run -d nginx

Check running containers with:

docker ps

7. Exposing Ports & Inspecting Logs

Exposing Ports

To make a container accessible, use the -p flag:

docker run -d -p 8080:80 nginx

Now, visit http://localhost:8080 in your browser.

Viewing Container Logs

docker logs <container_id>

Example:

docker logs 123abc456def

This helps debug issues and check container activity.


8. Conclusion

You now know how to run, stop, remove, and inspect Docker containers. πŸŽ‰

Try running different containers and experiment with ports and logs! πŸš€

0
Subscribe to my newsletter

Read articles from Pawan Luhana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pawan Luhana
Pawan Luhana