Troubleshooting Docker networks

By in
Troubleshooting Docker networks

Let’s explore some fundamentals of communicating with docker containers.

Here’s a video version of this post:

Example services that we can talk with

ryanlabouve/hello-node gives us a PONG style server that we can curl and get a simple response. Let’s get that running.

docker run -d -p 3000:3000 ryanlabouve/hello-node:v1

Talking from the outside

Let’s confirm where our container is running (even though we have a good guess from the -p 3000:3000.) Running docker ps will show us which port is running inside and outside of the container.

docker ps
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ad2e0bf1-b982-43b6-9747-2e756759b62d/Screen_Shot_2020-06-18_at_10.28.57_AM.png

Now in another tab you can curl port 3000

curl localhost:3000
# => Hello world!

Now let’s stop that container.

Checking from the inside

Let’s exclude the publish option (-p) to simulate us not knowing which port the container was exposing.

docker run -d ryanlabouve/hello-node:v1

Shell into running container

Now we can shell into the container to inspect. We can execute /bin/bash on our running container to shell in.

First we can use docker ps to verify the container is running and we don’t see any port information.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/10823eab-1e3e-4dbb-998a-e6570f1fb8d9/bash_2020-06-18_16-01-42.png

Now let’s shell in via docker exec.

# Put in YOUR container id or container name
# use `docker ps` if you need to look either of these up
docker exec -it 6f7076646b58 /bin/bash

Use netstat to check what’s running

We’ll need netstat to inspect the running. This tool is included in net-tools.

apt-get update
apt-get upgrade
apt-get install net-tools

Here are the docs for netstat.

netstat -plant
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/cdca3e9e-2ad8-4677-aa7b-430aa82cd89e/docker_2020-06-18_12-51-56.png

On the last line we can see port 3000 is listening. And if we curl localhost:3000 WHILE shelled into the box we can see a response. So if we were unaware if which port to expose, this would have helped us decide.

In the next post, we’ll put all of this together and work with multiple containers and Docker compose.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.