Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the port of nginx when using with docker

Tags:

docker

nginx

I am a docker beginner and the first thing i did was download nginx and tried to mount it on 80:80 port but Apache is already sitting there.

docker container run --publish 80:80 nginx

and docker container run --publish 3000:3000 nginx

I tried doing it like this 3000:3000 to use it on port 3000 but it doesn't work .And it doesn't log anything either which i could use for referance.

like image 419
Mayank Singh Fartiyal Avatar asked Nov 18 '17 08:11

Mayank Singh Fartiyal


People also ask

How do I use nginx on another port with Docker?

If you want to change the port nginx starts up on inside the container, you have to modify the /etc/nginx/nginx. conf file inside the container. navigating to localhost:3333 in a browser, you'll see your content. There is probably a way to include the default nginx.

Where is nginx config Docker?

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.


1 Answers

The accepted answer does not change the actual port that nginx is starting up on.

If you want to change the port nginx starts up on inside the container, you have to modify the /etc/nginx/nginx.conf file inside the container.

For example, to start on port 9080:

Dockerfile

FROM nginx:1.17-alpine
COPY <your static content> /usr/share/nginx/html
COPY nginx.conf /etc/nginx/
EXPOSE 9080
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

# on alpine, copy to /etc/nginx/nginx.conf
user                            root;
worker_processes                auto;

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

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    off;
    access_log                  off;
    keepalive_timeout           3000;
    server {
        listen                  9080;
        root                    /usr/share/nginx/html;
        index                   index.html;
        server_name             localhost;
        client_max_body_size    16m;
    }
}

Now to access the server from your computer:

docker build . -t my-app
docker run -p 3333:9080 my-app

navigating to localhost:3333 in a browser, you'll see your content.

There is probably a way to include the default nginx.conf, and override only the server.listen = PORT property, but I'm not too familiar with nginx config, so I just overwrote the entire default configuration.

like image 199
mancini0 Avatar answered Oct 07 '22 17:10

mancini0