Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Docker and Docker Compose install script for Ubuntu
#!/bin/sh
set -o errexit
set -o nounset
IFS=$(printf '\n\t')
# Docker
sudo apt remove --yes docker docker-engine docker.io containerd runc || true
sudo apt update
sudo apt --yes --no-install-recommends install apt-transport-https ca-certificates
wget --quiet --output-document=- https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository --yes "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release --codename --short) stable"
sudo apt update
sudo apt --yes --no-install-recommends install docker-ce docker-ce-cli containerd.io
sudo usermod --append --groups docker "$USER"
sudo systemctl enable docker
printf '\nDocker installed successfully\n\n'
printf 'Waiting for Docker to start...\n\n'
sleep 5
# Docker Compose
sudo wget --output-document=/usr/local/bin/docker-compose "https://github.com/docker/compose/releases/download/$(wget --quiet --output-document=- https://api.github.com/repos/docker/compose/releases/latest | grep --perl-regexp --only-matching '"tag_name": "\K.*?(?=")')/run.sh"
sudo chmod +x /usr/local/bin/docker-compose
sudo wget --output-document=/etc/bash_completion.d/docker-compose "https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose"
printf '\nDocker Compose installed successfully\n\n'
@hermannolafs
Copy link

hermannolafs commented Sep 4, 2020

Handy script! To skip the log out/log in you could add newgrp docker after line 16

@EvgenyOrekhov
Copy link
Author

EvgenyOrekhov commented Sep 6, 2020

@hermannolafs You can't use newgrp in a script because it starts a subshell, see https://unix.stackexchange.com/questions/18897/problem-while-running-newgrp-command-in-script.

@hermannolafs
Copy link

hermannolafs commented Sep 6, 2020

Well that's not so great.

@FrangaL
Copy link

FrangaL commented Oct 13, 2020

# Docker Compose
compose_release() {
  curl --silent "https://api.github.com/repos/docker/compose/releases/latest" |
  grep -Po '"tag_name": "\K.*?(?=")'
}

if ! [ -x "$(command -v docker-compose)" ]; then
  curl -L https://github.com/docker/compose/releases/download/$(compose_release)/docker-compose-$(uname -s)-$(uname -m) \
  -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose
fi

@Kevinsiahaan
Copy link

Kevinsiahaan commented Apr 10, 2021

nice bruh

@blackrosezy
Copy link

blackrosezy commented May 27, 2021

For Docker installation I just use this:

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

@orensbruli
Copy link

orensbruli commented Jun 25, 2021

set -o errexit make it fails on a first time installation?

Completion have changed:
https://docs.docker.com/compose/completion/

Thank you for the script!

@EvgenyOrekhov
Copy link
Author

EvgenyOrekhov commented Jun 27, 2021

@orensbruli

set -o errexit make it fails on a first time installation?

set -o errexit makes the script exit when any command fails. Does some specific command fail for you on a first time installation?

Completion have changed:
https://docs.docker.com/compose/completion/

I don't see how it changed, could you be more specific?

@orensbruli
Copy link

orensbruli commented Jun 28, 2021

@orensbruli

set -o errexit make it fails on a first time installation?

set -o errexit makes the script exit when any command fails. Does some specific command fail for you on a first time installation?

If it's a first-time installation I think the sudo apt remove --yes docker docker-engine docker.io containerd runc fails, so no other command is executed. I removed the errexit and it worked.

Completion have changed:
https://docs.docker.com/compose/completion/

I don't see how it changed, could you be more specific?

Yes, sorry, my fault. It's not the completion that is failing, it's the download of the compose itself.
It's because the last release (1.29.2) doesn't have a run.sh in the release:
https://github.com/docker/compose/releases/

But it's probably a problem with the docker-compose repo:
docker/compose#8335
Thank you for sharing!

@EvgenyOrekhov
Copy link
Author

EvgenyOrekhov commented Jun 28, 2021

@orensbruli Hmm, I just tried the sudo apt remove command several times, it always exits with code 0, even if the packages are already removed. Strange.

@tddschn
Copy link

tddschn commented Sep 10, 2021

I can confirm that sudo apt remove --yes docker docker-engine docker.io containerd runc fails on Ubuntu 21.04.

apt version: apt 2.2.3 (amd64)

@EvgenyOrekhov
Copy link
Author

EvgenyOrekhov commented Sep 10, 2021

@tddschn Does it show an error message? Also, could you please run it manually and check the exit code? You can check the exit code like this: echo $?

@tddschn
Copy link

tddschn commented Sep 10, 2021

@EvgenyOrekhov The error code is 100 (tested).
From apt.1: apt returns zero on normal operation, decimal 100 on error.
It seems that apt remove a package that is not installed throws and error on apt version 2.2.3, but there's no error on 2.2.4.

@EvgenyOrekhov
Copy link
Author

EvgenyOrekhov commented Sep 10, 2021

@tddschn Thanks! I added || true to ignore apt remove errors.

@tudoanh
Copy link

tudoanh commented Oct 29, 2021

Not work for Docker Compose v2

@Parm331
Copy link

Parm331 commented Nov 25, 2021

compose_release() {
  curl --silent "https://api.github.com/repos/docker/compose/releases/latest"   #@|
  grep -Po '"tag_name": "\K.*?(?=")'
}

if ! [ -x "$(command -v docker-compose)" ]; then
  curl -L https://github.com/docker/compose/releases/download/$(compose_release)/docker-compose-$(uname -s)-$(uname -m) \
  -o /usr/local/bin/docker-compose && chmo

@codeagencybe
Copy link

codeagencybe commented Dec 2, 2021

Current script fails on docker compose

`Docker installed successfully

Waiting for Docker to start...

--2021-12-02 08:25:56-- https://github.com/docker/compose/releases/download/v2.1.1/run.sh
Resolving github.com (github.com)... 140.82.121.4
Connecting to github.com (github.com)|140.82.121.4|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2021-12-02 08:25:56 ERROR 404: Not Found.`

running compose version also returns permissions denied

docker-compose --version
-bash: /usr/local/bin/docker-compose: Permission denied

@decentraliser
Copy link

decentraliser commented Dec 19, 2021

The link to run.sh seems broken

--2021-12-19 11:45:04--  https://github.com/docker/compose/releases/download/v2.2.2/run.sh
Resolving github.com (github.com)... 140.82.121.3
Connecting to github.com (github.com)|140.82.121.3|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2021-12-19 11:45:04 ERROR 404: Not Found.

@SirMorfield
Copy link

SirMorfield commented May 11, 2022

@decentraliser yes the link is broken, since the 2.x rework in GoLang the auto completion script was removed
I suggest we remove the auto completion install form the script

Reference:
docker/docker.github.io#14692 (comment)
docker/cli#3429 (review)

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