Docker Community Forums

Share and learn in the Docker community.

Change hostname of an existing container

I cannot find a way to change the hostname of an existing container, neither with docker start, the API, or running “hostname XXX” in a startup script.

Actaully would also be nice to change enviroment variables in existing containers and not just when creating.

1 Like

Can you give some reasons why this is important? So far, I rarely keep a container long enough to need to rename it - I start new ones based on the common image.

I’m creating containers for micro websites, the containers will live days, months or more. having a correct FQDN hostname is important for this use case.

having it correct makes sense - the question is really about changing it - it might be worth asking in the docker-users google group in case someone else has worked out a process that works.

1 Like

As suggested by #svendowideit, from Docker google forum:

Add '–hostname {name}’ when running a container

I got:


That is for new, not existing containers. There is no such argument for “docker start”.

It is docker run argument:

docker run --hostname {hostname} --name {name} {image}

From help

docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Run a command in a new container

-h, --hostname= Container host name

Hi @boran ,
Did you get any solution to this case?

Nope, that’s just a limitation one has to live with. To change the hostname would has to rebuild the container.

okay, Thanks for response.

You can commit current Docker container as new image.


  1. stop current container
  2. commit current container as image ($ docker commit -a “Your Name” current_container_name New_image_name:Version
  3. docker run new container by New image with --hostname option.
  4. remove as-is container & image.

I know this post is from a long time ago, but maybe this helps people that find this thread.

Generally the hostname is saved in the etc/hostname file on the container.

You could go into the container (docker exec -it <container_id> /bin/bash) and change this file.
I don’t know if this change requires a container restart to take effect.

I have heard about LongPathTool program, it might help you.

I managed to hack around this limitation from the outside:


#Run this on the docker host



HOSTNAME=`docker exec -i $CONTAINER_NAME bash -c “hostname” | tr -d ‘\n’`
IP="$(grep $HOSTNAME $TMP_FILE | tr -d ‘\n’ | awk ‘{print $1}’)"
sed -i “/$HOSTNAME$/d” $TMP_FILE
echo -e “$IP\t$NEW_HOSTNAME” >> $TMP_FILE

echo "----------------"
echo "IP: $IP"
echo "----------------"
echo "----------------"
echo “----------------”

docker exec -it $CONTAINER_NAME bash -c "hostname ‘$NEW_HOSTNAME’"
docker exec -it $CONTAINER_NAME bash -c “cat ${TMP_FILE} > ${FILE}”

I found a very simple way of achieving this. Install kitematic. This tools allows you to change the hostname - it will restart - after restarting it will be the newly applied hostname.

I found the answer for changing the docker hostname after the container has been running or I can say to the existing container here are some steps

  1. Run docker inspect -f ‘{{ .State.Pid }}’ <existing_docker_hostname>

Output will be some number <15580>

  1. Run this command to login to the container nsenter --target 15580 --uts

  2. Run this command to change the hostname hostname "node_js"

now exit the container and login again you will see the hostname has been changed.


Thanks for sharing @nilesh85


It does not survive reboot.