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

But it gives me back a traceback of

  • Why would I need to run docker privileged to access host log4j2.yaml
  • Creating/Running a docker container from within a c# program (under mono)
  • Ansible docker_container 'no Host in request URL', docker pull works correctly
  • Start only one service in a docker-compose.yml with ansible docker_service module
  • Run Docker container with preset IP from Ansible
  • Minimize CentOS-based Docker image
  • 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



    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 shared runners to access the gitlab registry
  • How to migrate an existing service docker?
  • Meteor Up Docker and Graphicsmagick
  • How to build an Image using Docker API?
  • lxc-kill: failed to get the init pid
  • Localhost connect MySQL Docker confusion
  • 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

    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
    root@92b712251220:/# hostname new-server
    hostname: you must be root to change the host name

    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

    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.