saadoune
saadoune

Reputation: 419

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?

Upvotes: 6

Views: 17010

Answers (2)

GAURAV SHARMA
GAURAV SHARMA

Reputation: 21

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

Upvotes: 2

CSJ
CSJ

Reputation: 2957

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

Upvotes: 5

Related Questions