My host:
» lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
My docker:
» docker --version
Docker version 17.05.0-ce, build 89658be
What I tried:
1) Setting the hostname in the container: fails becauset the container lacks the right capabilities
root@172:/# hostname something
hostname: you must be root to change the host name
2) Manually editing /etc/hostname
in the container: has no effect after container restart, and has no purpose without restarting.
3) Manually editing the container configuration: it does not work, no idea why.
sudo vi /var/lib/docker/containers/834787a141d73b359814055b5fc0ba3e1cc52effeb6f916e2adece297d600772/config.v2.json
4) Changing the hostname of the container in the host using namespaces tools, as described here: not possible in Ubuntu 16.04.4 LTS
since I have no access to the namespaces tools.
What other options do I have?
Rationale
GitLab seems to pass its current hostname to a GitLab runner for it to clone repositories. In the default network, the container can not be reached by name, only by IP address. That means I need to set the hostname of the gitlab container to the IP address, but I only have the IP address once it is already running. The only solution is thus to:
- Run the container
- Find out its IP address
- Overwrite the hostname with the IP address
- Now gitlab and gitlab runner can talk to each other
The other (clean) option would be to start the containers (gitlab and gitlab-runner) in a custom network, so that they can be reached by real hostname, but currently I have the problem that the containers started by the gitlab runner are not able to reach the required container services spawned by the runner when using custom networks.
Best Answer
Wrong answer to your problem, but the answer you are asking for:
You can set the hostname when creating the container with an option, e.g.:
Why is this the wrong answer? Because you should be treating the container as cattle, not a named pet. Adjust gitlab and the runner to use the service name if you are running your containers as a swarm stack or with docker compose. Or if you are starting containers with
docker run
, you can use the container name, though I'd recommend switching to a service as soon as possible to make updates easier.For an even worse solution to the above, you can add capabilities to the container to allow it to change it's own hostname from inside the container. This is creating security vulnerabilities and is not recommended for any environment where security is important: