Eric.M
Eric.M

Reputation: 857

Moved docker-compose.yml creates a new postgres database

I have set up a Postgres database on docker on ubuntu with the docker-compose.yml just for that database within the folder ~/postgres and I'd run docker-compose up -d to run my database from within the ~/postgres folder.

Here is my docker-compose.yml:

version: '3'
services:
  database:
    image: "postgres"
    ports:
      - "0.0.0.0:5432:5432"
    env_file:
      - database.env
    volumes:
      - database-data:/var/lib/postgresql/data/

volumes:
  database-data:

This database is set up and working perfectly, so I decided to set up my web application as well and, because the docker-compose.yml file was inside that folder, I moved it outside to ~/ so I could use it for my web app as well.

This is what the docker-compose.yml in ~/ looks like:

version: "3"
services:
  database:
    image: "postgres"
    ports:
      - "0.0.0.0:5432:5432"
    env_file:
      - postgres/database.env
    volumes:
      - database-data:/var/lib/postgresql/data/
  webapp:
    image: webapp/site
    build:
      context: ./retro-search-engine
      dockerfile: Dockerfile
      args:
        buildno: 1
    links:
      - "database:db"
    ports:
      - "0.0.0.0:8000:80"
    volumes:
      - webapp:/var/www
    environment:
      db_host: db
      db_username: xxxx
      db_password: xxxx
      db_database: xxxx
      db_port: 5432

volumes:
  database-data:
  webapp:

As you can see, the database docker configuration is basically the same, the only thing that changes is the path to the database.env file since it's still in the previous folder.

So, the problem here is that when I run docker-compose up -d from ~/, everything starts normally but when I access the database, all of my tables are gone.

If I go back to ~/postgres and do docker-compose up -d in that folder (with the previous docker-compose.yml) and connect to the db, I can access my tables.

So what I think is happening is that it's either creating a new container or somehow the folder where the data is stored is relative to the docker-compose.yml file and it's creating a new database because it can't find the old files.

I have no idea how to solve this issue, I have googled around and couldn't find anything so I decided to ask here before I dump my whole db and restore it into a new container, which I don't want to do because it's a 16gb database and it's gonna take forever.

Does anyone have any idea how I can use my new docker-compose.yml with the data from my database?

Thanks in advance.

Upvotes: 0

Views: 305

Answers (1)

blackbird
blackbird

Reputation: 146

First:

  • Replace : postgres/database.env by ./postgres/database.env
  • Use docker compose up --build : it will rebuild the image (usefull if you made some change to your dockerfile). try to avoid to use -d when developing, you'll avoid to have tons of container running.

Second:

I suggest you to follow the following reco, It will resolve your problem and it will be cleaner if you want to use a pipeline CI/CD and to create more "autonomous" image and container on demand.

rootfolder
|-docker-compose.yaml
|-postgres/
|  |--All_other_files_for_the_postgres_docker_image
|-webapp/
   |-- Dockerfile
   |-- All_other_files_for_the_webapp_docker_image

bellow you will find my "correction" :

version: "3"
services:
  database:
    image: "postgres"
    container_name: "my_postgres_container"
    ports:
      - "0.0.0.0:5432:5432"
    env_file:
      - ./postgres/database.env
    volumes:
      - database-data:/var/lib/postgresql/data/
  webapp:
    image: webapp/site
    container_name: "my_webapp_container"
    build:
      context: ./retro-search-engine
      dockerfile: Dockerfile
    links:
      - "database:db"
    ports:
      - "0.0.0.0:8000:80"
    volumes:
      - webapp:/var/www
    environment:
      db_host: db
      db_username: xxxx
      db_password: xxxx
      db_database: xxxx
      db_port: 5432

volumes:
  database-data:
  webapp:

If you want to use an existing postgres image that is already present (to see if an image already existe you can do : docker image | grep postgres)

then you can do directly in your docker-compose : image: "<your_image_name>"

Upvotes: 1

Related Questions