Docker Community Forums

Share and learn in the Docker community.

Change hostname of an existing container


(Sean Boran) #1

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.


(Sven Dowideit) #2

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.


(Sean Boran) #3

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.


(Sven Dowideit) #4

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.


(Ajnouri) #5

As suggested by #svendowideit, from Docker google forum:

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

I got:

root@pc3:/#


(Sean Boran) #6

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


(Ajnouri) #7

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


(Assupport) #8

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


(Sean Boran) #9

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


(Assupport) #10

okay, Thanks for response.


(Youngho Lee) #11

You can commit current Docker container as new image.

Steps:

  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.

(Thomas) #12

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.


(Stevengross) #13

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


(jclosure) #14

I managed to hack around this limitation from the outside:

#!/bin/bash

#Run this on the docker host

CONTAINER_NAME=$1
NEW_HOSTNAME=$2

FILE=/etc/hosts
TMP_FILE=/tmp/hosts

HOSTNAME=`docker exec -i $CONTAINER_NAME bash -c “hostname” | tr -d ‘\n’`
docker cp $CONTAINER_NAME:$FILE $TMP_FILE
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 "EXISTING HOSTNAME: $HOSTNAME"
echo "IP: $IP"
echo "----------------"
echo "NEW HOSTNAME: $HOSTNAME"
echo "NEW HOSTS FILE"
echo "----------------"
cat $TMP_FILE
echo “----------------”

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


(Bill5ter) #15

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.


(Nilesh85) #16

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.