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

  • Docker running on Centos7 with a Windows 7 container
  • Install Oracle on docker centos container: Insufficient disk space
  • Adding a new NIC to a Docker container in a specific order
  • Error response from daemon: chtimes /var/lib/docker/tmp/docker-export-$: invalid argument
  • Ansible, running role multiple times with different parameter sets
  • Can not load libjli.so in 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.

  • Using curl with success while running containers
  • How can I configure environment variables on a Docker container without hard-coding them in the Dockerfile? [closed]
  • Host screen turns off when I start X server in docker container
  • Docker - Backup/export of data container volume used by another container
  • Linux Networking - Docker Container to Remote Host via Gateway without using route add?
  • Docker Swarm: Does it let two containers in different hosts to communictae?
  • 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.