0

I have a problem with my database container, so I want to mount an existant database(from host) to the generated container (with docker-compose). I will show you the important part of the yaml file :

postgres:
        image: postgres
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: pass
        ports:
        - "5432:5432"
        volumes:
        - ./postgres/data:/var/lib/postgresql/data

So, when I build and turn on docker-compose, postgres container exited:

enter image description here

If I remove the persistant volume, it wokrs ! I have also tested with mysql, mongodb -> same issue.

Could you light me up ?

Thanks.

2
  • 2
    First thing to do is to check the logs (docker logs <container-id>). There may be something interesting to explain what's happening there. Otherwise, I usually try to run the image using a shell and then try to figure out what's wrong. In this case, you could run docker run -v data:/var/lib/postgresql/data -p 5432:5432 -e POSTGRES_USER=user -e POSTGRES_PASSWORD=pass --rm -it postgres bash From there you can try to start the postgres daemon just as if docker were not involved, and at that point your question becomes a postgres question (rather than a docker one). Commented Nov 16, 2018 at 16:50
  • Thanks for your contribution, I have just shared the way out of this problem. Commented Nov 20, 2018 at 11:19

2 Answers 2

2

Easiest way is to named volumes that can also be reused across multiple services, and are easily retrieved and inspected using the docker command line...

  1. First create volume docker volume create db-data
  2. Now modify your yml file
    services
        postgres:
            image: postgres
            environment:
                POSTGRES_USER: user
                POSTGRES_PASSWORD: pass
            ports:
            - "5432:5432"
            volumes:
            - db-data:/var/lib/postgresql/data
    volumes:
       db-data:
  1. You can inspect the volume docker volume inspect db-data

Source: Compose file reference

Sign up to request clarification or add additional context in comments.

Comments

1

I resolve this matter and I want to share it. In fact, for getting our container turns on after ' docker-compose up ' command, we should add tty and command arguments like :

postgres:
        image: postgres
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: pass
        ports:
        - "5432:5432"
        volumes:
        - ./postgres/data:/var/lib/postgresql/data
        tty: true
        command: /bin/bash

I hope that would be useful.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.