0

I was trying to run commands at startup of nginx container with RUN commands in Dockerfile, but i need the server to be running to execute the commands.

So I tried to add a command line in my docker-compose.yml but then the container was restarting again and again because the script exited and overrided the entrypoint.sh script that is loaded by default in nginx container.

So my question is only : how can i run a script after nginx container is loaded and has loaded the default entrypoint script?

I ve seen here where the default script is Running a bash script before startup in an NGINX docker container

But how can i modify it in the docker image before it is started?

I would like to avoid to substitute to the default starting files..just add some commands, in particular to get ssl/tls certificates.

update: here is my Dockerfile. The certbot command is not working because i need the nginx server to be running...So I would like to run the commands in a script

FROM node:14-alpine as frontend-builder
#WORKDIR /app/frontend
WORKDIR /app/frontend
COPY ./frontend .
ENV PATH ./node_modules/.bin/:$PATH
RUN set -ex; \
  yarn install --frozen-lockfile --production; \
  yarn cache clean; \
  yarn run build
###############################################
FROM nginx:1.19.2-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY ./compose/production/build/nginx.conf /etc/nginx/conf.d
#volume /var/log/nginx pour les logs? 
# copy the frontend build
COPY --from=frontend-builder /app/frontend/build /usr/share/nginx/html/build
# si << marche pas faire avec  en fin de ligne
RUN apk update && \
    apk add nano && \
    apk add certbot && \
    mkdir -p /var/lib/letsencrypt && \
    mkdir -p /var/lib/letsencrypt/.well-known && \
    chgrp www-data /var/lib/letsencrypt && \
    chmod g+s /var/lib/letsencrypt && \
    mkdir -p /etc/nginx/snippets && \
    touch /etc/nginx/snippets/letsencrypt.conf && \
    touch /etc/nginx/snippets/ssl.conf && \
    echo 'location ^~/.well-known/acme-challenge/ {'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo 'allow all;'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo 'root /var/lib/letsencrypt/;'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo 'default_type "text/plain";'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo 'try_files $uri =404;'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo '}'>> /etc/nginx/snippets/letsencrypt.conf && \
    echo "certbot" && \

Thank you

3
  • We could provide you with better answers if you could show exactly what you're trying to do (because there may be multiple ways to do what you're trying to do, perhaps with different requirements than "run a script after nginx has started"). Commented Jun 20, 2022 at 0:00
  • i ve updated with the commands im running in dockerfile Commented Jun 20, 2022 at 2:06
  • You'd almost always run these commands in a second container. A container only runs one process, and while that can be a shell script, it's hard to safely do additional things once the main long-running process is already going. If you search Stack Overflow, I've seen lots of Docker + Nginx + Certbot examples go by. Commented Jun 20, 2022 at 11:05

0