Reputation: 1549
I have been playing about with docker and apache recently neither of which I understand massively well.
I have a problem concerning communication between 2 docker containers on the same host.
One container is running apache with options -p 80:80. Going to localhost:80 shows the default apache page
I have a second container running the rocker/rstudio image with option -p 8787:8787. Going to localhost:8787 shows the rstudio log in page as expected.
I want to inside my apache container make it such that localhost/rstudio takes me to the login page for rstudio that is running in the rocker container.
As far as i understood the apache container should be able to see localhost:8787, under sites-available i have the following rstudio.conf file
<VirtualHost *:80>
<Proxy *>
Allow from localhost
</Proxy>
# Specify path for Logs
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Following lines should open rstudio directly from the url
# Map rstudio to rstudio/
RedirectMatch ^/rstudio$ /rstudio/
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /rstudio/(.*) ws://localhost:8787/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /rstudio/(.*) http://localhost:8787/$1 [P,L]
ProxyPass /rstudio/ http://localhost:8787/
ProxyPassReverse /rstudio/ http://localhost:8787/
ProxyRequests off
</VirtualHost>
as suggested by the rstudio server configuration docs. However localhost:80/rstudio returns a 404 and i don't understand why. Does anyone have any suggestions as to how to fix this?
The main reason I want to do this from inside the apache container rather than just install apache in the rocker container is such that the apache container can manage other connected containers too.
Upvotes: 0
Views: 191
Reputation: 4677
As far as i understood the apache container should be able to see localhost:8787, under sites-available i have the following rstudio.conf file
Almost. From inside the apache docker container, localhost is that container, not the host.
If you want to see what I'm talking about, go into your running apache container and curl localhost:8787
. You will get a 404. Now add another vhost in the apache container for 8787 and enable it, then from inside the container curl localhost:8787
again, you'll get the new vhost's content.
The two most straightforward options to do what you're asking would be either a custom network or using docker-compose.
custom network
docker network create jamie-rowan-network
docker run -itd -p 80:80 --network jamie-rowan-network --name apache <image>
docker run -itd -p 8787:8787 --network jamie-rowan-network --name rstudio <image>
This creates a bridge
network named jamie-rowan-network. When you run your containers, add them to this network. The embedded network DNS also has service discovery, so your containers will be able to resolve each other by the --name
given in the run. (Suggested reading about that here.
Now you should be able to resolve your rstudio container from your apache container with curl rstudio:8787
.
Important note: this behavior is a little bit different before and after Docker 1.10, definitely check the docs I linked above. I'm assuming you're on > 1.10.
docker-compose
docker-compose is a tool designed to make a container orchestration much simpler. In this case, it pretty much does all the lifting required for the custom network on it's own, with no work required on your part. I won't go in to how to write a docker-compose.yml, but any service listed in the docker-compose.yml is reachable by the other services by name.
Example:
version: '3'
services:
apache:
image: <image>
ports:
- 80:80
rstudio:
image: <image>
ports:
- 8787:8787
This would accomplish the same as the custom network; rstudio would be reachable from the apache container with curl rstudio:8787
and going the other way, apache would be reachable from rstudio with curl apache:80
Upvotes: 1