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 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 “” so you can now change .env file in laravel code to set host as “” 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, run server inside docker container as –host= where is localhost of your host.

# npm run server --host

Still have confusion? Write me in comments below 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s