Docker run - how to share UTS namespace between containers?

1.3k views Asked by At

Docker run command by default uses a dedicated UTS namespace for the container and because of it the container gets its own/unique hostname. I am trying to share the UTS namespace between two containers but it seems that it is not possible with docker run command.

Following are the commands that I ran -

docker run -d --name container1 alpine sleep infinity
docker run -it --name container2 --uts container:container1 alpine /bin/sh

Error -

docker: --uts: invalid UTS mode.

Based on the documentation, it looks like it is not possible to reference a container with the --uts flag. This is not the case with other namespace related flags like --pid, --network, etc. They support referencing other containers. Why "container:<name|id>” mode is not supported by the --utc flag? How to share UTS namespace between containers so that they share hostname?

1

There are 1 answers

0
atline On

I don't know how to share UTS namespace between containers. But, if your final aim is just to share hostname between containers, next should work if you not care about network share:

shubuntu1@shubuntu1:~$ docker run -idt --name container1 ubuntu:18.04 /bin/bash
3b024de861049c63852e5b196b8730c23ccba8454eb894aa1159c046dd35043e
shubuntu1@shubuntu1:~$ docker run -idt --name container2 --net container:container1 ubuntu:18.04 /bin/bash
ee3d696a589bb7aa3000cec7587f4b920088edf829f8cda029b019316451e92f
shubuntu1@shubuntu1:~$ docker exec -it container1 hostname
3b024de86104
shubuntu1@shubuntu1:~$ docker exec -it container2 hostname
3b024de86104

It let container2 use the same network of container1, refers to this. FYI in case this help.

Another way maybe just expose /etc/hostname to bind mounts or volume, and let all containers which you needed to use the same one, then you no need to share net.