can't run nginx in alpine linux (docker)

17,048

Solution 1

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;'

Solution 2

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

pid /run/nginx.pid;

Solution 3

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
Share:
17,048

Related videos on Youtube

paul23
Author by

paul23

Updated on September 18, 2022

Comments

  • paul23
    paul23 over 1 year

    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.

  • Toto
    Toto almost 6 years
    Can you elaborate on this a little more?
  • Milen John Thomas
    Milen John Thomas almost 6 years
    I have edited my answer above. There is a change in path.
  • Rafael Eyng
    Rafael Eyng almost 5 years
    You can't assume that "there is no real need". In my case, I have to use a custom nginx build with optional modules. That is a real need.
  • Sylver
    Sylver over 4 years
    nginx: [emerg] unexpected end of parameter, expecting ";" in command line You actually need to put a ";" at the end of the parameter nginx -g 'pid /tmp/nginx.pid;'