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?
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 ...
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
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
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
Some discussions here: https://github.com/docker/docker/issues/8902
What I got from above discussion is
add SYS_ADMIN
cap when run the container:
https://github.com/docker/docker/issues/8902#issuecomment-218911749
use nsenter
https://github.com/docker/docker/issues/8902#issuecomment-241129543
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With