How do I change the default docker container locat

2020-02-07 16:06发布

When I run docker, downloaded docker images (seem to be) stored in /var/lib/docker somewhere.

Since disk space is limited on this directory, and I'm provisioning docker to multiple machines at once; is there a way to change this default location to i.e. /mnt/hugedrive/docker/?

标签: docker
5条回答
冷血范
2楼-- · 2020-02-07 16:19

In /etc/default/docker or whatever location it exists in your system, change the following to something like this:

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.8.4 -g /drive/location

If you have issues and it is ignored, apply this solution: Docker Opts in Etc Default Docker Ignored

查看更多
男人必须洒脱
3楼-- · 2020-02-07 16:22

You can start the Docker daemon using -g option and the directory of your choice. This sets the appropriate runtime for Docker.

With version 1.8, it should be something like:

docker daemon -g /path/to/directory

With earlier versions, it would be:

docker -d -g /path/to/directory

From the man page:

-g, --graph=""
     Path to use as the root of the Docker runtime. Default is /var/lib/docker.
查看更多
神经病院院长
4楼-- · 2020-02-07 16:24

The best solution would be to start the docker daemon (dockerd) with a correct data root path. According to the official documentation, as of Feb 2019, there are no --graph, -g options. These were renamed to the single argument --data-root.

https://docs.docker.com/engine/reference/commandline/dockerd/

So you should modify your /lib/systemd/system/docker.service so that the ExecStart takes into consideration that argument

An example could be

ExecStart=/usr/bin/dockerd --data-root /mnt/data/docker -H fd://

Then you should restart your docker daemon. (Keep in mind that you will no longer have your containers and your images, copy the data from your old folder to the new one if you want to maintain everything)

service docker restart

Keep in mind that if you restart the docker daemon your containers will be stopped, and only those with a correct restart policy will be restarted.


Tested on Ubuntu 16.04.5 Docker version 18.09.1, build 4c52b90

查看更多
Anthone
5楼-- · 2020-02-07 16:36

You can perform the following steps to modify the default docker image location, i.e /var/lib/docker:-

  1. Stop Docker

    # systemctl stop docker
    # systemctl daemon-reload
    
  2. Add the following parameters to /lib/systemd/system/docker.service.

    FROM:
    ExecStart=/usr/bin/dockerd
    TO:
    ExecStart=/usr/bin/docker daemon -g /mnt/hugedrive/docker/
    
  3. Create a new directory and rsync the current docker data to new directory.

    # mkdir /mnt/hugedrive/docker/
    # rsync -aqxP /var/lib/docker/ /mnt/hugedrive/docker/
    
  4. Now, Docker Daemon can be started safely

    # systemctl start docker
    
查看更多
Bombasti
6楼-- · 2020-02-07 16:37

Working solution as of Docker v18.03

I found @Alfabravo's comment to work in my situation, so credit to them and upvoted.

However I think it adds value to provide an answer here to elaborate on it:

Ensure docker stopped (or not started in the first place, e.g. if you've just installed it)

(e.g. as root user):

systemctl stop docker

(or you can sudo systemctl stop docker if not root but your user is a sudo-er, i.e. belongs to the sudo group)

By default, the daemon.json file does not exist, because it is optional - it is added to override the defaults. (Reference - see Answer to: Where's docker's deamon.json? (missing) )

So new installs of docker and those setups that haven't ever modified it, won't have it, so create it:

vi /etc/docker/daemon.json

And add the following to tell docker to put all its files in this folder, e.g:

{
  "graph":"/mnt/cryptfs/docker"
}

and save.

(Note: According to stackoverflow user Alireza Mohamadi's comment beneath this answer on May 11 5:01: "graph option is deprecated in v17.05.0. Use data-root instead." - I haven't tried this myself yet but will update the answer when I have)

Now start docker:

systemctl start docker

(if root or prefix with sudo if other user.)

And you will find that docker has now put all its files in the new location, in my case, under: /mnt/cryptfs/docker.

This answer from @Alfabravo is also supported by: This answer to this problem: Docker daemon flags ignored

Notes and thoughts on Docker versioning

My host platform that is running docker is Ubuntu Linux 16.04.4 LTS 64bit.

I would therefore assume that this solution would apply to later, future versions of Docker, as well as the current time of writing, v18.03. In other words: "this solution should work from v18.03 onwards". As what seems to be the case with other answers, there is also the possibility that this answer might not work for some future version of Docker, if the Docker developers decide to change things in this area. But for now, it works with v18.03, at least in my case, I hope you also find it to work for you.

Optional Housekeeping tip:

If you had files in the original location /var/lib/docker and you know yourself that you definitely don't need them anymore (i.e. you have all the data (databases inside containers, files etc) within them backed up or in another form), you can delete them, so as to keep your machine tidy.

What did NOT work - other answers here (unfortunately):

Other solutions here did not work for my situation for the current version of docker that I am using (as the time of writing, current docker version was: Docker v18.03 (current) ).

Also note (as @AlfaBravo correctly points out in their comment to my answer) that the other answers may well have worked for different or earlier versions of docker.

I should note that my host platform is Ubuntu Linux 16.04.4 LTS 64bit.

In all cases when attempting the other answers I had followed the process of stopping docker before doing the solution and then starting it up after, as required. :

  • https://stackoverflow.com/a/47604857/227926 - @Gerald Sabu M's solution to alter the /lib/systemd/system/docker.service - alter the line to: ExecStart=/usr/bin/docker daemon -g /mnt/hugedrive/docker/ - Outcome for me: docker still put its files in the default, original location: /var/lib/docker

  • I tried @Fai's comment, but that file does not exist on my system, so it would be something particular to their setup: /etc/systemd/system/docker.service.d/exec_start.conf. docker.service

  • I also tried @Hatem Jaber's answer https://stackoverflow.com/a/32072042/227926 - but again, as will @Gerald Sabu M's answer, docker still puts the files in the original default location of /var/lib/docker.

(I would of course like to thank them for their efforts, though).

Why I am changing the default docker location: encrypted file system for GDPR purposes:

As an aside, and perhaps useful to you, I'm running docker inside an encrypted file system (as part of a GDPR initiative) in order to provide Encryption of Data-at-Rest data state (also known as Encryption-at-Rest) and also for Data-In-Use) (definitions).

The process of defining a GDPR datamap includes, among many other things, looking at the systems where the sensitive data is stored (Reference 1: GDPR Data Map Template: An easy to use self-assessment tool for understanding how data moves through your organisation) (Reference 2: Data mapping: Where to start for GDPR compliance). And by encrypting the filesystem where the database and application code is stored and the swap file, risk of residual data left behind when deleting or moving a VM can be eliminated.

I've made use of some of the steps defined in the following links, credit to them:

I would note that a further step of encryption is recommended: to encrypt the database fields themselves - the sensitive fields at least - i.e. user data. You can probably find out about various levels of support for this in the implementation of popular database systems. Field encryption provides defence against malicious instrusion and leakage of data while the web application is running.

Also, as another aside point: to cover 'Data-In-Motion' state of data, I am using the free Let's Encrypt

查看更多
登录 后发表回答