溫馨提示×

怎么在Docker容器內(nèi)外互相拷貝數(shù)據(jù)

小億
216
2024-06-11 18:56:45
欄目: 智能運維

在Docker容器內(nèi)外互相拷貝數(shù)據(jù)可以通過以下兩種方法實現(xiàn):

  1. 使用docker cp命令:可以使用docker cp命令將數(shù)據(jù)從主機復(fù)制到容器內(nèi)或從容器內(nèi)復(fù)制到主機。例如,將本地文件/path/to/file復(fù)制到容器container_name/path/to/destination目錄中:
docker cp /path/to/file container_name:/path/to/destination

或?qū)⑷萜?code>container_name中的文件/path/to/file復(fù)制到本地主機的/path/to/destination目錄中:

docker cp container_name:/path/to/file /path/to/destination
  1. 使用數(shù)據(jù)卷:可以通過掛載數(shù)據(jù)卷的方式在容器內(nèi)外共享數(shù)據(jù)。首先需要創(chuàng)建一個數(shù)據(jù)卷,然后將數(shù)據(jù)卷掛載到容器中。例如,創(chuàng)建一個數(shù)據(jù)卷my_volume
docker volume create my_volume

然后將數(shù)據(jù)卷my_volume掛載到容器container_name中的/path/to/destination目錄:

docker run -v my_volume:/path/to/destination container_name

這樣,在主機和容器之間就可以共享數(shù)據(jù)了。

0