简体   繁体   中英

Is any way to “add *.sql to docker-entrypoint-initdb.d” and execute an app in the same Dockerfile with alpine?

I need to write in the same dockerfile a service and BBDD, and I am using an image with alpine and postgresql. My dockerfile is like this:

FROM postgres:11.2-alpine


ENV DEBIAN_FRONTEND=noninteractive

RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add netcat-openbsd

RUN apk update && \
    apk add py-pip

RUN echo $(ls)
RUN echo $(find . -type d -name "postgresql")

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

#ADD /db/create.sql /docker-entrypoint-initdb.d
#RUN su - postgres

RUN /etc/init.d/postgresql start &&\ 
    psql -f /db/create.sql

COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY . /usr/src/app

RUN chmod 777 manage.py

EXPOSE 5000
EXPOSE 5432

CMD python manage.py runserver --host 0.0.0.0

The problem is that if I write "ADD *.sql /docker-entrypoint-initdb.d", the sql file is not execute. I have tried to execute in other way doing RUN /etc/init.d/postgresql start &&\\ psql -f /db/create.sql, but It does not work because the init of postgresql is not in.

Thanks in advance.

This will not work like this and mainly because this is not how you should do it. Normally database and application should run in different containers.

This is the Dockerfile for the posgres image: https://github.com/docker-library/postgres/blob/85aadc08c347cd20f199902c4b8b4f736341c3b8/11/Dockerfile

With your changes you are overwriting the CMD of this image and that is why your init scripts don't run. You can check the startup script here: https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh

Also with Postgres it is important to make sure you remove the volumes between runs. If the volume is present then init scripts are not run anyway.

My advice is to try the approach with 2 containers instead of trying to fix this issue that is unfixable in a nice way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM