Run a shell script from docker-compose command, inside the container

49,947

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

Share:
49,947
Kevin Smith
Author by

Kevin Smith

Here to learn, and ask questions.

Updated on December 02, 2020

Comments

  • Kevin Smith
    Kevin Smith over 3 years

    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.

  • Kevin Smith
    Kevin Smith over 4 years
    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.
  • Kevin Smith
    Kevin Smith over 4 years
    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.
  • dumbledad
    dumbledad about 2 years
    What does this line in Dockerfile do in your answer entrypoint "/entrypoint.sh"?
  • dumbledad
    dumbledad about 2 years
    Got it, it's just ENTRYPOINT "/entrypoint.sh" without the capitalization