Link 2 projects on your Desktop using the API
You might occasionally want to link two or more docker 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.
Basically, what needs to be done is to edit each of the docker container's /etc/hosts file and add in another container's IP and URL.
- The projects must be active on your Docker desktop and on the same network
- Enter each project's container
docker exec -ti <container-name> bash
- View one container's /etc/hosts file
cat /etc/hosts
- (the last entry on a newly built container should be the container's IP in the network)
- Add it in the other container's /etc/hosts file
echo "<IP of the container from step 3> <URL of the container from step 3>" >> /etc/hosts
Using a script:
- Copy the following code and create a file putSourceIpIntoHosts.sh into the target project's root folder
- Edit CONTAINER_SRC, CONTAINER_SRC_URL AND TARGET_CONTAINER
- Execute the script: ./putSourceIpIntoHosts.sh
#!/bin/bash
# execute by ./putSourceIpIntoHosts.sh
# will put IP of CONTAINER_SRC into whatever container is in TARGET_CONTAINER
CONTAINER_SRC=accounts-v2
CONTAINER_SRC_URL=accounts.nmsapps.local
TARGET_CONTAINER=nmsapps
CONTAINER_SRC_IP=$(docker inspect -f '{{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"
Credits: Sir Tyrel, Sir Jayditch, Ma'am Nicole
No Comments