Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access service running in docker

I'm unable to access a nodejs based service via http://localhost:8000 running in a docker image. I'm using Docker for Mac (https://docs.docker.com/docker-for-mac/)

I'm following the tutorial here https://nodejs.org/en/docs/guides/nodejs-docker-webapp/.

The server runs on port 8000. I start the docker image with the following:

$ docker run -p 8000:8000 -d geuis/node-server:latest

If I run docker ps I see:

CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                    NAMES
9fa2e446918b        geuis/node-server:latest   "npm start"         6 seconds ago       Up 5 seconds        0.0.0.0:8000->8000/tcp   unruffled_lewin

If I docker exec -it 9fa2e446918b /bin/bash I can access the docker vm and I can curl http://localhost:8000 and access the server from inside the container.

However, I try the same curl http://localhost:8000 from my system terminal and its not accessible.

Not sure what I need to do next.

like image 335
Geuis Avatar asked May 19 '17 03:05

Geuis


People also ask

How do I access docker service?

To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. If you have a service running on some other port you can access it via 172.17. 42.1:5432.

How do I start a service in a docker container?

Run Docker Container as a Service Docker team recommends to use cross-platform built-in restart policy for running container as a service. For this, configure your docker service to start on system boot and simply add parameter --restart unless-stopped to the docker run command that starts YouTrack.

Can you ssh into a running docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.


1 Answers

Try the following listen statement:

app.listen(PORT, '0.0.0.0');

From reading the tutorial you mention it looks like express is listening on localhost. This is fine if you're running locally but inside of a container, localhost is not the same localhost that's outside of the container.

0.0.0.0 is the unspecified IPv4 address and so Express will bind on any IP it can find, which will be the IP that your requests are coming in from outside the container.

like image 170
otupman Avatar answered Oct 30 '22 03:10

otupman