使用docker或docker-compose时访问宿主机上的服务

使用docker或docker-compose时访问宿主机上的服务,第1张

1.太长不看版

在容器的启动命令前执行

ip -4 route list match 0/0 | awk '{print $3 " host.docker.internal"}' >> /etc/hosts

在容器中访问host.docker.internal即可访问宿主机上的服务。

2.示例 本机上运行了minio作为对象存储:
zhang@zhang:~$ docker run -p 9000:9000 -e "MINIO_ROOT_USER=zhangzhipeng" -e "MINIO_ROOT_PASSWORD=12345678" -v /home/zhang/data/minio:/data minio/minio server /data
Endpoint: http://172.17.0.3:9000  http://127.0.0.1:9000 

Browser Access:
   http://172.17.0.3:9000  http://127.0.0.1:9000

Object API (Amazon S3 compatible):
   Go:         https://docs.min.io/docs/golang-client-quickstart-guide
   Java:       https://docs.min.io/docs/java-client-quickstart-guide
   Python:     https://docs.min.io/docs/python-client-quickstart-guide
   JavaScript: https://docs.min.io/docs/javascript-client-quickstart-guide
   .NET:       https://docs.min.io/docs/dotnet-client-quickstart-guide
IAM initialization complete

此时在宿主机上通过localhost:9000即可访问minio服务。

2. 构建chartmuseum镜像,使用宿主机的minio服务作为存储。
Dockerfile:

FROM alpine:latest

# chartmuseum的二进制执行文件
COPY linux-amd64/chartmuseum /chartmuseum
# 容器启动脚本
COPY linux-amd64/chartmuseum.sh /chartmuseum.sh

RUN chmod +x /chartmuseum
RUN chmod +x /chartmuseum.sh

ENTRYPOINT ["sh"]

CMD ["/chartmuseum.sh"]

chartmuseum.sh:

#! /bin/sh
ip -4 route list match 0/0 | awk '{print $3 " host.docker.internal"}' >> /etc/hosts

cat /etc/hosts

/chartmuseum --debug --port=8080 \
  --storage=$STORAGE \
  --storage-amazon-bucket=$STORAGE_AMAZON_BUCKET \
  --storage-amazon-prefix=$STORAGE_AMAZON_PREFIX \
  --storage-amazon-region=$STORAGE_AMAZON_REGION \
  --storage-amazon-endpoint=$STORAGE_AMAZON_ENDPOINT

在启动脚本中,我们首先把宿主机的地址映射为host.docker.internal并注入到容器的/etc/hosts中,之后再启动服务。
docker-compose.yaml:

version: '2.0'
services:
  ui:
    # build: ./
    image: idobry/chartmuseumui:latest
    environment:
      CHART_MUSESUM_URL: http://chartmuseum:8080
      # CHART_MUSESUM_URL: http://192.168.50.113:8080
    ports:
      - 3000:8080

  chartmuseum:
    # image: chartmuseum/chartmuseum:v0.7.1
    image: define_museum:0.1
    ports:
    - 8080:8080
    environment:
      PORT: 8080
      DEBUG: 1
      AWS_ACCESS_KEY_ID: "zhangzhipeng"
      AWS_SECRET_ACCESS_KEY: "12345678"
      STORAGE: "amazon"
      STORAGE_AMAZON_BUCKET: "charts"
      STORAGE_AMAZON_PREFIX: ""
      STORAGE_AMAZON_REGION: ""
      STORAGE_AMAZON_ENDPOINT: "http://host.docker.internal:9000"

在docker-compose中,把宿主机的minio地址写为http://host.docker.internal:9000传递给容器的STORAGE_AMAZON_ENDPOINT环境变量。
3. 查看启动日志

zhang@zhang:~/data/program/backend/chartmuseumUI$ docker-compose up
Recreating chartmuseumui_chartmuseum_1 ... done
Starting chartmuseumui_ui_1            ... done
Attaching to chartmuseumui_ui_1, chartmuseumui_chartmuseum_1
chartmuseum_1  | 127.0.0.1      localhost
chartmuseum_1  | ::1    localhost ip6-localhost ip6-loopback
chartmuseum_1  | fe00::0        ip6-localnet
chartmuseum_1  | ff00::0        ip6-mcastprefix
chartmuseum_1  | ff02::1        ip6-allnodes
chartmuseum_1  | ff02::2        ip6-allrouters
chartmuseum_1  | 172.19.0.3     98fdb4a47536
chartmuseum_1  | 172.19.0.1 host.docker.internal
chartmuseum_1  | 2021-04-15T05:25:57.293Z       DEBUG   index-cache.yaml loaded {"repo": ""}
chartmuseum_1  | 2021-04-15T05:25:57.293Z       INFO    Starting ChartMuseum    {"host": "0.0.0.0", "port": 8080}
chartmuseum_1  | 2021-04-15T05:25:57.293Z       DEBUG   Starting internal event listener
ui_1           | 2021/04/15 05:25:56.951 [I] [asm_amd64.s:1333]  http server Running on http://:8080
...

可以看到宿主机的地址在容器中是172.19.0.1,并并映射为host.docker.internal,并且容器中的chartmuseum服务可以通过该地址和宿主机上的minio服务正常通信。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存