我需要一个基于 nginx:alpine 的 Docker 容器从端口 8080 提供 http 内容,但 nginx:alpine 默认监听端口 80。
构建自定义容器时如何更改端口?
最佳答案
选项 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;"]
当然,备份原始配置是可选的
关于docker - 如何构建自定义 nginx :alpine based container listening on port other than 80?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55270099/