This post is to help in installing docker engine and docker-compose on almost any linux distro (with apt of course). So, without further ado, lets get cracking!

Deleting any existing installs

Run the command below:

1
    sudo apt-get remove docker docker.io containerd runc

Output when nothing is installed

Setting up your OS

  1. Set up the repository

    Run the commands to install prerequisites.

    1
    2
    3
    4
    5
    6
    7
    8
    
    sudo apt-get update
    
    sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
    

    Output of installing prerequisites

  2. Add docker’s official GPG key

    1
    
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    

    Adding docker’s GPG

  3. Setup the stable docker repository

    Note: amd64 can be substituted for armhf or arm64 based on your system architecture. See uname -a and uname -mfor more details.

    1
    2
    3
    4
    
        sudo add-apt-repository \
            "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
            $(lsb_release -cs) \
            stable"
    

    Adding docker repo

Installing docker

Now, we can proceed to install docker by:

1
2
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

Output of installing docker

Optional: Add your user to the docker group so that you can use docker without su

1
    sudo usermod -aG docker `whoami`

Install docker-compose

Docker compose releases are provided in their github repo . You can install the 1.26.2 (latest as of writing this) as:

1
2
3
4
5
    sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose

    # If /usr/local/bin is not in path, symlink to /usr/bin
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Output of docker-compose download

Verify the installation

Check the versions of docker-engine, docker-cli and docker-compose to see they have been installed successfully. See the docker website here and docker-compose here for more info.

1
2
docker version
docker-compose version

docker version outputs

Done, docker is ready

Now that you have docker and docker-compose installed, see a few samples below on how to use them:

1
2
3
$ docker run hello-world
$ docker run --interactive --tty ubuntu
# Can be run shorthand as: docker run -it ubuntu

Sample docker runs

Sample docker-compose to get you started :)

Try this docker-compose by saving the file below as docker-compose.yml in an empty directory and run the commands. Substitute nano for your favorite editor.

1
2
3
4
mkdir test-compose && cd test-compose
nano docker-compose.yml
# Paste the contents below into the file or download it from the below link
# Ctrl+X, Y, Enter to exit
1
docker-compose up --detach

Output of running docker-compose File docker-compose.yml :

To see the deployed UI, hit the http://localhost:8081/ and see the Mongo Express UI connected to the Mongo DB container. Mongo Express UI Running in docker

You can have a look at the full docker-compose syntax here .