简体   繁体   English

docker docker localhost 网络问题中的docker

[英]docker in docker localhost networking issue

I have a case where I want to do a quick healthcheck on a docker-compose up -d where I can successfully curl the endpoint being created by the compose file.我有一个案例,我想对docker-compose up -d进行快速健康检查,在那里我可以成功地卷曲由撰写文件创建的端点。

Here is what the relevant parts of my file look like:这是我的文件的相关部分的样子:

compose:
    stage: compose
    image: tmaier/docker-compose:latest
    services:
      - docker:dind
    before_script:
      - apk update
      - apk add -y curl
      - docker info
      - docker-compose --version
      - echo $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin registry.gitlab.com
    script:
      - cd docker/compose/
      - docker-compose run --use-aliases -d nginx
      - cat /etc/hosts
      - curl --fail http://localhost

I have tried all types of things like http://dockerhost , http://docker , http://nginx , --use-aliases , -p 80:80 .我已经尝试了所有类型的东西像http://dockerhosthttp://dockerhttp://nginx--use-aliases-p 80:80 The docker-compose run -d nginx and curl --fail http://localhost works fine on my computer, just doesn't work here, also here is some output that might help you help me. curl --fail http://localhost docker-compose run -d nginxcurl --fail http://localhost在我的电脑上工作正常,只是在这里不起作用,这里还有一些输出可以帮助你帮助我。

$ cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  docker fe41d99555a4 runner-0277ea0f-project-8964623-concurrent-0-docker-0
172.17.0.4  runner-0277ea0f-project-8964623-concurrent-0
$ curl --fail http://localhost
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to localhost port 80: Connection refused

This all works locally, but doesn't appear to work in gitlab-ci.这一切都在本地工作,但在 gitlab-ci 中似乎不起作用。 Cross posted from https://github.com/tmaier/docker-compose/issues/14 .https://github.com/tmaier/docker-compose/issues/14交叉发布。

Per request from a commenter, here is some additional debug info:根据评论者的请求,这里有一些额外的调试信息:

$ docker-compose port nginx 80
0.0.0.0:80

$ docker-compose port django 8000
0.0.0.0:8000

$ docker ps
$ docker ps
CONTAINER ID        IMAGE                                                                                           COMMAND                  CREATED             STATUS                            PORTS                                      NAMES
ea9c0d0f8aba        celery_worker                                           "/start_celery.sh -d…"   4 seconds ago       Up 1 second                                                                  compose_celery-beat_1_3cc1424b6246
6f5adc306f08        celery_beat                                         "/start_celery.sh -d…"   4 seconds ago       Up 1 second                                                                  compose_celery-worker_1_82f9d734734b
4d8d18ee0aa0        nginx                                               "nginx -g 'daemon of…"   4 seconds ago       Up 1 second                       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   compose_nginx_1_d6a895d7ea5a
0e3f43da2694        django                                              "/start.sh -docker"      4 seconds ago       Up 3 seconds                      0.0.0.0:8000->8000/tcp                     compose_django_1_c7d04b2e58cb
260a04a50134        redis:3.0-alpine                                        "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds                      0.0.0.0:6379->6379/tcp                     compose_redis_1_d6cf5f09d98a
49097614e743        postgres:10-alpine                                      "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds (health: starting)   5432/tcp                                   compose_hoarder-datastore_1_a7db557d6f4c  

I believe you do have a race condition.我相信你确实有竞争条件。 The http request is being sent too soon, when the container is not ready yet.当容器尚未准备好时,http 请求发送得太快。

See similar case at https://gitlab.com/gitlab-com/support-forum/issues/748#note_12330190https://gitlab.com/gitlab-com/support-forum/issues/748#note_12330190查看类似案例

So please add “sleep 60” before the curl所以请在卷曲前加上“sleep 60”

您应该使用http://docker而不是引用http://localhost

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

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