How to Mount a Volume into a Docker Container
Mounting a volume into a Docker container allows you to persist data even after the container is restarted or deleted. This is useful for storing data that needs to be preserved, such as configuration files, logs, or databases.
Note: This is general Docker information not specific to Password Pusher. It is provided here to help users in configuring their privately hosted instances.
Prerequisites
- Docker installed on your system
- A Docker container running or created
- A
docker-compose.yml
file (optional)
Mounting a Volume
To mount a volume into a Docker container, you can use the -v
or --volume
flag when running the docker run
command. The syntax is as follows:
docker run -v <host_path>:<container_path> <image_name>
Where:
<host_path>
is the path on the host machine where the volume will be mounted<container_path>
is the path inside the container where the volume will be mounted<image_name>
is the name of the Docker image to use
For example, to mount a volume at /mnt
on the host machine to /app
in the container, you would use the following command:
docker run -v /mnt:/app my_image
This will mount the /mnt
directory on the host machine to /app
in the container.
Mounting a Volume from a Directory
You can also mount a volume from a directory on the host machine by using the -v
flag with the .
syntax. For example:
docker run -v .:/app my_image
This will mount the current working directory on the host machine to /app
in the container.
Mounting a Volume from a File
You can also mount a volume from a file on the host machine by using the -v
flag with the @
syntax. For example:
docker run -v /path/to/file:/app my_image
This will mount the file at /path/to/file
on the host machine to /app
in the container.
Mounting a Volume with a Specific Permission
You can also specify the permission for the volume by using the --volume-permissions
flag. For example:
docker run -v /mnt:/app --volume-permissions=755 my_image
This will mount the /mnt
directory on the host machine to /app
in the container with the permission 755
.
Using a docker-compose.yml File
If you’re using a docker-compose.yml
file to manage your containers, you can mount a volume by adding a volumes
directive to the service configuration. For example:
version: '3'
services:
my_service:
image: my_image
volumes:
- ./app:/app
This will mount the current working directory (./app
) on the host machine to /app
in the container.
Unmounting a Volume
To unmount a volume from a Docker container, you can use the docker rm
command with the -v
flag. For example:
docker rm -v /app my_image
This will unmount the volume at /app
in the container.
Best Practices
- Use a consistent naming convention for your volumes to avoid confusion
- Use the
--volume-permissions
flag to specify the permission for the volume - Use the
docker rm
command to unmount the volume when you’re finished with it - Use the
docker run
command with the-v
flag to mount the volume when you start the container - Use a
docker-compose.yml
file to manage your containers and volumes
By following these best practices, you can effectively use volumes to persist data in your Docker containers.