Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to Docker container from host

Tags:

docker

Just using Docker for the first time so I'm probably making a rookie mistake, but here goes. I am trying to use the reactioncommerce/reaction image, and it appears to run correctly. However, I cannot seem to connect to the server from the host.

I am running docker run -p :8080 -it reaction as suggested on the Docker Hub page, then trying to access it by going to http://localhost:8080 on a browser on the host, but no connection can be made. Where am I going wrong?

I'm running on a Linux Mint host.

like image 739
tsvallender Avatar asked Dec 25 '22 10:12

tsvallender


2 Answers

I think your problem will be your -p (publish) flag. Assuming your container is actually listening on port 8080 - try -p 8080:8080 which will map localhost:8080 to your container. (Well, technically it'll map 0.0.0.0:8080 which is all addresses - including external)

But I think if you're not specifying something on the left hand side, you're getting a random port number mapped - you should be able to see this in docker ps or using the docker port command.

When you run docker run -it you start it interactively - and it should start 'whatever is defined in the docker file' unless you specify otherwise. I assume this will be a service you want, but I don't know that app. You can also use the -d flag, that runs the container in the background.

like image 78
Sobrique Avatar answered Jan 08 '23 00:01

Sobrique


I believe that this command will work:

docker run -p 127.0.0.1:8080:8080 -it reaction

I tried to do the same thing with a local webserver, but wasn't able to connect to my container using localhost until I added the address 127.0.0.1 to my -p command.

Your /etc/hosts file should have the following entry:

127.0.0.1    localhost

This will resolve the domain localhost allowing you to use it to access your server.

like image 43
Dave F Avatar answered Jan 08 '23 00:01

Dave F