【问题标题】:docker volume after upload to github?上传到github后的docker卷?
【发布时间】:2020-02-02 06:12:08
【问题描述】:

我正在使用 docker 并在容器中创建了 postgres 数据库(不在我的本地机器上)。 这是我的 docker-compose.yml 文件。我将所有代码推送到 github 并删除了笔记本电脑上的代码。但是当我克隆它时。数据库被删除,我不得不重新创建数据库。为什么它被删除,我应该怎么做才能使数据库存在。

谢谢

【问题讨论】:

  • 您似乎已将图片附加到此问题,而不是您的 docker-compose.yml 文件。你能用文件的实际文本替换图像吗?
  • 是 docker-compoes.yml 文件。我把它上传到了github,所以我从那里截屏了。

标签: django postgresql docker


【解决方案1】:

如果您只删除了包含代码的目录,则该卷可能仍然存在。

使用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。它对应于创建卷的名称。

如果我将撰写文件复制到另一个名为 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 的所有卷进行 greping 我得到:

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 服务附加到现有卷而不创建新卷。

【讨论】:

  • 感谢您的解释。所以我完全删除了代码文件夹,因为我已经将我的代码推送到了 github。当我将所有内容克隆并 cd 进入该文件夹时(如您所说,它与以前的名称相同,它没有那个数据库。还有其他原因导致它没有重新创建我的旧数据库吗?
  • docker volume ls 的输出是什么?有 2 个 postgres 卷吗?
  • 是的,它确实创建了两个卷。我不明白我在哪里犯了错误。所以我所做的是从 github 和 cd 下载代码到项目文件夹中。我做对了吗?这就是你给我的建议对吗?
  • 还有另一个问题。如果我想将其公开以便可以访问,那将如何工作。我们如何做到这一点?是卷还是任何云服务
  • 是的,cd 进入它很好。公开什么?音量?还是 docker 镜像?
猜你喜欢
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多