Hi everyone! 👋
In the previous article, we explored Docker basics and learned how to create and run a container. Today, let’s go a bit further and look beyond that.
Last time, we ended our discussion with port mapping. By now, you already know how to pull an image and run a container.
But what if you want to stop a running container? 🤔
Docker provides a simple command for that.
🛑 Stopping a Docker Container
docker stop [container_id]
docker stop 25c2e31ee050
🏷️ Using Container Name Instead of ID
Instead of using the container ID, you can also use the container name, which is often easier to remember.
For example, if your container name is my-container.
docker stop my-container
💡 Tip
So, remember how we talked about port mapping in the last article? Back then, we mapped a single port to run a container. Well, you can actually map multiple ports at the same time! This is called exposing multiple ports, and it’s really handy when your container needs to communicate over more than one port.
Here’s an example:
docker run -d -p 8080:80 -p 8000:80 nginx:latest
│ │ │ │ │ │
│ │ │ │ │ └─ Tag (latest)
│ │ │ │ └─ Image (nginx)
│ │ │ └─ Port mapping (host 8000 → container 80)
│ │ └─ Port mapping (host 8080 → container 80)
│ └─ Detached mode (-d)
└─ Docker run command
Other Useful Docker Commands
1. See all containers
To list all containers, including stopped ones:
docker ps -a
docker ps -aq
2. Remove a container
To remove a container by its ID:
docker rm 6bfc0782b1b
docker rm -f b8cf76901b1a
You can also remove all containers at once:
docker rm -f $(docker ps -aq)
3. Rename a container
You can give a container a custom name when running it with --name:
docker run --name website -d -p 8080:80 nginx:latest- Here, --name website assigns the container the name website.
- You can now start the container using the name instead of the ID:
docker start website
- And stop it using:
docker stop website
📝 Note
• Using docker ps -a helps you see all running and stopped containers.
• docker rm -f is useful to quickly clean up containers.
• Assigning a custom name makes it easier to start/stop containers without remembering long IDs.
• Always check which containers are running before removing anything to avoid mistakes.
Understanding Docker Volumes with a Practical Example
What are Docker Volumes?
Think about this you have a container running a simple HTML website on your machine. You make some changes to your files locally say, you change the background color of your website. If you run the container normally, the changes you make on your local machine will not appear inside the container. That’s because Docker containers have their own isolated filesystem.
This is where Docker volumes come in.
A volume is like a shared storage space between your local machine and the container. When you mount a volume, any changes you make locally will immediately appear inside the container. Similarly, changes in the container can also be reflected in your local files (unless you mount it as read-only).
In short, volumes allow persistent storage and live synchronization between your local files and the container. This is super useful for:
- Web development, where you want to see your changes live
- Databases, to keep data even after the container is removed
- Any application that requires data persistence
Practical Example: Mounting a Volume for Your Website
This is your local path. You want to run an Nginx container that serves this website and automatically reflects any changes you make locally.
You can do this using a Docker volume:
- --name website → Assigns a friendly name to your container, making it easier to start/stop it.
- -v C:\Users\ExampleUser\Desktop\website:/usr/share/nginx/html:ro → This is the volume mapping:
- Left side: C:\Users\ExampleUser\Desktop\website → Your local folder
- Right side: /usr/share/nginx/html:ro → Folder inside the container where Nginx serves files
- :ro → Mounts the volume as read-only (optional; remove if you want the container to modify files)
- -d → Runs the container in detached mode (in the background)
- -p 8080:80 → Maps host port 8080 to container port 80
- nginx:latest → The Docker image for Nginx
How It Works
Once you run this command:
- Docker creates a volume linking your local folder to the container folder.
- Any changes in C:\Users\ExampleUser\Desktop\website immediately reflect in the container.
- Open http://localhost:8080 in your browser to see your website live.
This is perfect for development because you can see your changes instantly without rebuilding the container.
How to Know the Container Path
You may ask: How do I know /usr/share/nginx/html is the right path inside the container?
- Check the official image documentation; for Nginx, this path is where files are served.
- For other applications, you can inspect the container:
docker exec -it website ls /
Benefits of Using Docker Volumes
- Live updates: Changes in local files reflect immediately in the container.
- Persistent storage: Data is not lost even if the container is removed.
- Better workflow: Perfect for development projects, websites, and databases.
- Flexibility: You can mount multiple volumes for different purposes.
📝 Tip
• Use :ro if you want the volume to be read-only.
• Volumes are essential for development projects to see live changes without rebuilding the container.
• For production databases, volumes help keep your data safe even if the container is removed.

