Antonios P.
Antonios P.

Reputation: 228

Change mountpoint of docker volume to a custom directory

I would like to have a Docker Volume that mounts to a container. This volume would need to be somewhere other than the default location of volumes, preferably somewhere on the Desktop. This is because I am running a web server and would like some directories to be editable by something like VSCode so I don't always have to go inside the container to edit a file. I am not going to be using Docker Compose and instead will be using a Docker File for the container. The functionality I'm going for is the following equivalent of Docker Compose, but in a Dockerfile or through docker run, whichever is easiest to accomplish:

volumes:
  - <local-dir>:<container-dir>

This directory will need to be editable LIVE and using the Dockerfile ADD command will not suffice, because after building, the image gets put into a tar archive and cannot be accessed after that.

Upvotes: 0

Views: 8425

Answers (2)

Ali Forouzan
Ali Forouzan

Reputation: 39

with this solution you can move even A live container to new partition:

  1. Add a configuration file to tell the docker daemon what is the location of the data directory Using your preferred text editor add a file named daemon.json under the directory /etc/docker. The file should have this content:
{ 
   "data-root": "/path/to/your/docker" 
}
  1. Copy the current data directory to the new one
sudo rsync -aP /var/lib/docker/ /path/to/your/docker
  1. Rename the old docker directory
sudo mv /var/lib/docker /var/lib/docker.old
  1. Restart the docker daemon
sudo service docker start

resource: https://www.guguweb.com/2019/02/07/how-to-move-docker-data-directory-to-another-location-on-ubuntu/

Upvotes: 2

Neo Anderson
Neo Anderson

Reputation: 6350

You can mount a directory from your host inside your container when you launch the docker container, using -v or --volume

docker run -v /path/to/desktop/some-dir:/container-dir/path <docker-image>

Volumes specified in the Dockerfile, as you exemplified, will automatically create those volumes under /var/lib/docker/volumes/ every time a container is launched from that image, but it is NOT recommended have these volumes altered by non-Docker processes.

Upvotes: 0

Related Questions