问题
I'm sorry if this turns out to be obvious, but I'm having a hard time accessing a dockerized server from outside the container on the host. Here's what I'm doing:
I'm running the container with the following command:
docker run -it --rm --name aurelia_dev -p 8888:8080 -v /Users/terskine/git/marvel/legendary:/app aurelia /bin/bash
Within the container I'm running the app:
cd app/
au run
In a separate terminal, I can see the container is running:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b0a14bc6e88 aurelia "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8888->8080/tcp aurelia_dev
However, I can't reach the server:
$ curl http://localhost:8888
curl: (52) Empty reply from server
But within the container, I can reach the server:
$ docker exec -it aurelia_dev /bin/bash
root@4b0a14bc6e88:/# curl http://localhost:8080
<!DOCTYPE html> ...
<html>
... [A whole bunch of HTML] ...
</html>
root@4b0a14bc6e88:/#
Why can't I access the server from outside the container on the host's port 8888?
I'm sure I'm missing an obvious step. Please help me out.
Thanks!
Edit 1: I don't think the following is necessary, but If it's helpful, I'm running a simple aurelia container which I created with the following Dockerfile:
FROM node
RUN npm install -g aurelia-cli
CMD /bin/bash
The aurelia project that I'm running is the official tutorial project which can be found here: https://aurelia.io/docs/tutorials/creating-a-contact-manager
Edit 2:
This is the content of my hosts file.
root@d7d1cc03a5c0:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 d7d1cc03a5c0
And it would appear I'm not listening on 172.17.0.2:8080
root@d7d1cc03a5c0:/# curl 172.17.0.2:8080
curl: (7) Failed to connect to 172.17.0.2 port 8080: Connection refused
回答1:
I have answered that on the other thread, but for other people just searching for an answer here it is:
you could override host
and port
in almost all aurelia commands, au run
is one of those,
Examples:
au run --host 0.0.0.0 --port 7070
au run --watch --host 127.0.0.1 --port 7080
the same set of flags also exists for au cypress
and au protractor
, besides, you can also tell au
to bring up and tear down the application before and after running tests, again on a different port and IP address if you desired so.
Example:
au cypress --run --start --port 7070 --host 127.0.0.1
au protractor --headless --start --port 7070 --host 127.0.0.1
checkout here and here
Also, you could check the documentation on Aurelia Build Systems and my blog post on Medium
回答2:
This turned out not to be a problem with my Docker configuration. Rather it was a problem with webpack, which is used by Aurelia. Webpack won't work with localhost
in a docker container, please checkout here.
To fix the problem, you must specify 0.0.0.0
as the host rather than using the default, which is localhost
.
au run --host 0.0.0.0
Note: I asked a question about the real problem here and answered it, so it would be easier to find for others who have the same problem in the future, but I'm leaving this question because there may be people like me who think this is a docker config problem.
来源:https://stackoverflow.com/questions/58683987/docker-port-not-mapping