Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change hostname after running a container

I am new to docker, i am using docker version 1.12. I am trying to modify the hostname of a running container, however the hostname command returns hostname: you must be root to change the host name . Is there any way to achieve that after running the container and not in the docker run command?

like image 1000
saadoune Avatar asked Nov 27 '16 16:11

saadoune


People also ask

How do I change the hostname of the container?

1 Run this command to login to the container nsenter --target 15580 --uts 2 Run this command to change the hostname hostname "node_js" More ...

How do I set the hostname of a docker container?

You're creating a new container You can set the hostname on the command line: docker run --rm -h "example.com" -t -i ubuntu bash #... root@example:/# hostname example.com Your container is already running

How to set after start/run the container?

By following Below Steps you can set after start/run the container. 1.Stop container and service sudo docker stop CONTAINER_NAME sudo service docker stop 2.Edit config file (JSON) [You should always make backup first] /var/lib/docker/containers/CONTAINER_ID/config.json

How to create a stack of containers with specific hostnames and addresses?

You can use docker-compose tool to create a stack of containers with specific hostnames and addresses. Here is the example docker-compose.yml with specific network config: version: "2" services: host1: networks: mynet: ipv4_address: 172.25.0.101 networks: mynet: driver: bridge ipam: config: - subnet: 172.25.0.0/24


2 Answers

Some discussions here: https://github.com/docker/docker/issues/8902

What I got from above discussion is

  1. add SYS_ADMIN cap when run the container: https://github.com/docker/docker/issues/8902#issuecomment-218911749

  2. use nsenter https://github.com/docker/docker/issues/8902#issuecomment-241129543

like image 119
CSJ Avatar answered Oct 24 '22 18:10

CSJ


By following Below Steps you can set after start/run the container.

1.Stop container and service

sudo docker stop CONTAINER_NAME

sudo service docker stop

2.Edit config file (JSON) [You should always make backup first]

/var/lib/docker/containers/CONTAINER_ID/config.json

Replace

"Hostname":"WHATEVER"

with

"Hostname":"NEW_HOSTNAME"

3.Start container and service

sudo service docker start

sudo docker start CONTAINER_NAME

like image 1
GAURAV SHARMA Avatar answered Oct 24 '22 18:10

GAURAV SHARMA