Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run nginx docker container with custom config?

I have a Dockerfile and custom nginx configuration file (in the same directory with Dockerfile) as follows:

Dockerfile:

FROM nginx  COPY nginx.conf /etc/nginx/nginx.conf 

nginx.conf file:

upstream myapp1 {           least_conn;           server http://domain.com:81;           server http://domain.com:82;           server http://domain.com:83;     }  server {           listen 80;            location / {             proxy_pass http://myapp1;             proxy_http_version 1.1;             proxy_set_header Upgrade $http_upgrade;             proxy_set_header Connection 'upgrade';             proxy_set_header Host $host;             proxy_cache_bypass $http_upgrade;           }     } 

I run these two commands:

docker --tls build -t nginx-image . docker --tls run -d -p 80:80 --name nginx nginx-image 

Then I checked out all running containers but it didn't show up. When I searched nginx container's log, I found this error message:

[emerg] 1#1: unknown directive "upstream" in /etc/nginx/nginx.conf:1 nginx: [emerg] unknown directive "upstream" in /etc/nginx/nginx.conf:

What am I missing?

like image 654
Abdurrahman Alp Köken Avatar asked May 10 '15 12:05

Abdurrahman Alp Köken


People also ask

Where is nginx config in Docker container?

Maintaining Content and Configuration Files on the Docker Host. Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container.

How can I get nginx config file from Docker?

Download the official Nginx Docker image from Docker Hub. Run the Nginx Docker image as a container. Copy the Docker container's Nginx config file to your local file system. Add proxy_pass entries that point to your backend origin servers.


1 Answers

As mentioned in the NGiNX documentation, upstream is supposed to be defined in an http context.

As mentioned in nginx unkown directive “upstream”:

When that file is included normally by nginx.conf, it is included already inside the http context:

http {   include /etc/nginx/sites-enabled/*; } 

You either need to use -c /etc/nginx/nginx.conf or make a small wrapper like the above block and nginx -c it.

In case of Docker, you can see different options with abevoelker/docker-nginx:

docker run -v /tmp/foo:/foo abevoelker/nginx nginx -c /foo/nginx.conf 

For a default nginx.conf, check your CMD:

CMD ["nginx", "-c", "/data/conf/nginx.conf"] 
like image 159
VonC Avatar answered Oct 04 '22 06:10

VonC