I am using docker and created the postgres db within the container( not on my local machine). This is my docker-compose.yml file. I pushed all my code to github and deleted my code on my laptop. 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.
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
:
version: "3"
services:
db:
image: postgres
volumes:
- postgres_data:/var/lib/postresql/data
volumes:
postgres_data:
inside a directory called postgres2342
leads to:
~/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. 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:
~/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. So greping for all volumes containing the substring postgres
I get:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.