Sometimes Docker on your laptop sucks. It’s inconvenient to install on your new Apple Silicon and casually eats 11.23 GB of RAM. Docker Mac incurs significant performance penalties since it depends on VMs. Docker Linux, however, does not suffer from this constraint. Here’s one weird trick to improve Mac performance that docker doesn’t want you to know about.
Run docker remotely in 2 easy steps:
For the tutorial’s sake, I will use a Brev environment named “brev-environment” as my remote computer.
Docker can connect to the remote Docker computer by using the environment variable DOCKER_HOST. Out of the box, docker supports SSH as a connection protocol.
You can also pass the Docker host in as a CLI argument
The Docker ssh protocol creates a new connection for each command. This operation can sometimes be slow. You can re-use an ssh connection by forwarding the docker endpoint to your local computer. The docker API, by default, is exposed as a UNIX socket.
SSH supports forwarding unix socket files
Change the Docker host to point to the locally forwarded unix file.
To permanently use this configuration, you can export the env variable DOCKER_HOST in your shell config(.zshrc, .bashrc), or you could use Docker contexts which allow you to switch between different Docker hosts easier.
You can now use Docker as if it’s running on your local computer!
Set your Docker context.
Pushing, pulling, and running large Docker images is vastly improved since you can take advantage of data center network speed!
Docker containers can be built faster on remote computers since you can provision powerful machines in the cloud. You can also trust that your lengthy builds won’t be interrupted by a dead battery or wifi outage!
Run your dev helper services.
Running remote Docker isn’t entirely a free lunch.
One of the first things you may want to do is connect to one of your Docker containers over the network. Since it runs on a remote computer, you can not simply access it at localhost:X000. You must forward the desired ports from your local computer to your remote computer to access a port.
Port forwarding can be achieved with SSH.
Another issue you may encounter is building Docker images with large local contexts. Moving large files over the network can result in poor performance. Moving more of your development assets onto the remote computer fixes this problem and is the standard approach at Brev.
Since Docker is running on a remote computer, the Docker daemon will make file volumes and mounts on the remote computer, not the local computer. You can use the following tools if you intend to sync files between your local computer and the remote one.
Besides using these tools, moving more of your development assets into the remote computer removes many file sync issues.
Working across multiple computers (local and remote) increases cognitive load. You need to know, in all contexts, what filesystem and network you’re currently working on and how to interface between them. Managing connections and keys yourself can quickly become tedious. Finally, I want to mention that running cloud computers can become quite costly if you are not actively managing and tracking computer type and usage.
If these problems interest you check out Brev; it’s what we work on full-time!