15

Well I have set up an alpine linux docker container with nginx on it

(apk add nginx)

Now I am trying to run nginx. With the simple command nginx This returns the following error:

nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)

What is causing this? - Even as root I do not have permission to open /run/ so I can't really check things out. My nginx.conf is:

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    #include /etc/nginx/mime.types;
    #default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    ##
    # Virtual Host Configs
    ##

    #include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;
}

Sure no server is included, but I expect it to be able to run and just never serve anything. - But even if I do enable the inclusion (which redirects to the default html into page, which I have also copied into the docker) the same error occurs.

paul23
  • 593

3 Answers3

18

The /run/nginx directory does not exist on the latest alpine containers.

You can either create the directory or change the PID file location.

Create the directory in your Dockerfile:

RUN mkdir -p /run/nginx

Or change the location of the PID file:

nginx -g 'pid /tmp/nginx.pid;'
ewatt
  • 424
2

Alpine linux does not have run/nginx/nginx.pid. Try add this on the top nginx.conf file.

pid /run/nginx.pid;

1

There is no real need to create your own nginx docker container from the alpine base image when there is an official, Alpine nginx docker image on Docker Hub.

Here is a Dockerfile of nginx upstream docker image.

To pull it directly from Docker hub, use:

$ docker pull nginx:alpine

You can modify its Dockerfile with your custom variables and build your own image.

$ docker build -t nginx-alpine
Ben Njeri
  • 279
  • 1
  • 2