Skip to content

Commit

Permalink
install docker in wsl without docker desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
svg153 committed Feb 25, 2024
1 parent b225d7c commit 8255788
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 20 deletions.
42 changes: 41 additions & 1 deletion .bash_aliases.d/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,47 @@ alias docker_list_images_all='docker images -q | tr "\n" " "' # list all docker
alias docker_rm_images_all='docker rmi -f $(docker_list_images_all)' # remove all docker images
alias docker_list_volumes_all='docker volume ls -q | tr "\n" " "' # list all volumes images
alias docker_rm_volumes_all='docker volumes rm $(docker_list_volumes_all)' # remove all docker volume
alias docker_clean_all="docker_stop_all; docker_rm_cont_all; docker_rm_images_all"
alias docker_clean_all="docker_stop_all; docker_rm_cont_all; docker_rm_images_all; docker_rm_volumes_all" # clean all docker containers, images and volumes
alias docker_remove_everything="docker system prune --all --volume"

# TODO: test this function
docker_rm_images()
{
if [ -n "$1" ]; then
docker rmi -f $(docker images -q --filter "reference=$1")
elif [ -x "$(command -v fzf)" ]; then
images_to_remove=$(docker images -a | fzf --multi --header-lines=1 | awk '{print $3}')
# check if the are any container that depends on the image
if [ -z "$images_to_remove" ]; then
echo "No images selected"
return
fi
for image in $images_to_remove; do
containers=$(docker container ls -a --filter "ancestor=$image" --format "{{.ID}}")
if [ -n "$containers" ]; then
echo "The following containers depend on $image:"
docker container ls -a --filter "ancestor=$image" --format "{{.ID}} {{.Names}}"
echo "Removing the container..."
docker rm -f $containers
fi
done
docker rmi -f $images_to_remove
else
echo "Usage: docker_rm_images <image_name> or install fzf to use interactive mode"
fi
}

# TODO: test this function
docker_rm_cont()
{
if [ -n "$1" ]; then
docker rm -f $(docker container ls -a --filter "name=$1" --format "{{.ID}}")
elif [ -x "$(command -v fzf)" ]; then
docker container ls -a | fzf --multi --header-lines=1 | awk '{print $1}' | xargs --no-run-if-empty docker rm -f
else
echo "Usage: docker_rm_cont <container_name> or install fzf to use interactive mode"
fi
}

docker_enter()
{
Expand Down
13 changes: 13 additions & 0 deletions .rc.d/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
isWSL=$(uname -a | grep WSL | wc -l)

if [ $isWSL -eq 1 ]; then

# if /bin/docker-service
if [ -f $HOME/bin/docker-service.sh ]; then
. $HOME/bin/docker-service.sh "Ubuntu-22.04"
fi

# check if docker is completely installed inside WSL without dockerd desktop
DOCKER_SOCK="/mnt/wsl/shared-docker/docker.sock"
test -S "$DOCKER_SOCK" && export DOCKER_HOST="unix://$DOCKER_SOCK"
fi
23 changes: 23 additions & 0 deletions .rc.d/podman.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# # https://dev.to/bowmanjd/using-podman-on-windows-subsystem-for-linux-wsl-58ji
# set -x
# if [[ -x "$(command -v podman)" ]]; then
# # Define runtime dir for podman socket
# if [[ -z "$XDG_RUNTIME_DIR" ]]; then
# export XDG_RUNTIME_DIR=/run/user/$UID
# if [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
# export XDG_RUNTIME_DIR=/tmp/$USER-runtime
# if [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
# mkdir -m 0700 "$XDG_RUNTIME_DIR"
# fi
# fi
# fi
# # Check if podman service is running (rootless), and start if not.
# if ! pgrep -f -x 'podman system service -t 0' > /dev/null;then
# podman system service -t 0 > /dev/null 2>&1 &
# fi
# # Define DOCKER_HOST to podman socket, so docker-compose can work with it
# # I've installed docker-compose using: pip3 install docker-compose from my user (non-root)
# DOCKER_HOST=`echo "unix://${XDG_RUNTIME_DIR}/podman/podman.sock"`
# export DOCKER_HOST
# fi
# set +x
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
### oh-my-zsh

- <https://github.com/unixorn/awesome-zsh-plugins>

### Docker

- <https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9>
9 changes: 9 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# bin

This folder contains binary files or scripts.

## Files

| File | Description | url |
| ---- | ----------- | --- |
| docker-service.sh | Docker service to start docker when | <https://github.com/bowmanjd/docker-wsl/blob/main/docker-service.sh> |
45 changes: 45 additions & 0 deletions bin/docker-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
# Launcher for dockerd on WSL 2

# Copyright 2021 Jonathan Bowman. All documentation and code contained
# in this file may be freely shared in compliance with the
# Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
# and is provided "AS IS" without warranties or conditions of any kind.
#
# To use this script, first ask yourself if I can be trusted, then read the code
# below and make sure you feel good about it, then consider downloading and
# executing this code that comes with no warranties or claims of suitability.
#
# You might name this script "docker-service" and place it in "$HOME/bin".
#
# This script should be called with a single argument: the name of the WSL
# distribution with dockerd. To find a list of WSL distros, launch Powershell then
#
# wsl -l -q
#
# A usage example:
#
# $HOME/bin/docker-service Ubuntu
#
# If no distribution is specified, the default one will be used.
# This script can also be sourced from your shell initialization script, such
# as .bashrc or .profile, with something like (assuming distro is "Ubuntu":
#
# . $HOME/bin/docker-service Ubuntu
#
# Or called from Windows with
#
# wsl -d Ubuntu ~/bin/docker-service Ubuntu

[ -z "$1" ] && DOCKER_DISTRO="$WSL_DISTRO_NAME" || DOCKER_DISTRO="$1"
# If embedding this in .bashrc, .profile, .zshenv, or the like, remove the above line
# and set $DOCKER_DISTRO to an empty string or something like this:
# DOCKER_DISTRO="Ubuntu"
DOCKER_DIR=/mnt/wsl/shared-docker
DOCKER_SOCK="$DOCKER_DIR/docker.sock"
export DOCKER_HOST="unix://$DOCKER_SOCK"
if [ ! -S "$DOCKER_SOCK" ]; then
mkdir -pm o=,ug=rwx "$DOCKER_DIR"
chgrp docker "$DOCKER_DIR"
/mnt/c/Windows/System32/wsl.exe -d $DOCKER_DISTRO sh -c "nohup sudo -b dockerd < /dev/null > $DOCKER_DIR/dockerd.log 2>&1"
fi
Loading

0 comments on commit 8255788

Please sign in to comment.