Post

Docker - use tools on host in container

My docker container doesn’t have a tool (e.g., telnet) installed. How do I run that tool without installing it in the container?

That tool (say, telnet) is already available on the host. nsenter allows us to run tools (like telnet) that are on the docker host (but not in docker container) within the container 🤯

In the past, I’d go into the container, install the tool (telnet), use it, the forget about it.

Now, I use nsenter to run telnet in a container that doesn’t have telnet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# display docker containers
julius@docker-host:$> docker ps
# output
CONTAINER ID   IMAGE        COMMAND                  CREATED        STATUS
74080122d165   namshi/smtp  "/bin/entrypoint.sh …"   24 hours ago   Up 2 hours
# declare variables
julius@docker-host:$> docker_container_id="74080122d165"
julius@docker-host:$> some_domain="example.com"
# run nsenter
# $(docker inspect --format  returns the IP of the docker container
julius@docker-host:$> nsenter \
  --target $(docker inspect --format {{.State.Pid}} $docker_container_name) \
  -n telnet $some_domain 443
# output
Trying 93.184.216.34...
Connected to example.com.
Escape character is '^]'.
This post is licensed under CC BY 4.0 by the author.