Skip to main content

Link 2 projects on your Desktop using the API

You might occasionally want to link two activeor projectsmore ondocker your Docker desktop.containers.

When your computer wants to connect to a website, it needs to talk to a DNS server to convert the URL to an IP Address same as the Docker desktop.

YouBasically, mustwhat convert the URLneeds to anbe IPdone Addressis to edit each of the competingdocker projectcontainer's /etc/hosts file and add in orderanother tocontainer's launchIP orand connect the two projects. Simply adhere to these few procedures to accomplish this:URL.

  1. The two projects must be active on your Docker desktop.desktop and on the same network
  2. Go toEnter each project's bashcontainer
    • docker exec -ti <container-name> bash
  3. ToView viewone container's /etc/hosts file
    • cat /etc/hosts
    • (the last entry on a newly built container should be the IP addresses, open each host'container's fileIP in the network)
  4. Add it in the other container's /etcetc/hosts folder.file
    • cdecho .."<IP of the container from step 3> <URL of the container from step 3>" >> /etcetc/hosts

 

Using a script:

  1. ConvertCopy the IPfollowing addresscode orand create a hostfile ofputSourceIpIntoHosts.sh into the rivaltarget projectproject's root folder
  2. Edit CONTAINER_SRC, CONTAINER_SRC_URL AND TARGET_CONTAINER
  3. Execute the script: ./putSourceIpIntoHosts.sh

Scenario:

#!/bin/bash

In
this

# example,execute Iby want./putSourceIpIntoHosts.sh
to
# requestwill an API to read theput IP address of theCONTAINER_SRC otherinto projectwhatever fromcontainer the backend recruitment.

Project 1: Recruitment Backend

  • Container name: recruitment-backend
  • IP Address: 

Project 2: Recruitment Frontend

NOTE: for our front-ends run: docker exec -ti recruitment sh

  • Container name: recruitment-frontend

Refer to the attached photos.

Step 1

image.png

Step 2

image.png

To save your modificationsis in vim:TARGET_CONTAINER

CONTAINER_SRC=accounts-v2
CONTAINER_SRC_URL=accounts.nmsapps.local
TARGET_CONTAINER=nmsapps

1. press
CONTAINER_SRC_IP=$(docker ESC
2.inspect type
-f :wq

'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
$CONTAINER_SRC)
docker exec -it $TARGET_CONTAINER bash -c "echo '$CONTAINER_SRC_IP $CONTAINER_SRC_URL' >> /etc/hosts && cat /etc/hosts"

If VIM is not found, install by running this commands:

Front-end / sh

image.png

Back-end/bash

1. 

image.pngCredits: Sir Tyrel, Sir Jayditch, Ma'am Nicole

2.