1

I am trying to use Gitlab CI to build a docker Nginx image then run & test a container by calling cURL on that container's Domain:PORT like this:

.gitlab-ci.yml:

variables:
  IMAGE_NAME: docker_curl_sandbox_img
  CONTAINER_NAME: docker_curl_sandbox_cont

image: docker:latest

services:
  - docker:dind

stages:
  - build
  - test

build_docker_img:
  stage: build
  only:
    - master
  script:
    - docker build -t $IMAGE_NAME .

test_run_docker:
  stage: test
  only:
    - master
  before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
  script:
    - docker kill $CONTAINER_NAME || true
    - docker rm $CONTAINER_NAME || true
    - docker run -p 8087:80 --name $CONTAINER_NAME -d $IMAGE_NAME
    - sleep 25
    - docker ps
    - cat /etc/hosts
    - curl http://docker:8087

Dockerfile:

FROM nginx
COPY ./src /usr/share/nginx/html

I have tried: curl http://docker:8087, curl http://0.0.0.0:8087, curl http://localhost:8087, ... I have tried the domains from /etc/hosts with no luck:

enter image description here

but I am keep getting: curl: (7) Failed to connect to 0.0.0.0 port 8087: Connection refused

enter image description here

any idea? thanks


Update: adding results of docker ps and netstat -na:

enter image description here

Jehad Nasser
  • 3,227
  • 5
  • 27
  • 39

1 Answers1

0

(edited)

URL http://0.0.0.0:8087 is invalid, that's why the connection is refused. See What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

Use netstat -na|grep 8087 on the docker host to check where/if the nginx connection is listening.

To access a different, non linked container you need to route through the port forwarding: try accessing it with the docker host external address (ie: curl http://192.168.1.2:8087).

Iron Bishop
  • 1,749
  • 1
  • 7
  • 16
  • Hello @Iron, the curl is installed inside the docker which enough I think, but you are right with the PORT, nginx is not connected. Please have a look at my update. thanks. – Jehad Nasser Feb 12 '20 at 09:52
  • 1
    Indeed you are running curl inside a docker, but not the image you are building, and that's fine. Since you are inside another container, netstat doesnt show anything. They aren't linked too, so you can't use local addressing. Try using the external IP address to connect, like `curl http://192.168.1.2:8087`. – Iron Bishop Feb 12 '20 at 14:54