Linux · April 30, 2019 0

How to access server running inside docker?

Docker runs in a virtual environment so has its own network and ip management. To access server running inside docker in your browser you need to either bind the server to port 0.0.0.0 which is local host of your system or use docker ip.
You can get docker ip using below

# docker inspect some-container-id

What if servers running inside docker to access each other?

Suppose you have two servers (laravel and mongodb) running inside separate docker containers. How would laravel access mongodb?

The answer is simple, you need to see on which ip mongo is running, you can get the container id by running docker ps

# docker ps
We get docker id as “452d608387a1” Now you can inspect and get ip address of that container.
# docker inspect 452d608387a1

You can see the mongo container is running on ip “172.25.0.2” so you can now change .env file in laravel code to set host as “172.25.0.2” for mongodb

That is how you access containers inside docker.

What if you want to access server outside docker?

You can get get docker container ip as described above to run in browser

How to access laravel container inside vue container ?

Get gateway IP of laravel container

# docker inspect 7eecc32e66a0
Get the gateway IP and use that ip in your vue/.env file
docker inspect laravel container
.env.development.local inside vue app

What if you want to bind docker container ip with localhost of your host?

To bind docker container ip with your host localhost i.e 127.0.0.1, run server inside docker container as –host=0.0.0.0 where 0.0.0.0 is localhost of your host.

# npm run server --host 0.0.0.0

Still have confusion? Write me in comments below 🙂