Docker Nsenter

Does anyone know what this image is? It keeps appearing when I run “docker images” every time after a docker restart. I have ran “docker rmi 9e4f13a0901e” to delete the image but after a docker restart, it is present again. Sometimes it even has a running container. When here is a running container of this n4w/nsenter image, the docker whale displays the message that docker is starting. How is it possible that this container is running while Docker is just trying to start up?

Docker Nsenter Netstat

I have to stop the container by running 'docker stop ', then the Docker whale will show that Docker is running and a message is displayed that I can hack away with powershell.

There are differences between nsenter and docker exec; namely, nsenter doesn't enter the cgroups, and therefore evades resource limitations. The potential benefit of this would be debugging and external audit, but for remote access, docker exec is the current recommended approach. Sending build context to Docker daemon 2.56 kB Step 1: FROM rhel7 - 59fd232b7e05 Step 2: LABEL version '1.0' Description 'This is a httpd container image with systemd running' creationdate 'Tue Mar 21 11:38:54 IST 2017' - Using cache - e187a8fff761 Step 3: MAINTAINER Niranjan Mallapadi - Using. Conveniently, it comes as an image in Docker Hub. Pull the image, run the container, and use nsenter. $ docker pull jpetazzo/nsenter $ sudo docker run docker run -rm -v /usr/local/bin:/target jpetazzo/nsenter. At this time, it is useful to have a look at the Dockerfile for nsenter and check the CMD option. Nsenter always sets GID for user namespaces, the default is 0.S, -setuid uid Set the user ID which will be used in the entered namespace. Nsenter always sets UID for user namespaces, the default is 0.preserve-credentials Don’t modify UID and GID when enter user namespace. The default is to drops supplementary groups and sets GID and UID.

Below is the image and container image. Any help or info would be great. I just need to know if it is malware.
docker ps output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd33be552913 d4w/nsenter “/usr/bin/nsenter1 /b” 59 minutes ago Up 59 minutes mad_roentgen

docker images output
REPOSITORY TAG IMAGE ID CREATED SIZE
d4w/nsenter latest 9e4f13a0901e 3 months ago 83.85 kB

It has been asked on #docker-dev recently if it was possibleto attach a volume to a container after it was started.At first, I thought it would be difficult, because of howthe mnt namespace works. Then I thought better :-)

TL,DR

To attach a volume into a running container, we are going to:

  • use nsenter to mount the whole filesystem containing thisvolume on a temporary mountpoint;
  • create a bind mount from the specific directory that wewant to use as the volume, to the right location of this volume;
  • umount the temporary mountpoint.

It’s that simple, really.

Preliminary warning

In the examples below, I deliberately included the $ signto indicate the shell prompt and help to make the differencebetween what you’re supposed to type, and what the machineis supposed to answer. There are some multi-line commands,with > continuation characters. I am aware that it makesthe examples really painful to copy-paste. If you want tocopy-paste code, look at the sample script at the end of thispost!

Step by step

In the following example, I assume that I started a simplecontainer named charlie, with the following command:

I also assume that I want to mount the host directory/home/jpetazzo/Work/DOCKER/docker to /src in my container.

Let’s do this!

nsenter

First, you will need nsenter, with the docker-enterhelper script. Why? Because we are going to mount filesystemsfrom within our container, and for security reasons, ourcontainer is not allowed to do that. Using nsenter, wewill be able to run an arbitrary command within the context(technically: the namespaces) of our container, but withoutthe associated security restrictions. Needless to say, thiscan be done only with root access on the Docker host.

The simplest way to install nsenter and its associateddocker-enter script is to run:

For more details, check the nsenter project page.

Find our filesystem

We want to mount the filesystem containing our host directory(/home/jpetazzo/Work/DOCKER/docker) in the container.

Docker Nsenter Tcpdump

We have to find on which filesystem this directory is located.

First, we will canonicalize (or dereference) the file, justin case it is a symbolic link - or its path contains anysymbolic link:

A-ha, it is indeed a symlink! Let’s put that in an environmentvariable to make our life easier:

Then, we need to find which filesystem contains that path.We will use an unexpected tool for that, df:

Let’s use the -P flag (to force POSIX format, just incase you have an exotic df, or someone runs that on Solarisor BSD when those systems will get Docker too) and put theresult into a variable as well:

Find the device (and sub-root) of our filesystem

Now, in a world without bind mounts or BTRFS subvolumes,we would just have to look into /proc/mounts to find outthe device corresponding to the /home/jpetazzo filesystem,and we would be golden. But on my system, /home/jpetazzois a subvolume on a BTRFS pool. To get subvolume information(or bind mount information), we will check /proc/self/mountinfo.

If you had never heard about mountinfo, check proc.txtin the kernel docs, and be enlightened :-)

So, first, let’s retrieve the device of our filesystem:

Next, retrieve the sub-root (i.e. the path of the mountedfilesystem, within the global filesystem living in thisdevice):

Perfect. Now we know that we will need to mount /dev/sda2,and inside that filesystem, go to /jpetazzo, and from there,to the remaining path to our file (in our example,/go/src/github.com/docker/docker).

Let’s compute this remaining path, by the way:

Note: this works as long as there are no , in the path.If you have an idea to make that work regardless of thefunky characters that might be in the path, let me know!(I shall invoke the Shell Triad to the rescue: jessie,soulshake, tianon?)

The last thing that we need to do before diving into thecontainer, is to resolve the major and minor device numbersfor this block device. stat will do it for us:

Note that those numbers are in hexadecimal, and later, we willneed them in decimal. Here is a hackish way to convert themeasily:

Putting it all together

Docker Nsenter

There is one last subtle hack. For reasons that are beyond myunderstanding, some filesystems (including BTRFS) will updatethe device field in /proc/mounts when you mount them multipletimes. In other words, if we create a temporary block devicenamed /tmpblkdev in our container, and use that to mount ourfilesystem, then now our filesystem (in the host!) will appear as/tmpblkdev instead of e.g. /dev/sda2. This sounds like alittle detail, but in fact, it will screw up all future attemptsto resolve the filesystem block device.

Long story short: we have to make sure that the block device nodein the container is located at the same path than its counterparton the host.

Let’s do this:

Create a temporary mount point, and mount the filesystem:

Make sure that the volume mount point exists, and bind mountthe volume on it:

Cleanup after ourselves:

(We don’t clean up the device node. We could be extra fancyand detect whether the device existed in the first place, butthis is already pretty complex as it is right now!)

Voilà!

Automating the hell out of it

Exec

This little snippet is almost copy-paste ready.

Docker Nsenter 1

Status and limitations

This will not work on filesystems which are not based on block devices.

Docker Nsenter

It will only work if /proc/mounts correctly lists the block devicenode (which, as we saw above, is not necessarily true).

Nsenter In Docker

Docker Nsenter

Docker Nsenter Windows 10

Also, I only tested this on my local environment; I didn’t even tryon a cloud instance or anything like that, but I would love to knowif it works there or not!