In 2019 I wrote about how I get around static IP restrictions when working in different places.
The world is very different now and whilst I’m not travelling anymore I still use sshuttle regularly to make my traffic look like its coming from a given location.
Recently I started switching some of my projects to run on docker locally (annoyingly this was before Laravel Sail was announced). However one of the challenges with sshuttle + docker in the way I’d originally got things set up is your networking gets messed up and consequently nothing really works for the project - not ideal.
Thats not the end of the story though, there’s an easy fix here - exclude the docker subnets from sshuttle.
First I added all of the services for my project to a shared network within my docker-compose.yml
file:1
2
3
4
5
6services:
app:
image: my_project
container_name: my_project_api
networks:
- laravel
Restart the containers:1
docker-compose up -d
Then list the networks in use:1
docker network ls
1 | NETWORK ID NAME DRIVER SCOPE |
Here we can see the new laravel
network. We need to find the subnet that its running on:
1 | docker network inspect laravel |
1 | [ |
What we’re looking for here i the network subnet (line 14), in this instance “172.19.0.0/16”.
With that we can change our sshuttle command to exclude traffic on that subnet.
1 | sshuttle -l 0.0.0.0 -r {user}@{host}:{port} -x 172.19.0.0/16 0.0.0.0/0 --dns -v |
This should get things up and running again. But we can still do better. Everytime you start your containers up that subnet could change. We should be able to figure it out and script this procces right?
Lets start by installing jq to help us parse docker cli output.
1 | // ubuntu |
Now we can use the cli to determine the network subnet:
1 | $NETWORK = docker network inspect laravel | jq -r '.[0].IPAM.Config[0].Subnet' |
So now lets script the whole thing, create a new file called docker-sshuttle
1 | cd ~/ |
And put the following in:
1 |
|
Now make the script executable:
1 | chmod +x docker-sshuttle |
and finally lets run it:
1 | ./docker-sshuttle laravel user@host |
After asking for your password you should be up and running!
This way we can customise the docker network and target server without having to remember all of the flags for the command.