Docker-compose mount postgress database for tests












0















I'm trying to put together a compose file for my development environment, but I'm having problems finding some convinient way to setup a database filled with test data. I tried mounting directory from my project as a data folder for postgres container, but it mounts as root and postgres throws:



data directory “/var/lib/postgresql/data/pgdata” has wrong ownership


Named volume works, but it would be problematic to use it with a git repo.



I could also just copy data directly into a docker image, but then I'd have to rebuild it whenever data changes.



Is there any other way around this?










share|improve this question























  • Change the owner/group of the file (chown)?

    – Chris Stryczynski
    Nov 19 '18 at 10:15













  • why is named volume problematic to be used with a git repo?

    – Siyu
    Nov 19 '18 at 10:20











  • @ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

    – K. Kowalczyk
    Nov 19 '18 at 10:55













  • You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

    – Chris Stryczynski
    Nov 19 '18 at 10:56











  • @ChrisStryczynski you mean changing owner of directory on host?

    – K. Kowalczyk
    Nov 19 '18 at 10:59
















0















I'm trying to put together a compose file for my development environment, but I'm having problems finding some convinient way to setup a database filled with test data. I tried mounting directory from my project as a data folder for postgres container, but it mounts as root and postgres throws:



data directory “/var/lib/postgresql/data/pgdata” has wrong ownership


Named volume works, but it would be problematic to use it with a git repo.



I could also just copy data directly into a docker image, but then I'd have to rebuild it whenever data changes.



Is there any other way around this?










share|improve this question























  • Change the owner/group of the file (chown)?

    – Chris Stryczynski
    Nov 19 '18 at 10:15













  • why is named volume problematic to be used with a git repo?

    – Siyu
    Nov 19 '18 at 10:20











  • @ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

    – K. Kowalczyk
    Nov 19 '18 at 10:55













  • You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

    – Chris Stryczynski
    Nov 19 '18 at 10:56











  • @ChrisStryczynski you mean changing owner of directory on host?

    – K. Kowalczyk
    Nov 19 '18 at 10:59














0












0








0








I'm trying to put together a compose file for my development environment, but I'm having problems finding some convinient way to setup a database filled with test data. I tried mounting directory from my project as a data folder for postgres container, but it mounts as root and postgres throws:



data directory “/var/lib/postgresql/data/pgdata” has wrong ownership


Named volume works, but it would be problematic to use it with a git repo.



I could also just copy data directly into a docker image, but then I'd have to rebuild it whenever data changes.



Is there any other way around this?










share|improve this question














I'm trying to put together a compose file for my development environment, but I'm having problems finding some convinient way to setup a database filled with test data. I tried mounting directory from my project as a data folder for postgres container, but it mounts as root and postgres throws:



data directory “/var/lib/postgresql/data/pgdata” has wrong ownership


Named volume works, but it would be problematic to use it with a git repo.



I could also just copy data directly into a docker image, but then I'd have to rebuild it whenever data changes.



Is there any other way around this?







docker docker-compose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 8:12









K. KowalczykK. Kowalczyk

443317




443317













  • Change the owner/group of the file (chown)?

    – Chris Stryczynski
    Nov 19 '18 at 10:15













  • why is named volume problematic to be used with a git repo?

    – Siyu
    Nov 19 '18 at 10:20











  • @ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

    – K. Kowalczyk
    Nov 19 '18 at 10:55













  • You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

    – Chris Stryczynski
    Nov 19 '18 at 10:56











  • @ChrisStryczynski you mean changing owner of directory on host?

    – K. Kowalczyk
    Nov 19 '18 at 10:59



















  • Change the owner/group of the file (chown)?

    – Chris Stryczynski
    Nov 19 '18 at 10:15













  • why is named volume problematic to be used with a git repo?

    – Siyu
    Nov 19 '18 at 10:20











  • @ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

    – K. Kowalczyk
    Nov 19 '18 at 10:55













  • You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

    – Chris Stryczynski
    Nov 19 '18 at 10:56











  • @ChrisStryczynski you mean changing owner of directory on host?

    – K. Kowalczyk
    Nov 19 '18 at 10:59

















Change the owner/group of the file (chown)?

– Chris Stryczynski
Nov 19 '18 at 10:15







Change the owner/group of the file (chown)?

– Chris Stryczynski
Nov 19 '18 at 10:15















why is named volume problematic to be used with a git repo?

– Siyu
Nov 19 '18 at 10:20





why is named volume problematic to be used with a git repo?

– Siyu
Nov 19 '18 at 10:20













@ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

– K. Kowalczyk
Nov 19 '18 at 10:55







@ChrisStryczynski I created Dockerfile for postgres and added RUN mkdir -p "$PGDATA" && chown postgres "$PGDATA" (PGDATA set to /var/lib/postgresql/data) I still get FATAL: data directory "/var/lib/postgresql/data" has wrong ownership I guess ownership changes when compose mounts volume.

