Skip to content

Instantly share code, notes, and snippets.

@mwufi
Last active November 1, 2024 01:05
Show Gist options
  • Save mwufi/6718b30761cd109f9aff04c5144eb885 to your computer and use it in GitHub Desktop.
Save mwufi/6718b30761cd109f9aff04c5144eb885 to your computer and use it in GitHub Desktop.
Install Docker in Google Colab!
# First let's update all the packages to the latest ones with the following command
sudo apt update -qq
# Now we want to install some prerequisite packages which will let us use HTTPS over apt
sudo apt install apt-transport-https ca-certificates curl software-properties-common -qq
# After that we will add the GPG key for the official Docker repository to the system
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# We will add the Docker repository to our APT sources
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
# Next let's update the package database with our newly added Docker package repo
sudo apt update -qq
# Finally lets install docker with the below command
sudo apt install docker-ce
# Lets check that docker is running
docker
# Originally, we did the following: (but doesn't work in Colab...)
# sudo systemctl status docker
# The output should be similar to this snippet below
# ● docker.service - Docker Application Container Engine
# Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
# Active: active (running) since Tue 2019-01-01 19:22:114 UTC; 1min 25s ago
# Docs: https://docs.docker.com
# Main PID: 10096 (dockerd)
# Tasks: 16
# CGroup: /system.slice/docker.service
# ├─10096 /usr/bin/dockerd -H fd://
# └─10113 docker-containerd --config /var/run/docker/containerd/containerd.toml
# And now that everything is good, you should be able to do:
# docker run -it -p 8888:8888 gcr.io/tensorflow/tensorflow
@miqrom29
Copy link

It works for me with Google Colab Pro and GPU Runtime

@colab635
Copy link

sudo dockerd

failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.6.1: can't initialize iptables table `nat': Permission denied (you must be root) Perhaps iptables or your kernel needs to be upgraded

same here

@biemster
Copy link

Apparently (at least the free) Colab runs in docker already:
image

Docker in docker requires some host container configurations to allow this.

@kaili-yang
Copy link

sudo dockerd

failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.6.1: can't initialize iptables table `nat': Permission denied (you must be root) Perhaps iptables or your kernel needs to be upgraded

Same here

@poojatambe
Copy link

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

@guccang
Copy link

guccang commented Apr 19, 2023

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

same here

@cerlymarco
Copy link

This works for me...

installing docker:
!apt-get -qq install docker.io

running desired docker command (docker --version):

%%shell
set -x
dockerd -b none --iptables=0 -l warn &
for i in $(seq 5); do [ ! -S "/var/run/docker.sock" ] && sleep 2 || break; done
docker --version
kill $(jobs -p)

@davies-w
Copy link

Are you on Google Colab, or running colab locally?

@kaili-yang
Copy link

Google Colab

@ledinhduy281
Copy link

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

yeah, me too. Can anyone who can run successfully show me their script?

@dhananjaymudgule11
Copy link

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

@littlewhitesea
Copy link

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I also met with the same problem.

@nibaijing
Copy link

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
How to do?

@MohannadEhabBarakat
Copy link

Thanks a lot

@musahi0128
Copy link

musahi0128 commented Aug 27, 2023

Hi, I found a solution for this. Check out https://github.com/indigo-dc/udocker. It's working perfectly in the free version of Google Colab.

  • Installation:
%%shell
pip install udocker
udocker --allow-root install
  • Sample usage:
udocker --allow-root run -p 127.0.0.1:8081:8081 -v -e TELEGRAM_API_ID=#### -e TELEGRAM_API_HASH=#### -e TELEGRAM_LOCAL=1 aiogram/telegram-bot-api:latest

Cheers!

@leschultz
Copy link

Thank you to @musahi0128, your solution worked for me.

@Raidus
Copy link

Raidus commented Sep 25, 2023

@musahi0128 works perfectly!

Tested it with
!udocker --allow-root run hello-world

and checking for the running container with
!udocker --allow-root ps

@buttercutter
Copy link

!pip install udocker
!udocker --allow-root install
!udocker --allow-root run scrapinghub/splash

Requirement already satisfied: udocker in /usr/local/lib/python3.10/dist-packages (1.3.10)
Warning: this container exposes TCP/IP ports
Error: invalid syntax for user splash:splash

@musahi0128
Copy link

!pip install udocker
!udocker --allow-root install
!udocker --allow-root run scrapinghub/splash

Requirement already satisfied: udocker in /usr/local/lib/python3.10/dist-packages (1.3.10) Warning: this container exposes TCP/IP ports Error: invalid syntax for user splash:splash

Do you have the Dockerfile for that? It looks to me the error comes from within the container

@buttercutter
Copy link

@jonathanagustin
Copy link

jonathanagustin commented Jan 7, 2024

@chinmoybasak
Copy link

docker-in-colab docker-in-colab

https://github.com/drengskapur/docker-in-colab

Facing the following error

With udocker command
Error: do not run as root !

With docker command

ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

With udocker command

@jonathanagustin
Copy link

jonathanagustin commented May 23, 2024

@chinmoybasak It works for me - how are you running it?

@sfboss
Copy link

sfboss commented Jun 27, 2024

@chinmoybasak prob need --allow-root

@hacken4221
Copy link

hacken4221 commented Jul 19, 2024

how to run it as a daemon (no --daemon option)?

solution: !nohup udocker &

@vishakha-gautam11041997
Copy link

vishakha-gautam11041997 commented Aug 25, 2024

Hi, I found a solution for this. Check out https://github.com/indigo-dc/udocker. It's working perfectly in the free version of Google Colab.

  • Installation:
%%shell
pip install udocker
udocker --allow-root install
  • Sample usage:
udocker --allow-root run -p 127.0.0.1:8081:8081 -v -e TELEGRAM_API_ID=#### -e TELEGRAM_API_HASH=#### -e TELEGRAM_LOCAL=1 aiogram/telegram-bot-api:latest

Cheers!

Hi, Thanks for the help.
It works also. I was wondering how you can copy the folder from your server [xxx.xxx.xx.xx] to this container?

@RCgit123
Copy link

Thank you it 100% works 😊

@HomeDev68
Copy link

HomeDev68 commented Oct 21, 2024

how to run it as a daemon (no --daemon option)?

solution: !nohup udocker &

use this to detach the process

!(nohup udocker &)

@HomeDev68
Copy link

For Colab users, use this for installation:

#@title Docker for Colab using udocker 
%%shell
pip install udocker
udocker --allow-root install
(nohup udocker &) #@markdown RUN IN BACKGROUND AS A DETACHED PROCESS

and this for easy usage:

#@title Easy Command Usage
args = "" # @param {"type":"string","placeholder":"commands"}
args = args.strip(" ")
if args:
  !udocker --allow-root {args}
else:
  !udocker --allow-root --help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment