Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a custom nginx:alpine based container listening on port other than 80?

I need a nginx:alpine based Docker container serving http content from port 8080, but nginx:alpine listens on port 80 by default.
How can I change the port when building my custom container?

like image 856
Ricardo Mendes Avatar asked Jan 01 '23 03:01

Ricardo Mendes


1 Answers

OPTION 1 (recommended): setting a new configuration file

Create a local default.conf* file with the following content:

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

* beyond port 8080, customize the above content to fit your needs

Copy default.conf to the custom container [Dockerfile]:

FROM nginx:alpine
## Copy a new configuration file setting listen port to 8080
COPY ./default.conf /etc/nginx/conf.d/
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

OPTION 2: changing nginx default configuration [Dockerfile]

FROM nginx:alpine
## Make a copy of default configuration file and change listen port to 8080
RUN cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.orig && \
    sed -i 's/listen[[:space:]]*80;/listen 8080;/g' /etc/nginx/conf.d/default.conf
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

making a backup of the original config is optional, of course

like image 158
Ricardo Mendes Avatar answered Jan 13 '23 13:01

Ricardo Mendes