Mastering Docker Commands and Volumes

Appblee
0



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

You can stop a running container using the following command:

docker stop [container_id]


For example, if your container ID is 25c2e31ee050.
You can stop it by running:

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.


You can simply run:

docker stop my-container


💡 Tip

Using container names can make your workflow much easier, especially when working with multiple containers.


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
  




Exposing multiple ports is especially useful because it allows your container to handle multiple services simultaneously. For instance, you might have a web server running on one port and an API on another. This setup makes it easier to test and access different endpoints directly from your host machine, giving you more flexibility and control over how your container interacts with other applications.


Other Useful Docker Commands

Along with the commands we’ve already learned, there are several other commands that are extremely useful when working with Docker. Let’s go through some of them.

1. See all containers

To list all containers, including stopped ones:


docker ps -a

If you only want the container IDs, use:

docker ps -aq


2. Remove a container

To remove a container by its ID:


docker rm 6bfc0782b1b


If the container is running, you need to force remove it using -f:

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


Now that you have a good understanding of Docker commands, the next important concept is Docker volumes.


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


Let’s say your HTML website is located at:

C:\Users\ExampleUser\Desktop\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:

docker run --name website -v C:\Users\ExampleUser\Desktop\website:/usr/share/nginx/html:ro -d -p 8080:80 nginx:latest


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


  1. Docker creates a volume linking your local folder to the container folder.
  2. Any changes in C:\Users\ExampleUser\Desktop\website immediately reflect in the container.
  3. 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 /

This helps you find the correct folder inside the container to map your local files.


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

• Always check the container path before mapping a volume.
• 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.



That’s it for this article! 😊 You’ve now learned the essential Docker commands and how to use volumes to keep your data persistent and in sync. With these skills, managing containers and experimenting with your projects becomes much easier and more fun. In the next article, we’ll explore Dockerfiles and see how to create your own custom images, taking your Docker knowledge to the next level. Stay tuned, and happy containerizing! 

Tags:

Post a Comment

0Comments

Post a Comment (0)