7

I'm trying to create a simple GitLab CI where I spin up a container using docker-compose up then try to access it using curl and finally tear it down using docker-compose down. docker-compose up spins up perfectly fine and I can see the container up using docker ps -a, however when I curl, I get "connection refused".

here is my gitlab-ci.yml

image: docker

services:
 - docker:dind

before_script:
 - apk add --update python py-pip python-dev && pip install docker-compose
 - apk add --update curl && rm -rf /var/cache/apk/*

stages:
 - test

test:
 stage: test
 script:
  - docker-compose up -d
  - docker ps -a
  - curl http://localhost:5000/api/values
  - docker-compose down

here is the runner's logs

Image for service testwebapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating test-container ... 

Creating test-container ... done
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                    NAMES
3423adfb1f3b        mytestwebapp        "dotnet TestWebApp.d…"   1 second ago        Up Less than a second   0.0.0.0:5000->5000/tcp   test-container
$ curl http://localhost:5000/api/values
  % 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 5000: Connection refused
ERROR: Job failed: exit code 7

Docker Compose:

version: '3.4'

services:
  testwebapp:
    image: mytestwebapp
    build:
      context: .
      dockerfile: TestWebApp/Dockerfile
    container_name: test-container

Docker compose override:

version: '3.4'

services:
  testwebapp:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://0.0.0.0:5000
    ports:
      - "5000:5000"
marwanm
  • 73
  • 1
  • 4
  • Why the command of your container is `dotnet TestWebApp.d…`. Is this correct? If yes, can you add more details on this. – Ortomala Lokni Dec 02 '18 at 09:19
  • Hello @OrtomalaLokni, that is the spin up of the app under docker. it is correct as this is the command used to launch the web app using dotnet cli. It's the result of the following section in the docker file: FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "TestWebApp.dll"] – marwanm Dec 03 '18 at 15:48
  • The source of the error obviously is the `curl` command, however I guess one couldn't help with this without more details about your `docker-compose.yml` file. Could you add it in your question? – ErikMD Dec 08 '18 at 22:11
  • Also did you pay attention to hard-code the special IP `0.0.0.0` in your code? Otherwise you would probably experiencing the same issue as https://stackoverflow.com/a/52729289/9164010 – ErikMD Dec 08 '18 at 22:16
  • Hello ErikMD, I added the docker-compose.yml and the docker-compose-override.yml. as you can see the ASPNETCORE_URLS is set to 0.0.0.0 Hope this helps clarify things better :) – marwanm Dec 10 '18 at 15:37

1 Answers1

8

Update your gitlab-ci.yml file:

  1. set sleep 15 before running curl. 15 is an arbitrary period in seconds when your service should start exactly.

  2. Next, there are 2 options: https://docs.gitlab.com/ee/ci/services/#how-services-are-linked-to-the-job:

Option 1: Replace localhost in curl http://localhost:5000/api/values with docker like this curl http://docker:5000/api/values

Option 2:

services:
  - name: docker:dind
    alias: localhost
Ilya Petukhov
  • 682
  • 7
  • 12