Docker-compose mount postgress database for tests











up vote
0
down vote

favorite












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
    2 hours ago












  • why is named volume problematic to be used with a git repo?
    – Siyu
    2 hours ago










  • @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
    1 hour ago












  • 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
    1 hour ago










  • @ChrisStryczynski you mean changing owner of directory on host?
    – K. Kowalczyk
    1 hour ago















up vote
0
down vote

favorite












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
    2 hours ago












  • why is named volume problematic to be used with a git repo?
    – Siyu
    2 hours ago










  • @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
    1 hour ago












  • 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
    1 hour ago










  • @ChrisStryczynski you mean changing owner of directory on host?
    – K. Kowalczyk
    1 hour ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 4 hours ago









K. Kowalczyk

428316




428316












  • Change the owner/group of the file (chown)?
    – Chris Stryczynski
    2 hours ago












  • why is named volume problematic to be used with a git repo?
    – Siyu
    2 hours ago










  • @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
    1 hour ago












  • 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
    1 hour ago










  • @ChrisStryczynski you mean changing owner of directory on host?
    – K. Kowalczyk
    1 hour ago


















  • Change the owner/group of the file (chown)?
    – Chris Stryczynski
    2 hours ago












  • why is named volume problematic to be used with a git repo?
    – Siyu
    2 hours ago










  • @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
    1 hour ago












  • 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
    1 hour ago










  • @ChrisStryczynski you mean changing owner of directory on host?
    – K. Kowalczyk
    1 hour ago
















Change the owner/group of the file (chown)?
– Chris Stryczynski
2 hours ago






Change the owner/group of the file (chown)?
– Chris Stryczynski
2 hours ago














why is named volume problematic to be used with a git repo?
– Siyu
2 hours ago




why is named volume problematic to be used with a git repo?
– Siyu
2 hours ago












@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
1 hour ago






@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
1 hour ago














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
1 hour ago




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
1 hour ago












@ChrisStryczynski you mean changing owner of directory on host?
– K. Kowalczyk
1 hour ago




@ChrisStryczynski you mean changing owner of directory on host?
– K. Kowalczyk
1 hour ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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
    1 hour ago










  • Possibly as a work around you could docker exec into the container and run the chown from within the container.
    – Chris Stryczynski
    1 hour ago










  • 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
    38 mins ago













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',
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








up vote
0
down vote













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
    1 hour ago










  • Possibly as a work around you could docker exec into the container and run the chown from within the container.
    – Chris Stryczynski
    1 hour ago










  • 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
    38 mins ago

















up vote
0
down vote













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
    1 hour ago










  • Possibly as a work around you could docker exec into the container and run the chown from within the container.
    – Chris Stryczynski
    1 hour ago










  • 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
    38 mins ago















up vote
0
down vote










up vote
0
down vote









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 1 hour ago









Chris Stryczynski

3,23612552




3,23612552












  • That won't work on Windows host tho.
    – K. Kowalczyk
    1 hour ago










  • Possibly as a work around you could docker exec into the container and run the chown from within the container.
    – Chris Stryczynski
    1 hour ago










  • 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
    38 mins ago




















  • That won't work on Windows host tho.
    – K. Kowalczyk
    1 hour ago










  • Possibly as a work around you could docker exec into the container and run the chown from within the container.
    – Chris Stryczynski
    1 hour ago










  • 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
    38 mins ago


















That won't work on Windows host tho.
– K. Kowalczyk
1 hour ago




That won't work on Windows host tho.
– K. Kowalczyk
1 hour ago












Possibly as a work around you could docker exec into the container and run the chown from within the container.
– Chris Stryczynski
1 hour ago




Possibly as a work around you could docker exec into the container and run the chown from within the container.
– Chris Stryczynski
1 hour ago












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
38 mins ago






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
38 mins ago




















 

draft saved


draft discarded



















































 


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







Popular posts from this blog

Volksrepublik China

How to test boost logger output in unit testing?

Write to the output between two pipeline