Why can't I cURL my HTTP service running on a Docker container in a CI?

Summary

  • I am starting a springboot application for running integration test in a gitlab-ci job.

  • The docker service have been started with docker run -p8080:8080 --name api-test

Steps to reproduce

I need to query the service with the following curl command:

curl --header "Accept:application/json" --connect-timeout 1000 localhost:8080/ping

What is the current bug behavior?

This is how it fail in Gitlab CI:

Tomcat started on port(s): 8080 (http)
Started ApiApplication in 26.11 seconds (JVM running for 26.673)
API started!
$ docker ps
CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS              PORTS                    NAMES
3d8e02acfafc        registry.domain.com/domain/api:0.5.0   "java -Djava.secur..."   31 seconds ago      Up 30 seconds       0.0.0.0:8080->8080/tcp   api-test
$ export API_TEST_HOST2=$(docker ps | grep api-test | awk -F ' ' '{print $1}' | xargs docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}')
$ echo $API_TEST_HOST2
172.18.0.2
$ echo "${API_TEST_HOST2} api-test.com" | tee -a /etc/hosts
172.18.0.2 api-test.com
$ hostname -i
172.17.0.20
$ curl --header "Accept:application/json" --connect-timeout 1000 api-test.com:8080/ping
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:06 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:07 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:08 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:09 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:11 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:12 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:13 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:14 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:15 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:16 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:17 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:18 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:19 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:20 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:02:09 --:--:--     0curl: (7) Failed to connect to api-test.com port 8080: Connection timed out
Running after script...
$ docker ps
CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS              PORTS                    NAMES
3d8e02acfafc        registry.domain.com/domain/api:0.5.0   "java -Djava.secur..."   2 minutes ago       Up 2 minutes        0.0.0.0:8080->8080/tcp   api-test
ERROR: Job failed: exit code 1

I have tried with the domain used for the cookie but it should have worked fine with localhost instead :

$ curl --header "Accept:application/json" --connect-timeout 1000 localhost:8080/ping
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

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

What is the expected correct behavior?

This is locally how it succeed:

dka@dev-01:[~]: curl -H "Accept: application/json" --connect-timeout 2 172.31.0.6:8080/ping
{"date":"2017-12-03 15:41:32","message":"Welcome to our API","version":"test-version","status":"OK"}

Possible fixes

If I try the curl command inside the container itself it work so it's clearly a networking issue:

$ docker exec $API_TEST_HOST wget -O - localhost:8080/ping | grep OK
Connecting to localhost:8080 (127.0.0.1:8080)
-                    100% |*******************************|   100   0:00:00 ETA

{"date":"2017-12-03 17:14:10","message":"Welcome to our API","version":"test-version","status":"OK"}

I am out of idea to debug this

  • Why is my service not accessible ?
  • How can I access it ?

I take all idea that could help me debug this (remember it is a ci build pipeline process and I don't have tty)

Edited by Kopax Anderson