Skip to content

Instantly share code, notes, and snippets.

@guizsantos
Last active May 2, 2023 18:26
Show Gist options
  • Select an option

  • Save guizsantos/a50d4edcd4754d91e2399fe536149c9f to your computer and use it in GitHub Desktop.

Select an option

Save guizsantos/a50d4edcd4754d91e2399fe536149c9f to your computer and use it in GitHub Desktop.
Install Docker in Windows 10/11 without Docker Desktop (WSL 2)
# Follow the Docker official instructions to install the engine, CLI and other utilities
# https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
# PS: the `docker run hello-world` will not work, follow the next steps to get it working
# In you startup script (bashrc, zshrc, or similar) add the following lines
export DOCKER_HOST=$(ifconfig eth0 | grep -Po "(?<=inet )\d+\.\d+\.\d+\.\d+")
if [ -z "$(ps aux | grep dockerd | grep -v grep)" ]; then
echo "Starting docker daemon on host $DOCKER_HOST"
sudo dockerd -H $DOCKER_HOST >>~/.logs/dockerd/out 2>>~/.logs/dockerd/err &
disown
fi
# Create the logs files to monitor the engine process
mkdir -p ~/.logs/dockerd/out ~/.logs/dockerd/err
# Make you user a sudoer for dockerd by default
# Run `sudo visudo`, add this to the end of the file and save (replace {myuser} with your username)
{myuser} ALL=(ALL) NOPASSWD: /usr/bin/dockerd
# Restart your session and test the commands
# (if the system is just starting up you may need to wait a minute before the daemon is fully started)
docker --version
docker ps
# Check the process logs
cat ~/.logs/dockerd/(out|err)
# You can access the daemon API at $DOCKER_HOST:2375 from the Windows host.
# Explanation:
# The `docker` command is just a CLI which translates shell to GET commands, which use the API exposed at some URL (in our case $DOCKER_HOST)
# This so-called API is an entrypoint to the Docker daemon (the process running on our "server" which hosts the containers)
# For WSL systems, the Docker daemon is usually setup at the host level (Windows machine) and the virtualized Linux instances call the API from the docker sock (unix://var/run/docker.sock)
# Since we want to run the daemon in the virtualized Linux environment, the whole process in this gist is to bootstrap the Docker daemon to our startup script, which initiates a daemon and set the host at the ethernet device allocated to the virtualized Linux instance.
# Furthermore, the `docker` command will use the host set by the env var DOCKER_HOST. If this var is unset you migth get "is the daemon running?" error.
# TODO:
# Test using the /etc/docker/daemon.json configuration instead of $DOCKER_HOST
# {
# "hosts": ["unix:///var/run/docker.sock", "tcp://127.0.0.1:2375"]
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment