【问题标题】:Run Docker image on a different host在不同的主机上运行 Docker 镜像
【发布时间】:2018-06-12 21:20:12
【问题描述】:

是否可以在 localhost 以外的其他主机上运行 docker 映像

端口80443 总是被其他应用程序占用,所以我想知道是否可以运行它,例如192.168.0.100,然后设置 /etc/hosts 文件为该 IP 地址分配一个名称。

我试图设置extra_hosts 选项,但我不确定它是否是为此而设计的。无论哪种方式,我都没有成功设置它,因为Value should be a mapping, not an array 存在一些问题。

我想值得一提的是,我在 macOS 上使用 docker-compose 运行所有内容。提前谢谢你。

docker-compose.yaml

version: "2"

services:
  php:
    build: ./php
    volumes:
      - ../develog.org:/usr/share/nginx/html
  depends_on:
    - memcached

nginx:
  build: ./nginx
  ports:
    - 4001:80
    - 4002:443
  volumes_from:
    - php:ro
  depends_on:
    - php

memcached:
    image: memcached:alpine

networks:
  default:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.1.0.0/16

nginx 配置

map $scheme $ssl_request {
  https   "https";
}
server {
  # support http and ipv6
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;

  # support https and ipv6
  listen 443 default_server ssl;
  listen [::]:443 ipv6only=on default_server ssl;

  # path to web directory
  root /web/;
  index index.html index.htm;

  # domain or subdomain
  server_name localhost;

  include self-signed.conf;
  include ssl-params.conf;
}

【问题讨论】:

  • 可以在docker compose中指定远程端口ports: - "8983:8983"

标签: docker docker-compose dockerfile docker-for-mac


【解决方案1】:

为什么在运行映像时不使用不同的端口? (除非您使用端口 80/443)

因此,例如,如果您正在运行像 Apache httpd 这样的网络服务器映像,它通常在端口 80 上运行,请使用:

docker run -p 127.0.0.1:8080:80/tcp httpd

那么你就可以在 localhost 上使用 8080 端口了。

对于 docker-compose,在 docker-compose.yaml 文件中添加一个端口部分。例如

image: <image>
ports:
- "8080:80"

实际的端口/映像取决于您使用的映像。

详情请见https://docs.docker.com/compose/compose-file/#ports

【讨论】:

  • 我现在正在这样做,但是在处理 443 时确实遇到了一些错误,例如当我为 HTTPS 使用自定义端口时,例如4400,为localhost, 127.0.0.1, 0.0.0.0 生成证书我在访问https://localhost:4400/ 后总是以403 Forbidden 结束。这就是为什么我想知道在其他主机上使用默认值是否不是一个更好的主意
  • 可能对发布您的 docker-compose.yaml 有用
  • 完成。我是 nginx 新手,所以我也发布了那个配置。感谢您的关注。
  • 文档都在端口周围使用引号,因此请先尝试。但我认为你的问题在于 nginx,因为如果端口不工作而不是“403 禁止”,你会收到连接被拒绝错误
猜你喜欢
  • 2021-07-31
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 2017-12-17
  • 1970-01-01
相关资源
最近更新 更多