Docker-compose mount postgress database for tests
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
add a comment |
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
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 addedRUN mkdir -p "$PGDATA" && chown postgres "$PGDATA"
(PGDATA set to/var/lib/postgresql/data
) I still getFATAL: 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
add a comment |
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
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
docker docker-compose
asked Nov 19 '18 at 8:12
K. KowalczykK. Kowalczyk
443316
443316
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 addedRUN mkdir -p "$PGDATA" && chown postgres "$PGDATA"
(PGDATA set to/var/lib/postgresql/data
) I still getFATAL: 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
add a comment |
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 addedRUN mkdir -p "$PGDATA" && chown postgres "$PGDATA"
(PGDATA set to/var/lib/postgresql/data
) I still getFATAL: 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
add a comment |
1 Answer
1
active
oldest
votes
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
.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
answered Nov 19 '18 at 11:00
Chris StryczynskiChris Stryczynski
3,81953062
3,81953062
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 getFATAL: 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