Abdul Samad
Abdul Samad

Reputation: 5918

Accessing running container (inside VM) from host OS

I have set up 3 Linux VM through multipass on my host Mac OSX. I have installed docker on these machines and started a 3 node docker swarm.

docker swarm init

After that, I have created 2 services one is for Postgres DB and the other is for drupal with the following command.

docker service create -d --name postgras-db --network test-swarm-network -e POSTGRES_PASSWORD=<password> postgres

docker service create -d  --name drupal-frontend --network test-swarm-network -p 80:80 drupal

Port 80:80 is exposed for drupal and both of these services are connected to the same overlay network. My services are up and running.

If I am doing everything on the host machine then I will simply do http://localhost:80 or http://localhost to get the desired output but now when running it in VM how I can test the drupal front from host MAC, i.e which IP to hit in the host browser to get the desired result.

P.S: There is nothing specific here about drupal (it could be any other container like Nginx etc), the question is about accessing running container (inside VM) from host OS

Upvotes: 0

Views: 991

Answers (2)

Abdul Samad
Abdul Samad

Reputation: 5918

I figured this out myself. For other help:

We can get the info of a running VM by running the following command on host machine.

multipass info <VM_Name> 

like

multipass info docker-vm 

it will give the info about the VM which also contains the IP on which VM is running

multipass info docker-vm
Name:           docker-vm 
State:          Running
IPv4:           192.168.xx.x
                172.xx.x.x
Release:        Ubuntu 20.04.3 LTS
Image hash:     3d7282d3e92b (Ubuntu 20.04 LTS)
Load:           0.08 0.15 0.09
Disk usage:     2.2G out of 4.7G
Memory usage:   264.6M out of 976.9M

From here user can copy the IP address and paste it into the host browser which will hit port 80 (by default) of the VM.

Thanks

Upvotes: 0

You can use below command:

 docker exec -it container_name /bin/bash 

Upvotes: 0

Related Questions