Home > Enterprise >  Docker not running entrypoint .sh script
Docker not running entrypoint .sh script

Time:05-20

I'm trying to build a docker-compose to set up a postgres database with an initial script that creates some tables there.

I have this docker-compose.yml

version: '3.9'
services:
    postgres:
        image: postgres:12.7
        #restart: always
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
        logging:
          options:
            max-size: 10m
            max-file: "3"
        ports:
          - '5438:5432'
        volumes: 
          - ./postgres-data:/var/lib/postgresql/data
          # copy the sql script to create tables
          #- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
          - ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh

this is the create_tables.sh:

#!/bin/bash          
echo Start Executing SQL commands
psql -U postgres -f create_tables.sql

And the error log:

postgres_1 | psql: error: create_tables.sql: No such file or directory 

Uncommenting the line - ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql and commenting the line - ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh make it work but I have to use the .sh.

My dockerfile:

FROM postgres:12.7

#WORKDIR ./sql/

ADD ./sql/create_tables.sql ./docker-entrypoint-initdb.d/create_tables.sql

ADD ./sql/create_tables.sh ./docker-entrypoint-initdb.d/create_tables.sh

RUN ./docker-entrypoint-initdb.d/create_tables.sh

Any ideas what I'm doing wrong? Thanks!!

CodePudding user response:

You just need to use this script below, pointing to your ".SQL" files folder in the volumes step. It will automatically create the database and execute each SQL file inside this folder. You don't need a separate .sh file to migrate/create/execute your SQL commands using the docker-compose with the correct volume command.

version: '3.9'
services:
    postgres:
        image: postgres:12.7
        #restart: always
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
        logging:
          options:
            max-size: 10m
            max-file: "3"
        ports:
          - '5438:5432'
        volumes: 
          - ./postgres-data:/var/lib/postgresql/data
          # copy the sql script to create tables
          #- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
          #- ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh
          - ./sql:/docker-entrypoint-initdb.d #put all .SQL files inside sql/ folder and all files will be executed.

CodePudding user response:

Finally I separate a sql folder and a sh folder and it worked, but the correct approach would be Paulo answer.

 - ./db-init-scripts/create_tables.sh:/docker-entrypoint-initdb.d/init.sh 
 - ./sql:/sql
  • Related