简体   繁体   English

上传到github后的docker卷?

[英]docker volume after upload to github?

在此处输入图片说明

I am using docker and created the postgres db within the container( not on my local machine).我正在使用 docker 并在容器内(不在我的本地机器上)创建了 postgres 数据库。 This is my docker-compose.yml file.这是我的 docker-compose.yml 文件。 I pushed all my code to github and deleted my code on my laptop.我将所有代码推送到 github 并删除了笔记本电脑上的代码。 But when i clone it.但是当我克隆它时。 The database was deleted and I had to re create database.数据库被删除了,我不得不重新创建数据库。 Why was it deleted and what should i do so that the db exists.为什么它被删除了,我应该怎么做才能使数据库存在。

Thanks谢谢

If you only deleted the directories with your code, chances are the volume still exists.如果您只使用代码删除了目录,则该卷可能仍然存在。

When using docker-compose , the name of the parent directory of your docker-compose.yml gets prepended to the volume name.使用docker-compose ,您docker-compose.yml的父目录的名称被添加到卷名称之前。

So you have to make sure that the directory structure doesn't change if you want to reuse the old volume.因此,如果要重用旧卷,则必须确保目录结构不会更改。

For instance, using the following docker-compose.yml :例如,使用以下docker-compose.yml

version: "3"

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

volumes:
  postgres_data:

inside a directory called postgres2342 leads to:在名为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

Notice the Creating volume "postgres2342_postgres_data" with default driver in the beginning and the Attaching to postgres2342_db_1 in the last line.请注意Creating volume "postgres2342_postgres_data" with default driver的开头和最后一行的Attaching to postgres2342_db_1 Creating volume "postgres2342_postgres_data" with default driver It corresponds to the name of the created volume.它对应于创建的卷的名称。

If I copy the compose file into another directory called another_postgres and spawn the service this leads to:如果我将撰写文件复制到另一个名为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

As you can see, the name of the volume changed.如您所见,卷的名称已更改。

With docker volume ls you can display all docker volumes on your system.使用docker volume ls您可以显示系统上的所有 docker 卷。 So greping for all volumes containing the substring postgres I get:所以对包含子字符串postgres所有卷进行 grep 我得到:

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

However, if I remove the directory and recreate it without changing its name everything goes fine:但是,如果我删除目录并重新创建它而不更改其名称,则一切正常:

~/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

It attaches the postgres service to the existent volume without creating a new one.它将 postgres 服务附加到现有卷而不创建新卷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM