Create and Mount a Docker Volume
Tasks
- Create a new Docker volume named
data_volume
.
- Run a new container using the Alpine image. Mount the
data_volume
volume to /app
inside the container. Create a file named hello.txt
with the content "Hello, Docker volumes." in the /app
directory. Ensure the container remains running in the background.
Requirements
To successfully complete this challenge, adhere to the following guidelines:
- Perform all operations in the
/home/labex/project
directory.
- Use the Alpine image for your container.
- Name your container
volume_mounter
.
- The content of
hello.txt
should be exactly "Hello, Docker volumes."
- Use Docker commands to create volumes and run containers.
- Mount the volume at the
/app
path inside the container.
- Ensure the container is running in the background.
Example
After completing the tasks, verify your work by running the following commands:
- Check if the volume was created:
docker volume ls | grep data_volume
This should list the "data_volume" you created.
- Check the status of your container:
docker ps | grep volume_mounter
This should show your "volume_mounter" container in a running state.
- Inspect the container to verify the volume mounting:
docker inspect volume_mounter --format '{{ range .Mounts }}{{ if eq .Destination "/app" }}{{ .Name }}{{ end }}{{ end }}'
This should output "data_volume", confirming that the volume is correctly mounted.
- Check if the file you created exists:
docker exec volume_mounter cat /app/hello.txt
This should display the content of the "hello.txt" file.