NGINX docker container immediately stops

I'm started to get to know Docker and nginx in docker. I want to use my own custom static files, and my own nginx.conf, so i've created a docker file:

FROM nginx
RUN rm /etc/nginx/nginx.conf
COPY /nginx.conf /etc/nginx/nginx.conf
COPY / /usr/share/nginx/html

# Expose ports

And my nginx.conf:

#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/;

events {
    worker_connections  1024;

http {
    include       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"' 
                      '$server_name to: $upstream_addr: $request';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri /index.html;
            include       mime.types;           

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;


I'm building this with:

docker build --no-cache -t nginx-custom .

And running it with:

docker run -d -p 8080:80 --name webserver nginx-custom

But it builds, and then runs, but stopps immediately so if i check with

docker ps -a

I can see it's exited 1 second ago. I tried to use CMD /usr/sbin/nginx -g "daemon off;" or deamon off; in the nginx conf, or use CMD ["nginx", "-g", "daemon off;"] no matter what i do, it's just exits right after run.

You have to define the correct path for your nginx logs.

/var/log/nginx/error.log and /var/log/nginx/access.log

Try running it with the below command:-

docker run -d -p 8080:80 --name webserver -it nginx-custom /bin/bash

when i tried to run the container in interactive mode i got:

[emerg] 1#1: open() "/etc/nginx/logs/access.log" failed (2: No such file or directory) nginx: [emerg] open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
then i added:
RUN mkdir -p /etc/nginx/logs/ and it works

where the nginx.conf file located in your pc?

if it is located in the same directory you should change the command to

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

the nginx.conf is located beside the Dockerfile, i tried to change the COPY, but still exits

In order to debug your config, you can run the container in interactive mode.

docker run -it -p 8080:80 --name web server nginx-custom

then inside the container, you can check your nginx configuration with nginx -t command

So your log path is wrong
you can create it in docker file or leave the default /var/log/nginx/error.log /var/log/nginx/access.log