skip to Main Content

I have a front end project and I can customize it by specifying environments in my front project thanks to the sh scripts I place in it while building. My sample compose file is as follows. Everything I wrote in the command section works fine in my k8s deployment.yaml file, but I get an error in compose yml and the commands I write in command do not work.

version: '3.7'
services:
  client:
    container_name: client
    image: imagename:tag
    command: bash -c "
      if [ -s /var/www/set_env.sh ]; then
      mv /var/www/set_env.sh /mnt;
      fi;
      chmod +x /mnt/set_env.sh;
      /mnt/set_env.sh;
      rm /mnt/set_env.sh;"
    environment:
      SYSTEM: 'system'
      COMPANY: 'company'
      API_URL: 'https://'
      SITEKEY_LOGGER: 'logger'
      SITEKEY_CAPTCHA: 'captcha_key'
    # restart: always
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: '512m'
    ports:
      -  "18001:80"

The error I got for the compose above is;
nginx: invalid option: "bash"
I tried other ways but couldn’t find how to run command in compose file for my front project.
The Dockerfile I use for the service is as follows;

FROM node:12.18.3-alpine as build
WORKDIR /app
ENV PROFILE=basic
COPY . ./
RUN npm install
RUN npm run build

FROM nginx:1.17.0-alpine
COPY --from=build /app/build /var/www/
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
ENTRYPOINT ["nginx","-g","daemon off;"]

Note: In this situation, I need to be able to run command in compose. Is there a solution to this?

2

Answers


  1. Your problem is using bash to run the script.
    Nginx doesn’t have bash by default, use sh to run your script.
    simply replace bash with sh in the command section.
    you might want to add nginx -s reload to reload nginx if your script changes something related.

    Login or Signup to reply.
  2. When you use the command option in a docker-compose file, whatever you specify is passed to the ENTRYPOINT of the Dockerfile as parameter. So here, your command becomes something like nginx -g "daemon off;" bash -c .... Thus you get the error nginx: invalid option: "bash"

    What I would advice :

    • Create a file, entrypoint.sh for example, with whatever you want to execute inside, and at the end the command to start nginx :
    #!/bin/bash
    if [ -s /var/www/set_env.sh ]; then
        mv /var/www/set_env.sh /mnt
    fi
    chmod +x /mnt/set_env.sh
    /mnt/set_env.sh
    rm /mnt/set_env.sh
    
    nginx -g 'daemon off;'
    
    • Then, put this script inside the nginx container, and override the ENTRYPOINT, your docker-compose file would become something like this (See volumes and entrypoint) :
    version: '3.7'
    services:
      client:
        container_name: client
        image: imagename:tag
        volumes:
          - ./entrypoint.sh:/entrypoint.sh
        entrypoint: sh entrypoint.sh
        environment:
          SYSTEM: 'system'
          COMPANY: 'company'
          API_URL: 'https://'
          SITEKEY_LOGGER: 'logger'
          SITEKEY_CAPTCHA: 'captcha_key'
        # restart: always
        deploy:
          resources:
            limits:
              cpus: '0.5'
              memory: '512m'
        ports:
          -  "18001:80"
    

    Now, when you start your container, entrypoint.sh is used as entrypoint, executing your commands and starting nginx at the end.

    Hope this helps !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search