how to run nginx docker container with custom config?

official nginx docker hub manual page:

docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx

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"]