首页 > 解决方案 > Docker(compose v3)静态IP,不绑定到本地主机 - Mac

问题描述

我在我的 Mac 上使用 docker 进行 Web 开发,它工作得非常好,我使用 docker-compose 和一个 traefik 容器将它通过隧道连接到我在 traefik 和 nginx 容器之间的共享网络上的开发网站。

我唯一的问题是我在端口 80 上有冲突,我只是在我的 mac 上使用不同的程序。

有没有办法将 docker 容器(traefik 容器)绑定到静态容器而不是本地主机?

这是我的撰写文件

version: '3.7'

services:
  reverse-proxy:
    container_name: traefik
    # The official v2.0 Traefik docker image
    image: traefik:v2.4
    # Enables the web UI and tells Traefik to listen to docker
    command:
      # - "--api.insecure=true" # Don't do that in production
      # - "--providers.docker=true"
      # - "--providers.docker.exposedbydefault=false"
      # - "--entrypoints.web.address=:80"
      # - "--entrypoints.web-secure.address=:443"
      # - --entrypoints.web.address=:80
      # - --entrypoints.web.http.redirections.entrypoint.to=websecure
      # - --entrypoints.web.http.redirections.entrypoint.scheme=https
      # - --entrypoints.websecure.address=:443
      # - --entrypoints.websecure.http.tls=true
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - --entrypoints.websecure.address=:443
      - --entrypoints.websecure.http.tls=true
      - --entrypoints.web.http.redirections.entrypoint.to=websecure
      - --entrypoints.web.http.redirections.entrypoint.scheme=https
    ports:
      # The HTTP port
      - "80:80"
      - "443:443"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro



networks:
  default:
    name: traefik_proxy
    external: false

标签: macosdockerdocker-composetraefik

解决方案


这是完全可能的。您需要做的是为您的 lo0 接口设置一个别名 ip,即

sudo ifconfig lo0 alias 123.123.123.123

查看:

ifconfig lo0 | grep "inet "
    inet 127.0.0.1 netmask 0xff000000
    inet 123.123.123.123 netmask 0xff000000

然后将您的另一个容器绑定到端口 80 上的此地址,您将能够将其用于本地测试。

在运行命令中绑定它

-p a.b.c.d:port:port

或在具有相同语法的撰写文件中:

a.b.c.d:port:port

查看:

有两个容器,绑定到端口 80:

docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS                               NAMES
64676a694bda   mendhak/http-https-echo   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   123.123.123.123:80->80/tcp          boring_murdock
c1671e852698   nginx                     "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   focused_cerf

向第一个容器发送请求:

curl 0.0.0.0      # or curl localhost

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

将请求发送到另一个容器,也在端口 80 上:

curl -s 123.123.123.123

{
  "path": "/",
  "headers": {
    "host": "123.123.123.123",
    "user-agent": "curl/7.64.1",
    "accept": "*/*"
  },
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "123.123.123.123",
  "ip": "::ffff:172.17.0.1",
  "ips": [],
  "protocol": "http",
  "query": {},
  "subdomains": [],
  "xhr": false,
  "os": {
    "hostname": "64676a694bda"
  },
  "connection": {}
}

完成后,您可以使用以下命令删除别名:

ifconfig lo0 -alias 123.123.123.123

推荐阅读