Search code examples
dockergitlab-cigitlab-ci-runner

How to avoid port conflict with current container while gitlab-runner execute the job


I am trying to deploy my application with gitlab-runner.

Here is .gitlab-ci.yml script:

stages:
  - build
  - deploy

image: docker:20

variables:
  IMAGE_NAME: front:$CI_COMMIT_REF_NAME

build:
  stage: build
  when: manual
  script:
    - docker build -t $IMAGE_NAME .
    - docker run -dp 80:80 $IMAGE_NAME
  only:
    - master
    - tags
  tags:
    - front

After first execution all is fine, I have a running container with my application. But while next job execution I get error:

$ docker run -dp 80:80 $IMAGE_NAME
956d5a27449df0d38db357256fb96cf8cab8ba689d05fda21e54552bbe93fb7a
docker: Error response from daemon: driver failed programming external connectivity on endpoint frosty_elion (d89b1f150968331727a7c00823c28f3ab6a80f7882d659fce3520834005ad197): Bind for 0.0.0.0:80 failed: port is already allocated.
Cleaning up file based variables
ERROR: Job failed: exit code 125

Question: how to avoid conflict with running container that still is using a required port?

Here`s gitlab-runner config.toml:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "name"
  url = "url"
  token = "token"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0


Solution

  • The build script will not clean automatically the containers you are running. You need to assure that the container is stoped as follows:

    build:
      stage: build
      when: manual
      script:
        - docker build -t $IMAGE_NAME .
        - (docker stop test_deployment_80 || true)
        - docker run -dp --rm --name test_deployment_80 80:80 $IMAGE_NAME
      only:
        - master
        - tags
      tags:
        - front
    

    Here I am adding the --rm to the container in order to tell docker to remove the container when it's stoped. Also, I added --name to have recognizable name of the running container. I use the name in previous command to stop the container before I start new one.