Home > Blockchain >  File is not getting copied inside Docker container using cp command in a script
File is not getting copied inside Docker container using cp command in a script

Time:08-02

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related