Sometimes you may want to connect an application running inside your docker container with some other application running on your localhost system.
For example, suppose you are running a web-server (like httpd or ngnix) inside your docker container and you want to connect your web-server with your database (like mysql) running in your localhost system. There are multiple options to connect to localhost from within a docker container and I will discuss the easiest one.
If your host machine is running on Windows or MAC, you can use host.docker.internal:<db_port> in your database connection string instead of localhost or 127.0.0.1
If your host machine is running on Linux, while running the container use –network=”host” in your docker run command, then use 127.0.0.1:<db_port> in your database connection string. You can also use the host.docker.internal:<db_port> in your database connection string provided you started your Docker container with the –add-host host.docker.internal:host-gateway option.
docker run -d --network=host <container_name> <image_name:tag>