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.
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.
Now let’s shell in via
# 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
netstat to inspect the running. This tool is included in
apt-get update apt-get upgrade apt-get install net-tools
Here are the docs for netstat.
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.