Skip to content

Can't run scripts with user in /docker-entrypoint-initdb.d #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robertdecaire opened this issue Jun 17, 2022 · 1 comment
Closed

Can't run scripts with user in /docker-entrypoint-initdb.d #973

robertdecaire opened this issue Jun 17, 2022 · 1 comment
Labels
question Usability question, not directly related to an error with the image

Comments

@robertdecaire
Copy link

I've got a docker-compose file where I include the following:

services:
    postgres-12:
        image: postgres:12-alpine
        user: root
        volumes:
          - "${PWD}/init-script.sh:/docker-entrypoint-initdb.d/init-script.sh"

I'm expecting that is will run this script as root, but the very first line of the script is:

apk update

And this results in:

postgres-12_1        | ERROR: Unable to lock database: Permission denied
postgres-12_1        | ERROR: Failed to open apk database: Permission denied

I've seen this issue: #321 and it seems to suggest, along with the documentation on the image page, that I should be able to run scripts as the user I specify. However, it doesn't seem to be respecting this. I can run these commands without issue if I start the container without mounting the script and then ssh in to get a terminal prompt, but I want them to run automatically on first startup. The purpose is to download and install an extension via its makefile so that I don't have to manually install it every time I spin up a new db. So the script contains some apk commands to update and get dependencies (e.g. gcc) to install the extension.

@yosifkit
Copy link
Member

yosifkit commented Jun 21, 2022

Additionally, as of docker-library/postgres#253, these initialization scripts are run as the postgres user (or as the "semi-arbitrary user" specified with the --user flag to docker run;

- https://github.com/docker-library/docs/blob/3f614f38145dbf4f31cd3a99bd6f87a3c3c3487d/postgres/README.md#initialization-scripts

They cannot be run as root, as the entrypoint script steps down after initial db directory creation & permissions:

# setup data directories and permissions (when run as root)
docker_create_db_directories
if [ "$(id -u)" = '0' ]; then
# then restart script as postgres user
exec gosu postgres "$BASH_SOURCE" "$@"
fi


Edit: You should probably just be using a Dockerfile to create a custom image FROM postgres:[version] with the needed extension since starting a new container with an already initialized database will result in no extension.

@yosifkit yosifkit closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2022
@wglambert wglambert added the question Usability question, not directly related to an error with the image label Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

3 participants