您可以使用 nginx代理容器 nginx-proxy设置一个运行nginx和docker-gen的容器。 docker-gen为nginx生成反向代理配置,并在启动和停止容器时重新加载nginx。
它比手动配置nginx更方便
注意:我没有看过 布哈罗夫谢尔盖的回答 然而,这可能更优雅。
这就是我想出的。我发现了两种实现网络共享功能的方法和一种使用所述方法时出现的Redmine问题的黑客攻击。
的 方法1(首选,因为它更短,但可能不是因为它已被弃用?): 集装箱连接 强>
泊坞窗,compose.yaml
version: '2' services: nginx: image: nginx ports: - "80:80" volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf # Method 1: Linking links: - redmine redmine: image: redmine # Method 1: Exposing port to linked containers expose: - "3000"
nginx.conf
http { server { listen 80; # Method 1: Access via alias from link location /redmine/ { proxy_pass http://redmine:3000/; } }
的 方法2: 定义网络 强>
version: '2' services: nginx: image: nginx ports: - "80:80" volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf # Method 2 networks: shared_net: ipv4_address: 172.22.0.4 redmine: image: redmine # Method 2 networks: shared_net: ipv4_address: 172.22.0.5 # Method number 2: Via networks networks: shared_net: driver: bridge ipam: config: - subnet: 172.22.0.0/16 gateway: 172.22.0.1
http { server { listen 80; # Method 2: Access via ip address in shared network location /redmine_networked/ { proxy_pass http://172.22.0.5:3000/; } } }
Redmine Hack: 通过郊区访问redmine
上述解决方案允许访问Redmine的主页。但是,所有Redmine URL都指向root(例如,'/'表示home,而不是'/ redmine'或'/ redmine_networked')。所以这些链接都不起作用。如果设置nginx将所有'/'url重定向到Redmine,这将是一个非问题。以下黑客假设情况并非如此。
要让Redmine指向配置的URL,需要编辑config / environment.rb文件。
这是黑客:
> docker exec -it <redmine_container> bash redmine> cd config # We're going to install vim (this is a hack) redmine> apt-get update redmine> apt-get install vim redmine> vim environment.rb
更改config / environment.rb底部的以下行 Initialize the Rails application Rails.application.initialize! 至 RedmineApp::Application.routes.default_scope = "/redmine" Initialize the Rails application Rails.application.initialize!
更改config / environment.rb底部的以下行
Initialize the Rails application Rails.application.initialize!
至
RedmineApp::Application.routes.default_scope = "/redmine" Initialize the Rails application Rails.application.initialize!
redmine> exit > docker restart <redmine> (or just kill other docker process and run docker up again)