Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit 21a2f28

Browse files
author
Renzo Lucioni
committed
Allow access to OS X hosts from inside containers
This adds a script for creating a loopback alias and adding a corresponding entry to /etc/hosts on OS X machines. See the README additions for more details. The script can be run with a Make target, which in turn is run by `devstack.start` to avoid any manual steps. ECOM-6586
1 parent f204fc5 commit 21a2f28

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ devstack.provision: ## Provision all services
1818
devstack.reset: ## Remove all service containers
1919
docker-compose down
2020

21-
devstack.start: clone ## Start all services
21+
devstack.start: clone loopback ## Start all services
2222
docker-compose up
2323

2424
devstack.stop: ## Stop all services
2525
docker-compose stop
2626

27+
loopback: ## Create loopback alias
28+
./loopback.sh
29+
2730
requirements: ## Install requirements
2831
pip install -r requirements.txt
2932

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,28 @@ After the services have started, if you need shell access to one of the services
2727
```
2828
$ make devstack.open.discovery
2929
```
30+
31+
## Loopback Alias
32+
33+
Containers making requests to the LMS and Studio must communicate with ports exposed on the host system by the Vagrant VM.
34+
35+
This is fine on Linux, but doesn't work out of the box on OS X. Attempting to access localhost on a container will result
36+
in talking to the Docker for Mac HyperKit VM, not the host machine.
37+
38+
While it's true that you can get this to work by accessing your Mac's external IP from your containers, this isn't ideal because
39+
it won't work if you have no network access on your host. Your external IP also changes as you switch networks, meaning you'd have
40+
to change the IP accessed by your containers every time you changed networks.
41+
42+
A better solution, borrowed from the [Docker forums](https://forums.docker.com/t/access-host-not-vm-from-inside-container/11747/10),
43+
is to give your host a fixed address by creating a [loopback](http://askubuntu.com/questions/247625/what-is-the-loopback-device-and-how-do-i-use-it)
44+
alias. This is done for you by the `devstack.start` target.
45+
46+
The result is a fixed IP which your containers can use to access ports on your host machine. Note that the underlying script uses `sudo`;
47+
adding IP addresses requires root access. Also note that the alias will not survive a host reboot, which is why the `devstack.start` target
48+
always attempts to set up the loopback for you.
49+
50+
Part of the loopback alias setup includes adding a line to the `/etc/hosts` file on your machine. If you want to stop using devstack, you can clean this up by opening your `/etc/hosts` file and removing this line:
51+
52+
```
53+
10.254.254.254 docker.host
54+
```

docker-compose.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ services:
1717
context: .
1818
dockerfile: ./docker/elasticsearch/Dockerfile
1919
container_name: edx.devstack.elasticsearch
20-
ports:
21-
- "9200:9200"
22-
- "9300:9300"
20+
# TODO: What to do about these forwarded ports? They'll conflict with ports forwarded by the Vagrant VM.
21+
# ports:
22+
# - "9200:9200"
23+
# - "9300:9300"
2324
volumes:
2425
- ./.dev/volumes/elasticsearch/data:/usr/share/elasticsearch/data
2526
- ./.dev/volumes/elasticsearch/logs:/usr/share/elasticsearch/logs
2627

2728
memcached:
2829
container_name: edx.devstack.memcached
2930
image: memcached:1.4.24
30-
ports:
31-
- "11211:11211"
31+
# ports:
32+
# - "11211:11211"
3233

3334
mysql:
3435
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci
@@ -37,8 +38,8 @@ services:
3738
MYSQL_ROOT_PASSWORD: ""
3839
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
3940
image: mysql:5.6
40-
ports:
41-
- "3306:3306"
41+
# ports:
42+
# - "3306:3306"
4243
volumes:
4344
- ./.dev/volumes/mysql:/var/lib/mysql
4445

@@ -68,6 +69,9 @@ services:
6869
environment:
6970
TEST_ELASTICSEARCH_URL: "http://edx.devstack.elasticsearch:9200"
7071
ENABLE_DJANGO_TOOLBAR: 1
72+
extra_hosts:
73+
# For server-to-server calls.
74+
- "docker.host:10.254.254.254"
7175
image: edxops/discovery:devstack
7276
ports:
7377
- "18381:18381"

loopback.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# It's not necessary to do this on Linux, so check that we're on OS X before continuing.
2+
if [ "$(uname)" == "Darwin" ]; then
3+
# This loopback alias allows access to OS X hosts from inside a container. 10.254.254.254
4+
# is a private IP likely to be unused. As is, the alias will not survive a reboot.
5+
# Borrowed from https://forums.docker.com/t/access-host-not-vm-from-inside-container/11747/10.
6+
sudo ifconfig lo0 alias 10.254.254.254
7+
8+
# This will check for a "docker.host" entry in your hosts file. It's created if it doesn't
9+
# exist, so you can use URLs like "http://docker.host:8000" in your service configuration.
10+
if ! grep -q "docker.host" /etc/hosts; then
11+
# Using tee to write to /etc/hosts because using >> to append isn't allowed, even as root.
12+
# See http://stackoverflow.com/a/550808.
13+
echo "10.254.254.254 docker.host" | sudo tee -a /etc/hosts > /dev/null
14+
fi
15+
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
16+
if ! grep -q "docker.host" /etc/hosts; then
17+
echo "127.0.0.1 docker.host" | sudo tee -a /etc/hosts > /dev/null
18+
fi
19+
fi

0 commit comments

Comments
 (0)