Weijing Jay Lin
Weijing Jay Lin

Reputation: 3238

How can I setup a container with container name as computer name?

For example, when I login to my container

docker exec -it vuejs_ci bash  
root@3dc77c3403c8:/# 

It is using container ID 3dc77c3403c8 as computer name. How can I make it root@container_name when I setup my container?

Upvotes: 0

Views: 787

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29608

It is not possible from the docker exec command, but with the docker run command, you can pass a --hostname <name> to set your container's host name.

$ docker run -it --hostname container_name ubuntu:18.04 bash
root@container_name:/#
root@container_name:/# echo $HOSTNAME
container_name
root@container_name:/# hostname
container_name

By default, containers use their container ID for their hostname. From https://docs.docker.com/config/containers/container-networking/#ip-address-and-hostname:

In the same way, a container’s hostname defaults to be the container’s ID in Docker. You can override the hostname using --hostname.

Upvotes: 1

Related Questions