Docker Container Links (James Lee)
- how to connect services that run in different containers
- (in Docker Image section, wrote Dockerfile that had Flask (aka dockerapp) service in one container using local memory). What if want 2+ services? Use multiple containers & link
- Recipient container can get information FROM source container
- Links established using --link flag AND container names
- Docker creates a secure tunnel that doesn’t need to expose any ports externally on the containers (note, do not need to use -p flag when spinning up source container - redis)
Steps to set up a link
- Modify dockerapp’s Dockerfile so that dockerapp installs not just Flask library but also redis client library (note, this is just the library. NOT the images). Redis service will be built from its own image. We need this so Dockerapp has a redis client (can talk to redis service/container).
Dockerapp Dockerfile:
FROM python:3.5 → base image is python:3.5
RUN pip install Flask==0.11.1 redis 2.10.5 → installs Flask libr & redis client libr (NOT img)
RUN useradd -ms /bin/bash admin → adds new user, admin.
USER admin -ms: adds default home dir for new user
WORKDIR /app /bin/bash: sets default shell to “bash”
COPY app /app
CMD ["python", "app.py"]
- Start redis service by downloading existing “official” redis image from Docker Hub
- $ docker run -d --name redis redis:3.2.0 → note named container “redis”
- Build dockerapp image from Dockerfile.
- $ docker build -t dougwells/dockerapp:v0.3.1 .
- Note name it “tag it” dougwells/dockerapp:v0.3.1
- Run dockerapp container & link to redis (note: redis container is already running)
- docker run -d -p 3000:5000 --link redis dougwells/dockerapp:v0.3.1
- (note: secure tunnel between containers. No external ports exposed to link containers. -p flag is URL for app. No -p when spun up redis container)
- docker run -d -p 3000:5000 --link redis dougwells/dockerapp:v0.3.1
- Look up docker-machine’s IP ($ docker-machine ls). Can visit dockerapp at that IP on port 3000 (set above)
- Start redis service by downloading existing “official” redis image from Docker Hub
How does container link work?
- To see IP of containers, log into the running recipient container (dockerapp). Mapping of host IP addresses is in /etc/hosts file
- $ docker exec -it <cont_id> bash
- To see IP of containers, log into the running recipient container (dockerapp). Mapping of host IP addresses is in /etc/hosts file
/: $ cd .. → move up to root directory
/: ls → see all directories (want etc/hosts
/ more etc/hosts → open hosts file inside etc directory
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 redis 7007a28ed576 → container_id
172.17.0.2 7a7bb4e21e6a
- Inspect redis container
- $ docker inspect redis
- Inspect redis container
"Networks": {
"bridge": { "Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
- Can also verify redis ip or 172.17.03 by logging into running dockerapp container & pinging redis
- $ docker exec -it 7a7 bash
- Can also verify redis ip or 172.17.03 by logging into running dockerapp container & pinging redis
/# ping redis note: 172.17.0.3
→ 64 bytes from 172.17.0.3: icmp_seq=0 ttl=64 time=0.106 ms