gpt4 book ai didi

nginx - Docker自动更新Nginx Web服务器容器的主机文件

转载 作者:行者123 更新时间:2023-12-02 20:15:47 26 4
gpt4 key购买 nike

我正在尝试使用localhost以外的其他URL访问我的Docker容器。
容器是一个nginx网络服务器,用于提供Ember应用程序的静态文件。
我的nginx配置看起来像这样:

server {
listen 80;
server_name my-app.dev;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

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

#error_page 404 /404.html;

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

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

我想通过 my-app.dev:8080访问我的应用程序。所以我的 Dockerfile看起来像这样:
FROM nginx:alpine

COPY ./my-app-nginx.conf /etc/nginx/conf.d/default.conf

COPY ./dist /usr/share/nginx/html

EXPOSE 80

和我的 docker-compose.yml文件是这样的:
version: "2"

services:
web:
restart: always
image: me/my-app:latest
build:
context: .
dockerfile: Dockerfile

container_name: my-app

# Keep the stdin open, so we can attach to our app container's process
# and do things such as debugging, etc:
stdin_open: true

# Enable sending signals (CTRL+C, CTRL+P + CTRL+Q) into the container:
tty: true

ports:
- "8080:80"

一切都差不多了,我必须修改主机上的 host文件以映射 127.0.0.1 my-app.dev,否则它将无法正常工作。

最佳答案

我想说的是,您想要的是打破封装原则,这是使用docker的优点之一。

一旦必须修改主机以使其能够以某种方式工作,您就脱离了Docker规则。

我并不是说这是不可能的,但您应该在打破隔离之前更好地考虑一下。

想象一下,如果我只能在运行一组Docker容器的情况下重新映射google.com,那实际上是一个安全问题。

如果要在主机中标识您的应用程序,则手动修改/etc/hosts文件是保持对主机系统的控制的最安全方法。

关于nginx - Docker自动更新Nginx Web服务器容器的主机文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com