2

I am trying to run a custom-nginx server in Docker with a custom configuration file(Let's call it custom.configuration.conf)

And, I want the container to pick the configuration file based on the deployment environment. Hence in my repository, I have put the configuration as follows:

configuration(folder)
|
----> qa (sub-folder of configuration folder)
     |
     ----> custom.configuration.conf
|      
----> prd (sub-folder of configuration folder)
      |
      ---> custom.configuration.conf

To pick these files dynamically I introduced an environment variable DEPLOYMENT_ENVIRONMENT which I'm using in a file named start.sh

The start.sh script is as follows:

cp configuration/${DEPLOYMENT_ENVIRONMENT}/custom.configuration.conf /etc/nginx/conf.d
nginx -g "daemon off;"

My Dockerfile looks like this:

FROM nginx:1.23-alpine as application
WORKDIR /build
COPY configuration configuration
COPY start.sh start.sh
ENTRYPOINT /build/start.sh

The commands I'm using to build and run the container are:

docker build -t custom-nginx .
docker run -e DEPLOYMENT_ENVIRONMENT=qa -p 8080:80 custom-nginx

However, when I docker exec into the container I can't see the custom.configuration.conf at /etc/nginx/conf.d.

I'm running this on a Windows 10 machine with Docker Desktop.

start.sh, Dockerfile and configuration folder/files are at the root level in my repository.

I don't understand where I am going wrong. Please help me understand why am I not able to copy the file from container to container.

P.S: I actually tried running the cp configuration/${DEPLOYMENT_ENVIRONMENT}/custom.configuration.conf /etc/nginx/conf.d command inside the container to check if the command was wrong. However, it works if I do it manually inside the container, but fails when I run it using docker run.

Rama Rahul
  • 93
  • 7

2 Answers2

2

Add error handling to your entrypoint.sh script:

cp -v configuration/${DEPLOYMENT_ENVIRONMENT}/custom-configuration.conf /etc/nginx/conf.d
if [ "$?" != "0" ]
then
    echo "Could not copy configuration for DEPLOYMENT_ENVIRONMENT=${DEPLOYMENT_ENVIRONMENT}!"
    exit 1
fi
nginx -g "daemon off;"

Now check the docker logs when running the container. They should give you more hints of what happened.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Getting the following error now: ` 'configuration/qa/custom-configuration.conf' -> '/etc/nginx/conf.d /build/start.sh: 7: Syntax error: end of file unexpected (expecting "then") ` – Rama Rahul Jul 30 '22 at 09:09
  • Please share your whole entrypoint.sh script. I suspect some whitespace got scrambled. – Queeg Jul 31 '22 at 18:45
  • I just googled this issue and the answer by David Gidony I found here - https://stackoverflow.com/a/50212715/6123155 fixed both of my problems - the issue above and the original question too. The issue was due to a mismatch between Windows and Unix EOL. Anyway, thanks for your help, too! – Rama Rahul Aug 01 '22 at 09:19
1

I've performed the EOL conversion from Windows(CR LF) to Unix(LF) as directed here: https://stackoverflow.com/a/50212715/6123155 and that fixed the issue for me.

Rama Rahul
  • 93
  • 7