1

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:

  1. Run the container
  2. Find out its IP address
  3. Overwrite the hostname with the IP address
  4. 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.

2 Answers 2

0

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.:

docker run --hostname example.com image_name

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:

docker run --cap-add SYS_ADMIN ...
10
  • This does not solve my problem at all: as explained in detail, I need the hostname to be the IP address, because DNS resolution is not possible on the default network. docker run creates a container, and I need to modify the hostname in an existing container.
    – blueFast
    Mar 2, 2018 at 13:22
  • And regarding your other comment: "Adjust gitlab and the runner to use the service name". That's my end goal, but for the reasons detailed it is not yet working in my setup. So I am stuck with using the IP address of the gitlab container as hostname, and for that I must be able to overwrite the hostname after container creation.
    – blueFast
    Mar 2, 2018 at 13:24
  • Create a new bridge network and create your containers there, you'll have DNS resolution on that network. DNS resolution is only disabled on the default bridge network named "bridge".
    – BMitch
    Mar 2, 2018 at 13:38
  • As explained in detail in my initial description, using a custom network lets me reach the services by name, but prevents the containers spawned by the gitlab runner executor to locate the associated services. I have not clarified why this is happening, but I have the feeling that this is because the runner is not properly linking containers when using a custom network.
    – blueFast
    Mar 2, 2018 at 13:51
  • But I digress, and my question is simple: how do I change the name of a running container?
    – blueFast
    Mar 2, 2018 at 13:51
0

For easy updating, do it on the docker host machine. Use the options like this with your docker run commands.

-v ./hosts:/etc/hosts -v ./hostname:/etc/hostname

You’ll be able to update the file on the host machine without having to get inside the container and update the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .