23

I am attempting to run a shell script by using docker-compose inside the docker container. I am using the Dockerfile to build the container environment and installing all dependancies. I then copy all the project files to the container. This works well as far as I can determine. (I am still fairly new to docker, docker-compose)

My Dockerfile:

FROM python:3.6-alpine3.7

RUN apk add --no-cache --update \
    python3 python3-dev gcc \
    gfortran musl-dev \
    libffi-dev openssl-dev

RUN pip install --upgrade pip

ENV PYTHONUNBUFFERED 1
ENV APP /app

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir $APP
WORKDIR $APP

ADD requirements.txt .
RUN pip install -r requirements.txt

COPY . .

What I am currently attempting is this:

docker-compose file:

version: "2"

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "8000:8000"
      - "443:443"
    volumes:
      - ./:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./config/nginx/ssl/certs:/etc/ssl/certs
      - ./config/nginx/ssl/private:/etc/ssl/private
    depends_on:
      - api
  api:
    build: .
    container_name: app
    command: /bin/sh -c "entrypoint.sh"
    expose:
      - "5000"

This results in the container not starting up, and from the log I get the following:

/bin/sh: 1: entrypoint.sh: not found

For more reference and information this is my entrypoint.sh script:

python manage.py db init
python manage.py db migrate --message 'initial database migration'
python manage.py db upgrade
gunicorn -w 1 -b 0.0.0.0:5000 manage:app

Basically, I know I could run the container with only the gunicorn line above in the command line of the dockerfile. But, I am using a sqlite db inside the app container, and really need to run the db commands for the database to initialise/migrate.

Just for reference this is a basic Flask python web app with a nginx reverse proxy using gunicorn.

Any insight will be appreciated. Thanks.

2
  • 1
    Give exact path to entrypoint.sh command: /bin/sh -c "/path/to/entrypoint.sh" Commented Sep 8, 2019 at 9:48
  • Thanks, managed to solve the issue, ended up having to ssh into the container to find that the entrypoint.sh script was never included in the container. The solution as per [Adiii] below proved useful in figuring that out. Commented Sep 8, 2019 at 13:07

2 Answers 2

21

First thing, You are copying entrypoint.sh to $APP which you passed from your build args but you did not mentioned that and second thing you need to set permission for entrypoint.sh. Better to add these three lines so you will not need to add command in docker-compose file.

FROM python:3.6-alpine3.7
RUN apk add --no-cache --update \
    python3 python3-dev gcc \
    gfortran musl-dev \
    libffi-dev openssl-dev
RUN pip install --upgrade pip
ENV PYTHONUNBUFFERED 1
ENV APP /app
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir $APP
WORKDIR $APP
ADD requirements.txt .
RUN pip install -r requirements.txt

COPY . .
# These line for /entrypoint.sh
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT "/entrypoint.sh"

docker compose for api will be

  api:
    build: .
    container_name: app
    expose:
      - "5000"

or you can use you own also will work fine

version: "2"

services:
  api:
    build: .
    container_name: app
    command: /bin/sh -c "entrypoint.sh"
    expose:
      - "5000"

Now you can check with docker run command too.

docker run -it --rm myapp

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this solved the issue for me. I ended up using only the first 2 lines added to the Dockerfile and running the /bin/sh -c "/entrypoint.sh" command from docker-compose. Basically to allow separation the container build vs running a container.
What does this line in Dockerfile do in your answer entrypoint "/entrypoint.sh"?
Got it, it's just ENTRYPOINT "/entrypoint.sh" without the capitalization
0

entrypoint.sh needs to be specified with its full path.

It's not clear from your question where exactly you install it; if it's in the current directory, ./entrypoint.sh should work.

In some more detail, the working directory inside the container is set by WORKDIR; and so, for example, if WORKDIR is /foo then ./entrypoint.sh is equivalent to /foo/entrypoint.sh

(Tangentially, the -c option to sh is superfluous if you want to run a single script file.)

1 Comment

Thanks for the insight. I wound up having to ssh into the container and discovering that the entrypoint.sh wasn't present, managed to solve it by including it in the Copy command via the Dockerfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.