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?
docker docker-compose
add a comment |
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?
docker docker-compose
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 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
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
add a comment |
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?
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 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 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
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
add a comment |
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 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
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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 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
add a comment |
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
add a comment |
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
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 getFATAL: 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