Random thoughts and things I find useful.
by Manuel Lopez
Making http calls from Docker containers to applications in other containers.
Lets create containers that fire up a python http server.
~$ mkdir tmp && cd tmp
~/tmp$ echo ContainerA > indexA.html && echo ContainerB > indexB.html
Create the files a.Dockerfile
FROM python:3.5.9-alpine3.12
WORKDIR /app
COPY indexA.html index.html
CMD [ "python", "-m", "http.server" ]
and b.Dockerfile
FROM python:3.5.9-alpine3.12
WORKDIR /app
COPY indexB.html index.html
CMD [ "python", "-m", "http.server" ]
Create the images
docker build -t image-a -f a.Dockerfile . && docker build -t image-b -f b.Dockerfile .
Lets run image-a
on port 8000
docker run -p 8000:8000 -it --rm --name image_a image-a
and image-b
on port 9000
docker run -p 9000:8000 -it --rm --name image_b image-b
In the host machine we can open up a terminal and do
~$ curl http://localhost:8000
ContainerA
~$ curl http://localhost:9000
ContainerB
From within the container we should be able to do the same
~$ docker exec -it image_a sh
/app # apk add curl
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/3) Installing nghttp2-libs (1.41.0-r0)
(2/3) Installing libcurl (7.69.1-r0)
(3/3) Installing curl (7.69.1-r0)
Executing busybox-1.31.1-r16.trigger
OK: 10 MiB in 28 packages
/app # curl http://localhost:8000
ContainerA
/app # curl http://localhost:9000
curl: (7) Failed to connect to localhost port 9000: Connection refused
How can we reach the server located in image_b
container?
Use the IP address of the host
/app # curl http://192.168.1.21:9000
ContainerB
The above does not work on mac. Funny enough, if we obtain the IP address from the OS X machine and curl port 9000 from a linux box we obtain the file we want but the containers running inside OS X cannot connect to any other container. Why?
tags: docker - network