简体   繁体   English

使用 Docker 启动时,Docker 容器中没有 Internet

[英]No Internet in Docker Container When Launched with Docker Compose

The following command starts the container and it can access the Internet:以下命令启动容器,它可以访问 Internet:

docker run --rm \
-e PLUGIN_BUCKET=the-bucket \
-e PLUGIN_SOURCE=/ \
-e PLUGIN_TARGET=/mnt/data/google \
-e GOOGLE_APPLICATION_CREDENTIALS_FILE=/mnt/key/cred.json \
--mount type=bind,source=/home/ec2-user/data/google,target=/mnt/data/google \
--mount type=bind,source=/home/ec2-user,target=/mnt/data \
 gcs-sync

The following docker compose file will start the same container but the sync will fail because there is no Internet:以下 docker 组合文件将启动相同的容器,但由于没有 Internet,同步将失败:

version: '3.7'
services:
  gcs_sync:
    image: gcs-sync:latest
    container_name: gcs_sync
    environment:
      PLUGIN_BUCKET: the-bucket
      PLUGIN_SOURCE: /
      PLUGIN_TARGET: /mnt/data/google
      GOOGLE_APPLICATION_CREDENTIALS_FILE: /mnt/key/cred.json
    volumes:
      - type: bind
        source: /home/ec2-user/data/google
        target: /mnt/tdata/google
      - type: bind
        source: /home/ec2-user/key
        target: /mnt/key

Here is the content of the docker-compose logs command:下面是 docker-compose 日志命令的内容:

gcs_sync | gcs_sync | ERROR: gcloud crashed (TransportError): HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f78ad0b7d90>: Failed to establish a new connection: [Errno -3] Try again')) ERROR: gcloud crashed (TransportError): HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f78ad0b7d90>:无法建立新连接:[Errno -3] 重试'))

I have tried using the host network but I don't know how to connect to it, I get the我曾尝试使用主机网络,但我不知道如何连接到它,我得到了

I found the solution, with docker 3.7, you can add networking_mode: host :我找到了解决方案,使用 docker 3.7,您可以添加 network_mode networking_mode: host

version: '3.7'
services:
  gcs_sync:
    image: gcs-sync:latest
    container_name: gcs_sync
    networking_mode: host
    environment:
      PLUGIN_BUCKET: bucket
      PLUGIN_SOURCE: /
      PLUGIN_TARGET: /mnt/data/google
      GOOGLE_APPLICATION_CREDENTIALS_FILE: /mnt/key/cred.json
    volumes:
      - type: bind
        source: /home/ec2-user/data/google
        target: /mnt/data/google
      - type: bind
        source: /home/ec2-user/key
        target: /mnt/key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM