Entering a containers shell environment

Sometimes it's necessary to open up a shell into a container for debugging purposes. This should really never be needed in production, but sometimes needs must.

We already have a mongo container, so to begin with let's start it:

$ docker run --name=gazorpzorp -p 127.0.0.1:27017:27017 -d mongo

We gave the container a name (gazorpzorp), so now we can open a shell within the container and log into like so:

$ docker exec -ti gazorpzorp /bin/bash
Container IDs
Another way to reference a container (especially if we've neglected to name it) is using the container identifier, which can be located in the output of the docker ps command.

This will drop us into a root prompt inside the container and we can now run commands to diagnose issues.

For example:

root@88d2d16c08fe:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
mongodb 1 0.9 2.8 268452 57632 ? Ssl 12:41 0:00 mongod
root 28 0.0 0.1 20248 3196 ? Ss 12:41 0:00 /bin/bash
root 32 0.0 0.1 17500 2064 ? R+ 12:42 0:00 ps aux
root@88d2d16c08fe:/# netstat -an
bash: netstat: command not found

We can see that the Mongo daemon is running. However, netstat isn't available on the system (whereas it usually would be). Most containers are stripped down since they don't require the usual command-line tools.

We can alter containers on the fly:

root@88d2d16c08fe:/# apt-get update
root@88d2d16c08fe:/# apt-get install net-tools
root@88d2d16c08fe:/# netstat -an | grep -i listen
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
unix 2 [ ACC ] STREAM LISTENING 22078 /tmp/mongodb-27017.sock

We can use the containers package manager to install software packages while the container is running to debug, understand, and solve issues.

Once we kill and start a fresh container from the mongo image our changes will disappear of course.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.194.170