In partnership with

This is a series called Save Intern Ruby, where you help Ruby debug her program.

Ruby is an intern and a fresh graduate.

Ruby wrote this compose.yaml file

services:
  web:
    image: nginx
    ports: 
      - "3000:80"

  demo-tools:
    image: busybox
    # This keeps the container running so you can exec into it
    command: sleep infinity 

She started the containers docker compose up -d, Both are running properly.

 ✔ Container web-1         Started                                              
 ✔ Container demo-tools-1  Started  

She wants to send a GET request from the busybox container to the nginx container like this:

docker compose exec demo-tools wget http:// web:3000

(This runs wget in the busybox container shell)

Ruby expects an output like this:

<title>Welcome to nginx!</title>

But instead, she got this:

wget: can't connect to remote host (172.18.0.3): Connection refused 

What went wrong?

Help her fix the problem.

Solution

It's a networking issue.

The reason why it didn't work is because how exposing ports work.

When you expose a port like this:

ports: 
  - "3000:80"

You are forwarding the container's port 80 to the host's port 3000.

There's nothing running on the Nginx container's port 3000. That's why this request doesn't work:

docker compose exec demo-tools wget http:// web:3000

Because we're trying to send a request to the container directly, not to the host, we should be querying port 80:

docker compose exec demo-tools wget http:// web:80

Keep Reading