Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker container exits on entry points

I am trying to run a docker container through Dockerfile with docker run command. Following is my Dockerfile.

FROM python:3.6
# for imaging stuff
RUN apt-get update
RUN apt install libmagickwand-dev
# Create  app directory
RUN mkdir -p /home/test/app
# Install Libre Office and ghostscript for pdf conversion and repairing
RUN apt-get update -qq \
    && apt-get install -y -q libreoffice \
    && apt-get remove -q -y libreoffice-gnome \
    && apt-get update \
    && apt-get -y install ghostscript \
    && apt-get -y install nano \
    && apt-get -y install poppler-utils \
    && apt-get install -y nginx
# Cleanup after apt-get commands
RUN apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    /var/cache/apt/archives/*.deb /var/cache/apt/*cache.bin
# Activate WORKING DIR
WORKDIR /home/test/app
# Initiating devnull directory
RUN mkdir -p dev_null
# Copying requirements
COPY requirements/local.txt /tmp/requirements.txt
# Install the app dependencies
RUN pip install -r /tmp/requirements.txt
COPY id_rsa /root/.ssh/id_rsa
COPY requirements/private.txt /tmp/private.txt
RUN ssh-keyscan -T 60 bitbucket.org >> /root/.ssh/known_hosts \
    && chmod 600  /root/.ssh/id_rsa \
    && pip install -r /tmp/private.txt
# Envs
ENV DJANGO_SETTINGS_MODULE app.settings.local
ENV ENVIORNMENT local
# ADD the source code and entry point into the container
ADD . /home/test/app
ADD docker-entrypoint.sh /home/test/app/docker-entrypoint.sh
# Making entry point executable
RUN apt-get update && apt-get install -y supervisor \
    && apt-get install -y nginx
#RUN mkdir -p /var/log/test
#COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf
RUN chmod +x docker-entrypoint.sh
RUN mkdir -p /var/log/test
COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf
# Exposing port


# Copy entrypoint script into the image
COPY ./docker-entrypoint.sh /
COPY ./django_nginx.conf /etc/nginx/
RUN chmod +x start.sh

#RUN chmod +x /docker-entrypoint.sh
EXPOSE 8000
ENTRYPOINT ["bash", "/docker-entrypoint.sh"]
CMD ["bash", "/home/test/app/start.sh"]

and the following are my entry points.

#!/usr/bin/env bash
set -e

# ToDo Need to enable this
#until psql $DATABASE_URL -c '\l'; do
#  >&2 echo "Postgres is unavailable - sleeping"
#  sleep 1
#done
#
#>&2 echo "Postgres is up - continuing"
cd app
mkdir -p app/keys
if [[ ! -e /var/log/gunicorn-access.log ]]; then
    touch /var/log/gunicorn-access.log
fi
if [[ ! -e /var/log/gunicorn-error.log ]]; then
    touch /var/log/gunicorn-error.log
fi

if [ "x$DJANGO_MANAGEPY_MIGRATE" = 'xon' ]; then
    echo "Django starting to migrate un-applied migrations"
    python manage.py migrate --noinput
fi

if [ "x$DJANGO_MANAGEPY_COLLECTSTATIC" = 'xon' ]; then
    echo "Django starting to collect static data"
    python manage.py collectstatic --noinput
fi

if [ "x$DJANGO_LOADDATA" = 'xon' ]; then
    python manage.py loaddata taxing/fixtures/province-taxing-table-initial-data.json
fi

if [ "x$LOAD_TEMPLATE_FROM_S3" = 'xtrue' ]; then
    echo "loading s3"
    python manage.py loadindex

fi


# Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn test.wsgi:application \
    --name test \
    --workers 3 \
    --log-level=info \
    --log-file=/srv/logs/gunicorn.log \
    --access-logfile=/srv/logs/access.log &

exec service nginx start

and following is my start.sh

#!/bin/bash

echo "I am running"
echo "Belive me"
/usr/bin/supervisord -n

and i am building the container with docker build -t test . and it is building just fine. But when i try to run this container using docker run --name test --env-file ./env test my container exits on ["bash", "/docker-entrypoint.sh"] command of Dockerfile without any errors/message but if I remove the entrypoint command from Dockerfile it's working just fine. I am unable to find out the mistake. Any help is appreciated.

like image 428
Shoaib Iqbal Avatar asked Nov 06 '22 20:11

Shoaib Iqbal


1 Answers

Here's what I see happening in this sequence:

ENTRYPOINT ["bash", "/docker-entrypoint.sh"]
CMD ["bash", "/home/test/app/start.sh"]

When you start the container, Docker runs bash /docker-entrypoint.sh bash /home/test/app/start.sh. However, the entrypoint script never looks at its command-line arguments at all, so any CMD you specify here or any command you give at the end of the docker run command line gets completely ignored.

When that entrypoint script runs:

exec gunicorn ... &
exec service nginx start
# end of file

it starts gunicorn as a background process and continues to the next line; then it replaces itself with a service command. That service command becomes the main container process and has process ID 1. It starts nginx, and immediately returns. Now that the main container process has returned, the container exits.


For this code as you've written it, you should delete the exec lines at the end of your entrypoint script and replace them with just

exec "$@"

which will cause the shell to run its command-line parameters (that is, the Dockerfile CMD).

However, there's a readily available nginx Docker image. Generally if you need multiple processes, it's easier and better to run them as two separate containers on the same Docker network. This avoids the complexities around trying to get multiple things running in the same container.

like image 145
David Maze Avatar answered Nov 16 '22 12:11

David Maze