Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EvgenyOrekhov/1ed8a4466efd0a59d73a11d753c0167b to your computer and use it in GitHub Desktop.
Save EvgenyOrekhov/1ed8a4466efd0a59d73a11d753c0167b to your computer and use it in GitHub Desktop.
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'
@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

nice bruh

@blackrosezy
Copy link

For Docker installation I just use this:

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

@orensbruli
Copy link

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

@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

@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

@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

@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

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

@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/docs#14692 (comment)
docker/cli#3429 (review)

@mricim
Copy link

mricim commented May 9, 2023

#!/bin/sh

set -o errexit
set -o nounset
IFS=$(printf '\n\t')

# Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
printf '\nDocker installed successfully\n\n'

printf 'Waiting for Docker to start...\n\n'
sleep 5

# Docker Compose
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
sudo curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
printf '\nDocker Compose installed successfully\n\n'
sudo docker-compose -v

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