Rob Carpenter
Rob Carpenter

Reputation: 689

Vagrant to Docker communication via Docker IP address

Here's my situation. We are slowly migrating our VMs from Vagrant to Docker but we are mostly still Docker newbs. Some of our newer system's development environments have already been moved to Docker. We have test code that runs on an older Vagrant VM and used to communicate with a Vagrant running a Django Restful API application in order to run integration tests. This Django API is now in a Docker container. So now we need the test code that runs in a vagrant to be able to make requests to the API running in docker. Both the docker container and the vagrant are running side by side on a host machine (MacOS). We are using Docker compose to initialize the docker container the main compose yaml file is shown below.

services:
  django-api:
    ports:
      - "8080:8080"
    build:
        context: ..
        dockerfile: docker/Dockerfile.bapi
    extends:
      file: docker-compose-base.yml
      service: django-api
    depends_on:
      - db
    volumes:
      - ../:/workspace
    command: ["tail", "-f", "/dev/null"]
    env_file:
      - ${HOME}/.pam_environment
    environment:
      - DATABASE_URL=postgres://postgres:password@db
      - PGHOST=db
      - PGPORT=5432
      - PGUSER=postgres
      - PGPASSWORD=password
      - CLOUDAMQP_URL=amqp://rabbitmq

  db:
    ports:
      - "5432"
    extends:
      file: docker-compose-base.yml
      service: db
    volumes:
      - ./docker-entrypoint-initdb.d/init-postgress-db.sh:/docker-entrypoint-initdb.d/init-postgress-db.sh
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: django-api-dev

I would like the tests that are running on the vagrant to still be able to communicate with the django application that's now running on docker, similar to the way it could communicate with the api when it was running in a vagrant. I have tried several different types of network configurations within the docker compose file but alas networking is not my strong suit and I'm really just shooting in the dark here.

Is there a way to configure my docker container and/or my vagrant so that they can talk to each other? I need to expose my docker container's IP address so that my vagrant can access it.

Any help/tips/guidance here would be greatly appreciated!

Upvotes: 0

Views: 2615

Answers (2)

Ayushya
Ayushya

Reputation: 10447

I would like the tests that are running on the vagrant to still be able to communicate with the django application that's now running on docker, similar to the way it could communicate with the api when it was running in a vagrant.

As you say, you are using docker-compose, so exposing ports would do the purpose you are looking for. In the yml file, where django application is defined, create a port mapping which would bind the port on host to the port in container. You can do this by including this:

ports:
  "<host_port_where_you_want_to_access>:<container_port_where_application_is_running>"

Is there a way to configure my docker container and/or my vagrant so that they can talk to each other? I need to expose my docker container's IP address so that my vagrant can access it.

It is. If both the containers are in the same network (when services are declared in same compose file, all services are in same network by default), then one container can talk to other container by calling to their service name.

Example: In the yml file specified in question, django-api can access db at http://db:xxxx/ where xxxx can be any port inside container. xxxx need not be mapped to host or need not be exposed.

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

In your Vagrantfile, make sure you have a private host only network. I usually use them with a fixed IP

config.vm.network "private_network", ip: "192.168.33.100"

Now both the VMs will get a static IP in the host only network. When you run docker-compose up -d in your Django VM. Your VM will map port 8080 to the container 8080. So you can use 192.168.33.100:8080 in the other VM for testing the APIs

Upvotes: 2

Related Questions