DEV Community

Meshack Mulei
Meshack Mulei

Posted on

Docker Volumes + Live Website Editing

Introduction

Yesterday, every time I updated index.html, I had to rebuild my Docker image and launch a brand-new container just to see the change. Today, I solved that bottleneck using Docker Volumes (bind mounts), linking my local project folder directly to a running Apache container so edits appear in the browser the instant I hit save.

1. Research: Understanding Docker Volumes

  • What is a Docker Volume? A Docker Volume (and bind mount) is a persistent storage mechanism that connects a folder on your host machine to a folder inside a container, allowing data to survive container restarts or deletions and stay synced in real time.
  • How do Docker Volumes work? Instead of freezing files inside an image's read-only layers, Docker mounts a directory from the host filesystem directly into a target path inside the container. Both the host and the container point to the same files on disk.
  • What is the difference between using a Docker Volume and the COPY instruction in a Dockerfile? COPY takes a static, one-time snapshot of your files during docker build and bakes them permanently into the image. A Volume creates a live two-way link at runtime (docker run), so changes made on your machine immediately reflect inside the container without rebuilding.

2. Returning to My Workspace

I navigated back to my Day2 folder and verified that both index.html and Dockerfile were intact—and that yesterday's container had already been stopped and removed:

cd ~/Day2
ls -la
docker ps -a
Enter fullscreen mode Exit fullscreen mode

Returning to My Workspace

  • What happens to your project files after you remove a container? Nothing happens to your local project files. They stay completely safe on your host machine's hard drive because removing a container only deletes the container's isolated runtime layer, not your local folder.

3. Running Apache with a Live Volume Mount

Instead of building a custom image today, I used the official httpd:2.4 Apache image and mounted my current Day2 directory directly into Apache's web root:

docker run -d -p 8080:80 -v $(pwd):/usr/local/apache2/htdocs/ --name my-site httpd:2.4
docker ps
Enter fullscreen mode Exit fullscreen mode

Running Apache with a Live Volume Mount

  • What does -v do? The -v (volume) flag creates a live mount linking a path on my host machine (left of :) to a path inside the container (right of :).
  • What does $(pwd) do? $(pwd) is Linux command substitution. It runs pwd (print working directory) and automatically inserts the full absolute path of my current folder (/home/meshack-mulei/Day2) into the command.
  • Why /usr/local/apache2/htdocs/? That is the default document root directory where the official httpd:2.4 Apache server looks for website files to serve over port 80.

4. Opening the Website (Before Editing)

Visiting http://localhost:8080 showed my original webpage from yesterday, served live by the official httpd:2.4 container:

Opening the Website

5. Experiencing the Magic (Live Sync)

Next, I opened index.html and updated the file. When I refreshed http://localhost:8080, the updates appeared immediately:

Live Sync

  • Did I have to rebuild the image, stop the container, or start a new container? No. I didn't touch Docker at all. I just saved index.html and refreshed the browser.
  • Why did the changes appear immediately? Because -v $(pwd):/usr/local/apache2/htdocs/ mounted my actual Day2 folder into the container rather than copying a static snapshot. As soon as I saved index.html on Ubuntu, Apache inside the container read the updated file from disk.

6. Verifying the Volume Mount

I inspected the running my-site container to confirm the mount configuration under the Mounts section:

docker inspect my-site | grep -A 15 '"Mounts"'
Enter fullscreen mode Exit fullscreen mode

Verifying the Volume Mount

  • Which folder on my machine is being shared? /home/meshack-mulei/Day2 (Source)
  • Which folder inside the container is connected to it? /usr/local/apache2/htdocs (Destination)

7. Docker Volume Management & Cleanup

Finally, I checked Docker volumes, stopped and removed my-site, and confirmed my local project files were still intact:

docker volume ls
docker ps
docker stop my-site
docker rm my-site
docker ps -a
docker images
ls -la
Enter fullscreen mode Exit fullscreen mode

(Note: Because I used a host bind mount with -v $(pwd):..., it links my existing local folder directly rather than creating a standalone named volume in docker volume ls.)

Docker Volume Management & Cleanup

Reflection

  • What surprised you most about Docker Volumes? Seeing the browser update instantaneously without restarting the container. It completely changed how fast I can test code inside Docker.
  • Why are Volumes better than rebuilding an image every time you make a small change? Rebuilding an image for every single HTML or code tweak wastes time and creates redundant image layers. Volumes let you iterate in real time during development.
  • When would you use a Docker Volume instead of the COPY instruction? I would use a Volume during local development (for instant code previews) or for persistent data like databases, and use COPY when packaging a finished, self-contained image ready to ship to production.
  • Which part of today's challenge was the most difficult? Understanding the difference between a host bind mount (-v $(pwd):...) and a Docker-managed volume in docker volume ls, as well as reading through the large JSON output of docker inspect to locate the Mounts section.

Top comments (0)