Error while changing hostname inside docker container

I am trying to change the hostname of a docker container running centos using the command

hostnamectl set-hostname test.ovirt.org

But it gives me back a traceback of

  • Ansible cannot import docker-py even though it is installed (Ansible 2.3.0.0)
  • docker-compose build error: command 'gcc' failed with exit status 4
  • Ansible roles task failing docker swarm node join
  • Deploying with Docker into production: Zero downtime
  • Alternating provisioning with Vagrant
  • How to check the configuration of an app deployed on docker container
  • Could not set property: Failed to execute program /lib64/dbus-1/dbus-daemon-launch-helper: Success
    

    with an exit code of 1.

    Here is the relevant travis-CI build traceback

    • https://travis-ci.org/rhevm-qe-automation/ovirt-ansible/jobs/242733186#L6181-L6210

    EDIT:

    I am running the command to change the hostname inside an ansible task inside Travis build. Editing the hostname using an interactive mode wouldn’t be an option.

  • Vagrant up --no-parallel flag meaning
  • Where does docker ps Command look for current containers?
  • Docker not starting “ could not delete the default bridge network: network bridge has active endpoints”"
  • How can I increase my Docker Sonarqube disk space to avoid this warning?
  • Docker: How do I pull a specific build-id?
  • Error instantiating Spring Configuration class from properties file when running in Docker container
  • One Solution collect form web for “Error while changing hostname inside docker container”

    If you are looking to name the container as per your requirement you can make use of argument –hostname while running the container.

    # docker run --hostname=my-server -it ubuntu:15.0 /bin/bash
    root@my-server:/# hostname
    my-server
    root@my-server:/# 
    

    EDIT1 : As you need to change during the runtime, container needs to have root privileges to change the hostname. By default Docker containers will have limited root permissions.

    When you try to change the hostname following error may occur saying that you should have root permissions:

    # docker run -it ubuntu:15.0 /bin/bash
    root@92b712251220:/# hostname
    92b712251220
    root@92b712251220:/# hostname new-server
    hostname: you must be root to change the host name
    root@92b712251220:/# 
    

    When you run the same container with –privileged option which means running the container with all the root privileges, you will be able to change the hostname:

    # docker run -it --privileged ubuntu:15.0 /bin/bash
    root@08c70ab0c9e3:/# hostname new-server
    root@08c70ab0c9e3:/# hostname
    new-server
    root@08c70ab0c9e3:/# 
    

    Ideally it is not the best practise to run the container with –privileged option which gives full control of host machine which is not secured.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.