Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied

I have the following docker compose file:

version: "3"
services:
  postgres:
    image: postgres:11.2-alpine
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER: root
    ports:
      - "5432:5432"
    volumes:
      - ./init-db/init-db.sql:/docker-entrypoint-initdb.d/init.sql

This is the init-db.sql:

CREATE TABLE users (
    email VARCHAR(355) UNIQUE NOT NULL,
    password VARCHAR(256) NOT NULL
);

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    title VARCHAR(100) NOT NULL,
    price NUMERIC(6, 2) NOT NULL,
    category INT NOT NULL
);

INSERT INTO users VALUES ('[email protected]', 'Test*123');
INSERT INTO products (title, price, category) VALUES ('Truco', 9.90, 13);

When I run docker-compose up, I'm getting this error:

server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
/docker-entrypoint-initdb.d/init.sql: Permission denied

I already tried to:

  • chmod 777 on the sql file
  • chmod -x on the sql file
  • Run docker and docker-compose using sudo

Any idea?

like image 214
Ben-hur Ott Avatar asked Feb 28 '20 19:02

Ben-hur Ott


3 Answers

I had a similar problem when using the ADD command.

When using ADD (eg to download a file) the default chmod value is 711. When using the COPY command the chmod will match the hosts chmod of the file where you copy it from. The solution is to set the permission before copying, or change them in your Dockerfile after they've been copied.

It looks like there will finally be a "COPY --chmod 775" flag available in the upcoming docker 20 which will make this easier.

https://github.com/moby/moby/issues/34819

When the sql file in /docker-entrypoint-initdb.d/ has a permission of 775 then the file is run correctly.

You can test this within the image (override entrypoint to /bin/bash) using: docker-entrypoint.sh postgres

like image 159
user3559338 Avatar answered Sep 17 '22 14:09

user3559338


I found the easy way to solve this...
You should use "build" way to create postgres service
And DO NOT setting the volume for init.sql, it will cause the permission problem.

    postgres:
        build: ./postgres

Create a Dockerfile for postgres like this

FROM postgres:12
COPY ./init.sql /docker-entrypoint-initdb.d/init.sql
CMD ["docker-entrypoint.sh", "postgres"]

Then it should works out. Hope my answer would help you!

like image 3
Anthony Lin Avatar answered Nov 20 '22 13:11

Anthony Lin


For me problem was in my machine. enabled SELinux access control, which did not allow for containers to expand files.

Solution, disable SELinux:

echo SELINUX=disabled > /etc/selinux/config
SELINUXTYPE=targeted >> /etc/selinux/config
setenforce 0

From this

like image 3
Pavel Maisiuk-Dranko Avatar answered Nov 20 '22 15:11

Pavel Maisiuk-Dranko