You can access the port from inside the container.
Check with the $ curl URL
command that displays the contents of the WEB page.
There are two connection methods: (1) specifying the container port and (2) specifying the host port.
-Docker exec -it [container name] bash
You can now access the bash inside the container. Depending on the container, it may be sh instead of bash.
▼ When accessing the container port directly
-Curl http: // [container name]: [port on the container side]
▼ When accessing the port on the host side
-Curl http://host.docker.internal: [host side port]
Check the running container
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4ae7996f53c django_web "python manage.py ru…" 10 hours ago Up 10 hours 0.0.0.0:8100->8000/tcp django_web_1
・ 0.0.0.0:8100-> 8000 / tcp
This is important!
"Port number on the host side-> Port number in the container"
"0.0.0.0" represents access from all ports, not localhost.
So, "0.0.0.0:8100-> 8000" means to map all access to 8100 to port 8000 in the container.
Enter the container
$ docker exec -it django_web_1 bash
root@d4ae7996f53c:/code#
Start bash with container name "django_web_1".
python
root@d4ae7996f53c:/code# curl http://django_web_1:8000
http: // container name: port number on the container side
, you can access the port specified by the container.
Error example
root@d4ae7996f53c:/code# curl http://django_web_1:8100
curl: (7) Failed to connect to django_web_1 port 8100: Connection refused
python
root@d4ae7996f53c:/code# curl http://host.docker.internal:8100
host.docker.internal
is the DNS prepared by docker. Corresponds to localhost on the host side.
This is from the outside of the container
Same as running curl http: // localhost: 8100
.
Get out of the container
root@d4ae7996f53c:/code# exit
$
that's all.
Recommended Posts