Search code examples
dockernetwork-programmingvolumeswarm

Docker suddenly removing volume when adding network to container


I have database container in swarm network. It has its own node and it is not changing.

The unexpected behavior has started when I add or remove some network for my db container:

For example, I added traefik network:

my_db:
  volumes:
    db1:/var/lib/postgresql
  networks:
    - my_project
    - traefik # added

Then I run docker swarm deploy and my database is empty :(

The same behavior is when I remove this network. Both networks are overlay. I tested also on another swarm (which has only one node) and another project - the behavior is the same - database erased.

I checked volumes one node with the only db and portainer which is deployed globally:

docker ps
CONTAINER ID        IMAGE                    
16a78f8b609d        postgres:11.3 
b71166ae4601        portainer/agent:latest 

docker volume ls
DRIVER              VOLUME NAME
local               4c3bd578714937e3a5f97e9c5a659a36ad7b9fd0921f57a303f33189275bc997
local               0252ae24cb58f0bd3fec93660f2d09d339d7d579b6d0f9fee5b49e7def9b288b
local               413363adb68602ef02928462e33c53d083a1c507af32c6312632bac68b23cd0a
local               c6b042815270a5d37aa710923f69aae9e4ace7c0a0e3a402d0ed3f267acc1923
local               cf803014263516e023a208537658c4e7d79d61b5e42fb84d696dfa64fda65c78
local               my-project_db1

It seems that older data is in first volumes with hash volume name, and my-project_db1 is recreated when network is added/removed.

I also tried using name in volumes section - the same.


Solution

  • Problem has been solved by using answer from: How to persist data in a dockerized postgres database using volumes

    Adding data in db1:/var/lib/postgresql/data solved this behavior