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
Your problem is using
bash
to run the script.Nginx doesn’t have
bash
by default, usesh
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.When you use the
command
option in a docker-compose file, whatever you specify is passed to theENTRYPOINT
of the Dockerfile as parameter. So here, your command becomes something likenginx -g "daemon off;" bash -c ...
. Thus you get the errornginx: invalid option: "bash"
What I would advice :
entrypoint.sh
for example, with whatever you want to execute inside, and at the end the command to start nginx :ENTRYPOINT
, your docker-compose file would become something like this (Seevolumes
andentrypoint
) :Now, when you start your container,
entrypoint.sh
is used as entrypoint, executing your commands and starting nginx at the end.Hope this helps !