– K. Kowalczyk
Nov 19 '18 at 10:55















You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

– Chris Stryczynski
Nov 19 '18 at 10:56





You need to do the chown on the directory you mount - not the directory within the container (dockerfile). The mounted directory overrides whatever is in the container.

– Chris Stryczynski
Nov 19 '18 at 10:56













@ChrisStryczynski you mean changing owner of directory on host?

– K. Kowalczyk
Nov 19 '18 at 10:59





@ChrisStryczynski you mean changing owner of directory on host?

– K. Kowalczyk
Nov 19 '18 at 10:59












1 Answer
1






active

oldest

votes


















0














You'll need to determine the userid of the postgresql user, this can be done with id -u postgres (within the container).



Replace 1234 with the id you get from the above.



Then on the volume that you mount, from the host you need to do chown -R 1234:1234 path/to/volume/that/you/are/mounting.






share|improve this answer
























  • That won't work on Windows host tho.

    – K. Kowalczyk
    Nov 19 '18 at 11:07











  • Possibly as a work around you could docker exec into the container and run the chown from within the container.

    – Chris Stryczynski
    Nov 19 '18 at 11:10











  • Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

    – K. Kowalczyk
    Nov 19 '18 at 11:54













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370612%2fdocker-compose-mount-postgress-database-for-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You'll need to determine the userid of the postgresql user, this can be done with id -u postgres (within the container).



Replace 1234 with the id you get from the above.



Then on the volume that you mount, from the host you need to do chown -R 1234:1234 path/to/volume/that/you/are/mounting.






share|improve this answer
























  • That won't work on Windows host tho.

    – K. Kowalczyk
    Nov 19 '18 at 11:07











  • Possibly as a work around you could docker exec into the container and run the chown from within the container.

    – Chris Stryczynski
    Nov 19 '18 at 11:10











  • Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

    – K. Kowalczyk
    Nov 19 '18 at 11:54


















0














You'll need to determine the userid of the postgresql user, this can be done with id -u postgres (within the container).



Replace 1234 with the id you get from the above.



Then on the volume that you mount, from the host you need to do chown -R 1234:1234 path/to/volume/that/you/are/mounting.






share|improve this answer
























  • That won't work on Windows host tho.

    – K. Kowalczyk
    Nov 19 '18 at 11:07











  • Possibly as a work around you could docker exec into the container and run the chown from within the container.

    – Chris Stryczynski
    Nov 19 '18 at 11:10











  • Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

    – K. Kowalczyk
    Nov 19 '18 at 11:54
















0












0








0







You'll need to determine the userid of the postgresql user, this can be done with id -u postgres (within the container).



Replace 1234 with the id you get from the above.



Then on the volume that you mount, from the host you need to do chown -R 1234:1234 path/to/volume/that/you/are/mounting.






share|improve this answer













You'll need to determine the userid of the postgresql user, this can be done with id -u postgres (within the container).



Replace 1234 with the id you get from the above.



Then on the volume that you mount, from the host you need to do chown -R 1234:1234 path/to/volume/that/you/are/mounting.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 11:00









Chris StryczynskiChris Stryczynski

4,12153066




4,12153066













  • That won't work on Windows host tho.

    – K. Kowalczyk
    Nov 19 '18 at 11:07











  • Possibly as a work around you could docker exec into the container and run the chown from within the container.

    – Chris Stryczynski
    Nov 19 '18 at 11:10











  • Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

    – K. Kowalczyk
    Nov 19 '18 at 11:54





















  • That won't work on Windows host tho.

    – K. Kowalczyk
    Nov 19 '18 at 11:07











  • Possibly as a work around you could docker exec into the container and run the chown from within the container.

    – Chris Stryczynski
    Nov 19 '18 at 11:10











  • Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

    – K. Kowalczyk
    Nov 19 '18 at 11:54



















That won't work on Windows host tho.

– K. Kowalczyk
Nov 19 '18 at 11:07





That won't work on Windows host tho.

– K. Kowalczyk
Nov 19 '18 at 11:07













Possibly as a work around you could docker exec into the container and run the chown from within the container.

– Chris Stryczynski
Nov 19 '18 at 11:10





Possibly as a work around you could docker exec into the container and run the chown from within the container.

– Chris Stryczynski
Nov 19 '18 at 11:10













Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

– K. Kowalczyk
Nov 19 '18 at 11:54







Not possible since container is stopped after fatal is rised. I could theorethically write bash script to switch ownership and use it as entrypoint, but thats starting to get messy.

– K. Kowalczyk
Nov 19 '18 at 11:54






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370612%2fdocker-compose-mount-postgress-database-for-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()