Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker Learning 6: change the location of the Docker default image and container in Centos7

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Repost address: https://www.cnblogs.com/hanyifeng/p/6837512.html?utm_source=itdadao&utm_medium=referral

1. Why?

Usually, when you start using docker, we don't care much about the default directory that Docker uses to store images and containers. When you do more experiments with Docker and Docker starts to take up a lot of space, you have to press the emergency button. So it's time to put troubleshooting on how to change the default storage location of docker. After spending a lot of time browsing many websites and forums, I was not sure about the steps to change the default directory of Docker images and containers on the Centos7 host. This article provides instructions specific to Centos7 (it also applies to RHEL7). For Debian, I can find a lot of documentation and "how to" post articles on the Internet, but Centos7 doesn't have much.

2. How?

When trying to change the default storage location of Docker, we must know some important information.

? What is the default storage location currently used by Docker? Which storage driver is currently used by Docker? New storage space for images and containers

The default location of Docker is / var/lib/docker, where all current images and containers are stored. If you have any containers that are running, stop them and make sure there are no containers running, and then run the following command to determine the storage driver currently used by Docker. (you will find out why you want to record it in a later article)

# docker info

In the output, look for the line Storage Driver and write it down. On my mainframe is devicemapper. The next step is to stop the Docker service

# sudo systemctl stop docker

Create a Drop-In file

The next step is to create a Drop-In file "docker.conf" in the / etc/systemd/system/docker.service.d directory. The default docker.service.d folder does not exist. So you have to create it first.

# sudo mkdir / etc/systemd/system/docker.service.d# sudo touch / etc/systemd/system/docker.service.d/docker.conf

The reason for creating the Drop-In file is that we want the Docker service to override the parameters in the / lib/systemd/system/docker.service file used by the default service with the specific parameters mentioned in the docker.conf file. If you want to learn more about Drop-In, please read the system.unit documentation

Define a new storage location now open docker.conf and add the following:

# sudo vi / etc/systemd/system/docker.service.d/ docker.confs [Service] ExecStart= ExecStart=/usr/bin/dockerd-- graph= "/ mnt/new_volume"-- storage-driver=devicemapper

Save and exit the VI editor, / mnt/new_volume is the new storage location, and devicemapper is the storage driver used by the current docker. If your storage driver is different, please enter the value you viewed and noted in the first step before. More information about various storage drives is provided in the official Docker documentation. Now you can reload the service daemon and start the Docker service. This will change the storage location of the new images and containers.

# sudo systemctl daemon-reload# sudo systemctl start docker

To make sure everything is going well, run the # docker info command to check the root directory of Docker. It will be changed to / mnt/new_volume

What if you already have containers and images that exist?

If you want to migrate existing containers and images to a new location, do not reload the daemon daemon and start the docker service after modifying the docker.conf. Move the data that already exists in / var/lib/docker to a new location Then create a symbolic link.

Note: I haven't tried the following, because I don't need to keep the existing containers and images, but these steps should work; if you encounter any problems and any other alternatives, please comment below. I will modify the post. Please back up your data before you are ready to take risks.

# cp-rp / var/lib/docker / mnt/new_volume

Create a soft link (translator's note: before creating a soft chain, please change the original / var/lib/docker directory to another name, such as / var/lib/docker-backup)

# mv / var/lib/docker / var/lib/docker-backup-2017-051 ln-s / mnt/new_volume/docker / var/lib/docker

Then reload the daemon daemon and start the docker service. (translator's note: there is no need to reload the daemon daemon here, just start the docker service)

# sudo systemctl daemon-reload # sudo systemctl start docker

The existing data should be in the source directory of the soft chain, and the new containers and images will be stored in the new location, / mnt/new_volume/docker, run # docker info to confirm.

(translator's note: if the steps are correct, the root directory should point to the source directory / mnt/new_volume/docker of the soft link, as follows:)

... Omit the output Name: dockerID: 5WBA:EF4D:WQ7P:DVRN:JCI4:LWDT:XSR2:G7RE:F5TI:PD3B:A57K:E4QADocker Root Dir: / mnt/new_volume/docker... Omit output

Remarks: the typeface with italics and horizontal lines in the text means that there is something wrong with the author of the original text (English is not good, if you misunderstand the meaning of the original text, please point it out in time, please do not hesitate to comment), there are comments on the relevant positions.

This article belongs to the original translation, welcome to reprint, please note the original English link before reprinting. You can't fly away.

Link to English original text:

Https://sanenthusiast.com/tag/docker-root-directory/

Reference:

Http://www.infoq.com/cn/articles/analysis-of-docker-file-system-aufs-and-devicemapper

Http://coolshell.cn/articles/17200.html

Everyone should be a watcher, watching our minds, our ideals, in case they unwittingly fall, manipulated and forgotten in life.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report