🐳 Basic Docker Commands Every Beginner Should Know
If you're starting out with Docker, the command-line interface can feel overwhelming. But once you know the basic commands, managing containers and images becomes second nature.
In this guide, we’ll walk through the most important Docker commands step by step, with examples you can try out. By the end, you’ll feel confident running, stopping, and managing containers like a pro.
🚀 Running a Container
The docker run command creates and starts a container from an image.
Example:
docker run nginx
If the nginx image is already on your system, Docker starts it immediately.
If not, Docker will pull it from Docker Hub first.
Once downloaded, future runs will use the cached image.
📋 Listing Containers
To see running containers:
docker ps
To list all containers, including stopped ones:
docker ps -a
This gives you container IDs, image names, statuses, and more.
🛑 Stopping and Removing Containers
Stop a container by ID or name:
docker stop silly_sammet
Remove a stopped container:
docker rm silly_sammet
Now it won’t appear in your docker ps -a
list anymore.
📦 Managing Docker Images
Check which images are stored locally:
docker images
Remove an image:
docker rmi nginx
⚠️ Note: Docker won’t remove an image if any container (running or stopped) is still using it. Remove those containers first.
⬇️ Pulling Images Without Running
You can download an image for later use with:
docker pull nginx
This way the image is ready whenever you want to run it.
🐧 Running Ubuntu Containers
Running Ubuntu like this:
docker run ubuntu
will start and immediately exit — because Ubuntu doesn’t have a long-running process by default.
Keep it alive with a command:
docker run ubuntu sleep 5
This container runs sleep for 5 seconds, then exits.
🔍 Executing Commands Inside Containers
If you run an Ubuntu container in the background:
docker run -d ubuntu sleep 100
docker ps
Run a command inside the container:
docker exec <container_id> cat /etc/hosts
This is super useful for debugging or checking files in real time.
🌐 Running a Web Application Container
Try running a sample web app:
docker run kodekloud/simple-webapp
It will log directly to your terminal.
To run it in the background (detached mode):
docker run -d kodekloud/simple-webapp
See running containers:
docker ps
To reattach:
docker attach <container_id>
✅ Recap
Here’s a quick summary of the commands:
docker run
→ Start a container
docker ps / docker ps -a
→ List containers
docker stop / docker rm
→ Stop & remove containers
docker images / docker rmi
→ Manage images
docker pull
→ Download an image without running
docker exec
→ Run commands inside containers
docker attach
→ Reconnect to a running container
🎯 Next Steps
These are the fundamentals of Docker. Once you’re comfortable here, you can move on to networking, volumes, and multi-container applications with Docker Compose.
👉 Try these commands on your own system, and you’ll quickly build confidence.
✍️ If you enjoyed this guide, follow me here on Dev.to and subscribe to my YouTube channel Data Enthusiast Era for more beginner-friendly DevOps tutorials.
Top comments (0)