0

I have nginx on my local machine that redirects all http traffic from port 80 to 8008 with this simple config:

server {

    listen 8008;

#        location some-location {
#        
#        }

    location / {
        proxy_pass http://localhost:80/index.php/;
        proxy_set_header  X-Real-IP  $remote_addr;
    }
}

Everything works OK locally, but I want to place that nginx inside a docker container. My Dockerfile:

FROM nginx

COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8008 80

But when I run my image with cmd

docker run -d -p 8008:80 <image ID>

Nothing happens, the container stops. Logs here:

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2f2602efc455 a58e81b31db6 "nginx -g 'daemon of…" 10 seconds ago Exited (1) 10 seconds ago unruffled_galileo 


docker logs
2018/01/03 12:17:08 [emerg] 1#1: "server" directive is not allowed here in /etc/nginx/nginx.conf:1 nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1

What am I doing wrong?

  • Be so kind and show logs from container runtime. docker ps -a and docker logs <container-id> commands might be helpful. – xulsitatirev Jan 3 '18 at 12:05
  • docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2f2602efc455 a58e81b31db6 "nginx -g 'daemon of…" 10 seconds ago Exited (1) 10 seconds ago unruffled_galileo – Bill Walters Jan 3 '18 at 12:17
  • docker logs 2018/01/03 12:17:08 [emerg] 1#1: "server" directive is not allowed here in /etc/nginx/nginx.conf:1 nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1 – Bill Walters Jan 3 '18 at 12:18
  • Maybe not the main issue, but are you trying to expose port 8008 and 80? You only have to define which port you want to expose. The mapping is then done in your docker run call. – Patrick Bucher Jan 3 '18 at 13:09
0

OK, finally I found the error in my nginx.conf: user nginx; worker_processes 1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

      server {

        location / {
            proxy_pass http://localhost:80/index.php/;
            proxy_set_header  X-Real-IP  $remote_addr;
                   }
             }
}

Dockerfile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8008

And launched the image with command:

docker run -d -p 8008:80 <ID>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.