如何建立自定义的基于nginx:alpine的容器,侦听80以外的端口?

时间:2019-03-20 20:59:50

标签: docker http nginx containers alpine

我需要一个基于nginx:alpine的Docker容器,该容器从端口8080提供HTTP内容,但是默认情况下nginx:alpine会在端口80上进行监听。
构建自定义容器时如何更改端口?

1 个答案:

答案 0 :(得分:3)

选项1(推荐):设置新的配置文件

创建具有以下内容的本地 default.conf * 文件:

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;
    }
}

*超出端口8080,根据您的需要自定义上述内容

default.conf 复制到自定义容器[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;"]

选项2:更改nginx默认配置[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;"]
当然,

备份原始配置是可选的