簡體   English   中英

上傳到github后的docker卷?

[英]docker volume after upload to github?

在此處輸入圖片說明

我正在使用 docker 並在容器內(不在我的本地機器上)創建了 postgres 數據庫。 這是我的 docker-compose.yml 文件。 我將所有代碼推送到 github 並刪除了筆記本電腦上的代碼。 但是當我克隆它時。 數據庫被刪除了,我不得不重新創建數據庫。 為什么它被刪除了,我應該怎么做才能使數據庫存在。

謝謝

如果您只使用代碼刪除了目錄,則該卷可能仍然存在。

使用docker-compose ,您docker-compose.yml的父目錄的名稱被添加到卷名稱之前。

因此,如果要重用舊卷,則必須確保目錄結構不會更改。

例如,使用以下docker-compose.yml

version: "3"

services:
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postresql/data

volumes:
  postgres_data:

在名為postgres2342的目錄中導致:

~/workspace/teststuff/postgres2342$ docker-compose up
Creating network "postgres2342_default" with the default driver
Creating volume "postgres2342_postgres_data" with default driver
Pulling db (postgres:)...
latest: Pulling from library/postgres
8ec398bc0356: Pull complete
65a7b8e7c8f7: Pull complete
b7a5676ed96c: Pull complete
3e0ac8617d40: Pull complete
633091ee8d02: Pull complete
b01fa9e356ea: Pull complete
4cd472257298: Pull complete
1716325d7dcd: Pull complete
9b625d69c7c8: Pull complete
74d8b4d9818c: Pull complete
c36f5edbeb97: Pull complete
9b38bb0fb36e: Pull complete
6b5ee1c74b9a: Pull complete
5fcc518252b4: Pull complete
Digest: sha256:52579625addc98a371a644c0399f37e0908b6c28d3b68a0e418394adbe0eb699
Status: Downloaded newer image for postgres:latest
Creating postgres2342_db_1 ... done
Attaching to postgres2342_db_1

請注意Creating volume "postgres2342_postgres_data" with default driver的開頭和最后一行的Attaching to postgres2342_db_1 Creating volume "postgres2342_postgres_data" with default driver 它對應於創建的卷的名稱。

如果我將撰寫文件復制到另一個名為another_postgres目錄中並生成服務,這將導致:

~/workspace/teststuff/postgres2342$ cd ..
~/workspace/teststuff$ mkdir another_postgres
~/workspace/teststuff$ cp postgres2342/docker-compose.yml another_postgres/
~/workspace/teststuff$ cd another_postgres/
~/workspace/teststuff/another_postgres$ docker-compose up -d
Creating network "another_postgres_default" with the default driver
Creating volume "another_postgres_postgres_data" with default driver
Creating another_postgres_db_1 ... done

如您所見,卷的名稱已更改。

使用docker volume ls您可以顯示系統上的所有 docker 卷。 所以對包含子字符串postgres所有卷進行 grep 我得到:

apoehlmann:~/workspace/teststuff/another_postgres$ docker volume ls | grep postgres
local               another_postgres_postgres_data
local               postgres2342_postgres_data

但是,如果我刪除目錄並重新創建它而不更改其名稱,則一切正常:

~/workspace/teststuff/another_postgres$ cd ..
~/workspace/teststuff$ rm -r postgres2342/
~/workspace/teststuff$ mkdir postgres2342
~/workspace/teststuff$ cp another_postgres/docker-compose.yml postgres2342/
~/workspace/teststuff$ cd postgres2342/
~/workspace/teststuff/postgres2342$ docker-compose up
Starting postgres2342_db_1 ... done
Attaching to postgres2342_db_1

它將 postgres 服務附加到現有卷而不創建新卷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM