Skip to content

Commit 74f8d26

Browse files
committed
Detect if Takeout is running in a container and use host.docker.localhost instead of localhost
This works on Windows and Mac, but will require Linux users to add `--add-host=docker.internal.host:host-gateway` to their aliases. The docs was updated.
1 parent 1328dc3 commit 74f8d26

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ The recommended way to install Takeout is the dockerized version via an alias (a
3232
On Linux or macOS, use:
3333

3434
```bash
35-
alias takeout="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -it tighten/takeout:latest"
35+
alias takeout="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --add-host=host.docker.internal:host-gateway -it tighten/takeout:latest"
3636
```
3737

38-
On Windows 10, if you're using Bash, use:
38+
On Windows 10|11, if you're using Bash, use:
3939

4040
```bash
41-
alias takeout="docker run --rm -v //var/run/docker.sock:/var/run/docker.sock -it tighten/takeout:latest"
41+
alias takeout="docker run --rm -v //var/run/docker.sock:/var/run/docker.sock --add-host=host.docker.internal:host-gateway -it tighten/takeout:latest"
4242
```
4343

44-
On Windows 10, if you're using PowerShell, use:
44+
On Windows 10|11, if you're using PowerShell, use:
4545

4646
```bash
47-
function takeout { docker run --rm -v //var/run/docker.sock:/var/run/docker.sock -it tighten/takeout:latest $args }
47+
function takeout { docker run --rm -v //var/run/docker.sock:/var/run/docker.sock --add-host=host.docker.internal:host-gateway -it tighten/takeout:latest $args }
4848
```
4949
5050
That's it. You may now use Takeout on your terminal. The first time you use this alias, it will pull the Takeout image from Docker Hub.

app/Shell/Environment.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function portIsAvailable($port): bool
3232
// If we cannot open the socket, it means there's nothing running on it, so the
3333
// port is available. If we are successful, that means it is already in use.
3434

35-
$socket = @fsockopen('localhost', $port, $errorCode, $errorMessage, timeout: 5);
35+
$socket = @fsockopen($this->localhost(), $port, $errorCode, $errorMessage, timeout: 5);
3636

3737
if (! $socket) {
3838
return true;
@@ -43,6 +43,11 @@ public function portIsAvailable($port): bool
4343
return false;
4444
}
4545

46+
public function isTakeoutRunningOnDocker(): bool
47+
{
48+
return boolval($_SERVER['TAKEOUT_CONTAINER'] ?? false);
49+
}
50+
4651
public function userIsInDockerGroup(): bool
4752
{
4853
return $this->shell->execQuietly('groups | grep docker')->isSuccessful();
@@ -63,4 +68,9 @@ public function homeDirectory(): string
6368

6469
return '~';
6570
}
71+
72+
private function localhost(): string
73+
{
74+
return $this->isTakeoutRunningOnDocker() ? 'host.docker.internal' : 'localhost';
75+
}
6676
}

0 commit comments

Comments
 (0)