Linking Docker containers

Posted: September 23, 2018 in docker

By linking containers we enable communication between docker containers.

Create httpd container:

docker run -d --name reuse_httpd httpd:latest

This container is running web server, it will start as detached (-d) without exposing ports,so it won’t be accessible from host

Now we’ll create another Docker container (i used mysq) and link it to httpd container created in previous step

docker run -it --link "reuse_httpd:web" mysql /bin/bash

we linked these 2 containers with –lin directive (web is alias for httpd container),now from mysql container try accessing httpd container:

apt-get update && apt-get install wget
wget http://web

root@0e9a0af44162:/# wget http://web
--2018-09-23 14:34:35-- http://web/
Resolving web (web)... 172.17.0.2
Connecting to web (web)|172.17.0.2|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 45 [text/html]
Saving to: 'index.html.1'

index.html.1 100%[==================================================================================>] 45 --.-KB/s in 0s

2018-09-23 14:34:35 (10.5 MB/s) - 'index.html.1' saved [45/45]
root@0e9a0af44162:/# cat index.html

It works!

 

root@0e9a0af44162:/#

We can also see from docker logs that we reached web page of httpd docker

C:\Users\ja>docker logs reuse_httpd
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message
[Sun Sep 23 14:27:56.576491 2018] [mpm_event:notice] [pid 1:tid 139809677555584] AH00489: Apache/2.4.34 (Unix) configured — resuming normal operations
[Sun Sep 23 14:27:56.576601 2018] [core:notice] [pid 1:tid 139809677555584] AH00094: Command line: ‘httpd -D FOREGROUND’172.17.0.3 – – [23/Sep/2018:14:34:26 +0000] “GET / HTTP/1.1” 200 45
172.17.0.3 – – [23/Sep/2018:14:34:35 +0000] “GET / HTTP/1.1” 200 45

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s