[Docker] How to access the host from inside the container. http://host.docker.internal:

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.

Access procedure

  1. Enter the running container

-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.

  1. Request http with curl

▼ 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]

Illustration

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".


### When accessing the container port directly

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.


If you specify the port on the host side by mistake, an error will occur.

Error example


root@d4ae7996f53c:/code# curl http://django_web_1:8100
curl: (7) Failed to connect to django_web_1 port 8100: Connection refused

### When accessing the port on the host side Use `host.docker.internal: host-side port number` to access the host-side port.

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

[Docker] How to access the host from inside the container. http://host.docker.internal:
[Docker] Copy files from docker container to host
How to check the logs in the Docker container
How to get a heapdump from a Docker container
Copy files from docker container to host (docker cp)
[Docker context] ~ How to access docker in remote environment from VScode ~
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
Investigate the replacement from Docker to Podman.
Access MySQL on a Docker container from a local (host OS) Java program
Setting to exit from Docker container with VScode
How to update pre-built files in docker container
How to mount the batch file location via WSL2 and start the Docker container
How to display the amount of disk used by Docker container for each container
I was addicted to not being able to connect to AWS-S3 from the Docker container
How to install Docker
3. Create a database to access from the web module
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
[Java] How to extract the file name from the path
Communicate from the outside to the container launched by docker-compose
How to determine the look-ahead request (Prefetch) from the browser
[Docker] How to see the contents of Volumes. Start a container with root privileges.
Let's write how to make API with SpringBoot + Docker from 0
How to transition from the [Swift5] app to the iPhone settings screen
[JDBC] I tried to access the SQLite3 database from Java.
Wait for the container service to start with docker healthcheck
[IOS] How to get the table name from AWS DynamoDB
Migrating from vargrant to docker
How to get inside a container running on AWS Fargate
How to create a form to select a date from the calendar
Nginx container that displays the Hostname of the host running Docker
How to get the longest information from Twitter as of 12/12/2016
Learn how to customize the Navigation Bar from the sample app
How to migrate from JUnit4 to JUnit5
[Docker] How to forcibly disconnect the session connected to the database [Postgres]
How to apply C code format from the command line
How to solve the problem that you can not pull image from docker hub with Minikube
When requested access to the resource is denied when pushing with Docker
How to set the IP address and host name of CentOS8
Use Docker and Keycloak to specify the access token and execute the API
Check the contents by docker pull from ECR and start the container
How to run a Kotlin Coroutine sample from the command line
I examined the concept of the process to understand how Docker works
[Note] How to restart the Windows container set up with docker-compose
[GCP] Until you push the local Docker image to the Container Registry
[Java] How to convert from String to Path type and get the path
How to use the link_to method
Introduction to Linux Container / Docker (Part 1)
How to use the include? method
How to push from Tarminal to GitHub
How to use the form_with method
How to find the average angle
How to make a Java container
How to use the wrapper class
Introduction to Linux Container / Docker (Part 2)
[Artifactory] How to use Docker repository
How to add the delete function
Pass environment variables to docker container
[Docker] Operation up to container creation # 2
Update MySQL from 5.7 to 8.0 with Docker
How to start Camunda with Docker