Search code examples
dockergitlabgitlab-cigitlab-ci-runner

GitLab CI script needs entry in /etc/hosts


I have a GitLab CI docker runner to execute my automated tests when I push. One of my tests requires a custom entry in /etc/hosts. I can't figure out how to get the entry into that file.

Here's basically what my .gitlab-ci.yml file looks like:

before_script:
  - cat /etc/hosts   # for debugging
  - ...              # install app dependencies
specs:
  script:
    - rspec          # <- a test in here fails without the /etc/hosts entry 

All my tests pass, except for the one that requires that /etc/hosts entry.

Let's say I'm trying to have the hostname myhost.local resolve to the IPv4 address XX.XX.XX.XX...

I tried using extra_hosts on the runner config, but it didn't seem to have any effect (got idea from here):

/etc/gitlab-runner/config.toml:

concurrent = 1
check_interval = 0

[[runners]]
  name = "shell"
  url = "https://mygitlabinstance.com/"
  token = "THETOKEN"
  executor = "shell"
  [runners.cache]

[[runners]]
  name = "docker-ruby-2.5"
  url = "https://mygitlabinstance.com/"
  token = "THETOKEN"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "ruby:2.5"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    extra_hosts = ["myhost.local:XX.XX.XX.XX"]
  [runners.cache]

But the test still failed. The cat /etc/hosts shows that it's unchanged:

# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
#
127.0.1.1 ip-172-31-2-54.ec2.internal ip-172-31-2-54
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

I figured I could just add the entry myself in a before_script line, but I don't seem to be able to execute anything with root privileges in the container:

before_script:
  - echo 'XX.XX.XX.XX myhost.local' >> /etc/hosts
  ...

But that just fails because the gitlab-runner user doesn't have permissions to write to that file. I tried to use sudo, but gitlab-runner can't do that either (echo 'XX.XX.XX.XX myhost.local' | sudo tee --non-interactive --append /etc/hosts --> sudo: a password is required)

So in summary, how can I get my container to have the host entry I need (or how can I execute a before_script command as root)?


Solution

  • The following statement is incorrect:

    "But that just fails because the gitlab-runner user doesn't have permissions to write to that file."

    The gitlab-runner is not the user executing your before_script, it is the user that runs the container in which your job is executed.

    You are using the ruby:2.5 Docker image as far as I can tell and that does not contain any USER reference in its or its parents Dockerfile.

    Try adding a whoami command right before your echo 'XX.XX.XX.XX myhost.local' >> /etc/hosts command to verify you are root.

    Update

    If gitlab-runner is shown as the result of whoamithe docker-executor is not used and instead a shell-executor has picked up the job.