GAK
GAK

Reputation: 1088

nginx docker file cannot open file

I am writing to a create a docker file to run openresty nginx in a container.

my docker file is below.

FROM ubuntu:latest


ENV PATH="/usr/local/openresty/nginx/sbin:${PATH}"

LABEL info="running open resty on docker container"

RUN  apt-get update -y \
     && apt-get install -y wget \
     && wget -qO - https://openresty.org/package/pubkey.gpg | apt-key add - \
     && apt-get -y install software-properties-common \
     && add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" \
     && apt-get update -y \
     && apt-get install -y openresty
RUN mkdir -p /opt/poc

RUN cd /opt/poc \
       && mkdir logs \
       && touch logs/error.log \
       && mkdir conf \
       && cd ../

RUN chmod -R 755 /opt/poc 
WORKDIR /opt/poc


ADD ./conf/nginx.conf conf/

EXPOSE 8383


CMD ["nginx", "-p /opt/poc","-c conf/nginx.conf"]

The image gets built with no issues. But when I run the docker I am getting the below error message.

nginx: [alert] could not open error log file: open() " /opt/poc/logs/error.log" failed (2: No such file or directory) 2017/09/23 03:35:44 [emerg] 1#1: open() " /opt/poc/ conf/nginx.conf" failed (2: No such file or directory)

instead of running CMD ["nginx", "-p /opt/poc","-c conf/nginx.conf"] if I change to CMD $ls the docker is listing the conf and logs directory. so I am not sure what I am missing here.

Upvotes: 0

Views: 2154

Answers (1)

William Martins
William Martins

Reputation: 2067

Your problem is a tricky one, but I think I figured it out.

TL;DR

Use this CMD (it has no spaces in -p and -c flags)

CMD ["nginx", "-g", "daemon off;", "-p", "/opt/poc", "-c", "conf/nginx.conf"]

The -g daemon off; flag brings nginx as a forward process. Otherwise, if this runs as a daemon, your container will just die after nginx started.

Long Answer

The problem occurs when you add a spaced string to any of your CMD commands. In your case, -p /opt/poc, -c conf/nginx.conf.

I believe (and I would need to look more in docs/Docker source code to confirm) that CMD is ignoring one of the two "words" you put in your CMD parts.

As the documentation says, the recommended way is doing like this:

CMD ["executable","param1","param2"]

So, in your case, your params are doing "more" than just being a param (the "more" is when you use a space there). So, removing the spacing and splitting the command to be in the way that docs say fixed the problem.

Tip

I don't know what's your use case, but if you can, try to use the images provided by the community. In this example, the owners of openresty maintain the images:

https://hub.docker.com/r/openresty/openresty

Also, they provide a good documentation on how you can run it.

Hope it helps!

Upvotes: 2

Related Questions