Sunday, December 17, 2017

Debuging Your Docker Image

When building and testing a docker container it often does not work on the first try. When this happens to me one of the first things I do is to run Bash inside of the container. Then once in that container I run whatever command I expect the image to be running. This often will grant some insights into what is happening.

To run Bash from almost any container you can do something like this:

docker run -it --rm --entrypoint=/bin/bash image_name_here

The -it portion of the command basically asks Docker to run in the foreground and allow the user to interact with it. More details can be found in the Docker run reference.

The --rm tells Docker to remove the container after it exits. This isn't strictly needed but it's probably a good idea. It will help keep your system from filling up with old single use containers. Again, more details can be found in the Docker run reference.

Then, the --entrypoint=/bin/bash is the part that allows you to override the ENTRYPOINT and COMMAND entries in the Dockerfile and supply something new to run—in this case Bash. This is also documented in the Docker run reference.

Of course, when you are doing this you will also need to add any options you need to mount volumes or expose ports.