Mount a volume to persist data
In this step, you will learn how to use Docker volumes to persist data generated by and used by Docker containers. By default, the data inside a container is ephemeral; it is lost when the container is removed. Volumes provide a way to store data outside the container's filesystem, allowing it to persist even after the container is stopped or removed.
First, let's stop and remove the my-nginx
container from the previous step.
docker stop my-nginx
docker rm my-nginx
Now, we will create a Docker volume. Volumes are managed by Docker and are stored in a dedicated area on the host machine.
docker volume create my-volume
You should see the name of the volume (my-volume
) printed, confirming that it was created.
You can list existing volumes using the docker volume ls
command.
docker volume ls
You should see my-volume
in the list of volumes.
Now, we will run a new nginx
container and mount the my-volume
to the default Nginx webroot directory inside the container, which is /usr/share/nginx/html
. This means that any files placed in /usr/share/nginx/html
inside the container will actually be stored in the my-volume
on the host.
We will run the container in detached mode (-d
), publish port 80 inside the container to port 8081 on the host (-p 8081:80
), assign it a name (my-nginx-volume
), and use the -v
flag to mount the volume. The format for mounting a named volume is volume_name:container_path
.
docker run -d --name my-nginx-volume -p 8081:80 -v my-volume:/usr/share/nginx/html nginx
You should see the container ID printed, indicating that the container is running.
Now, let's place a simple HTML file in the mounted volume. We can do this by executing a command inside the running container using docker exec
. We will create a file named index.html
in /usr/share/nginx/html
with some simple content.
docker exec my-nginx-volume sh -c 'echo "<h1>Hello from the volume!</h1>" > /usr/share/nginx/html/index.html'
This command executes a shell (sh -c
) inside the my-nginx-volume
container and runs the echo
command to create the index.html
file.
Now, let's access the web server on port 8081 on the host to see the content of the index.html
file we just created.
curl http://localhost:8081
You should see <h1>Hello from the volume!</h1>
in the output. This confirms that the file we created inside the container is being served by Nginx, and since it was written to the mounted volume, the data is persistent.
To demonstrate persistence, let's stop and remove the my-nginx-volume
container.
docker stop my-nginx-volume
docker rm my-nginx-volume
Now, let's run a new container, mounting the same volume. We will name this new container my-nginx-volume-new
and publish its port 80 to host port 8082.
docker run -d --name my-nginx-volume-new -p 8082:80 -v my-volume:/usr/share/nginx/html nginx
The new container is running, and it's using the same my-volume
. Let's access the web server on port 8082.
curl http://localhost:8082
You should still see <h1>Hello from the volume!</h1>
in the output. This is because the index.html
file was stored in the my-volume
, which persisted even after the original container was removed. The new container, by mounting the same volume, has access to the data that was previously written.
This demonstrates the power of volumes for persisting data independently of the container lifecycle.