Install Docker on Ubuntu

Install Docker on Ubuntu

  • Enviroment: Ubuntu=24.4

Firewall limitations

  1. If using ufw or firewalld to manage firewall settings,

    When you expose container ports using Docker, these ports bypass your firewall rules.

  2. Docker is only compatible with iptables-nft and iptables-legacy.

    Firewall rules created with nft are not supported on a system with Docker installed.
    Make sure that any firewall rulesets you use are created with iptables or ip6tables, and that you add them to the DOCKER-USER chain.

Uninstall Docker(old versions)

  1. Uninstall the Docker Engine, CLI, containerd, and Docker Compose packages:

    1
    sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
  2. Remove all images, containers, and volumes:

    1
    2
    sudo rm -rf /var/lib/docker
    sudo rm -rf /var/lib/containerd
  3. Remove source list and keyrings

    1
    2
    sudo rm /etc/apt/sources.list.d/docker.list
    sudo rm /etc/apt/keyrings/docker.asc

Installation

Switch apt sources

  1. Backup source list

    1
    sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
  2. Find out sources
    Here I use Tsinghua mirror

  3. Modify the source

    1
    sudo vi /etc/apt/sources.list.d/ubuntu.sources
  4. Update

    1
    2
    sudo apt-get update
    sudo apt-get upgrade

Install using the apt repository

  1. Set up the Docker apt repository

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # Add Docker's official GPG key:
    sudo apt-get install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc

    # Add the repository to Apt sources:
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
  2. Install the Docker packages

    1
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  3. Verify that the installation is successful

    Check docker command:

    1
    2
    sudo docker image ls
    # REPOSITORY TAG IMAGE ID CREATED SIZE

    OR running the hello-world image:

    1
    sudo docker run hello-world

    The later one may issue error for two reason:

    1. Run without sudo: The docker user group exists but contains no users, which is why you’re required to use sudo to run Docker commands.

    2. Can not access to Docker Hub: The image ‘hello-world’ is not exist locally, it requires to get access to Docker Hub to download the image.

Reference

https://docs.docker.com/engine/install/ubuntu/
https://www.cnblogs.com/lcxhk/p/14951334.html


Install Docker on Ubuntu
https://dyliu0312.github.io/blog.github.io/2024/12/01/Install-Docker-on-Ubuntu/
Author
dyliu
Posted on
December 1, 2024
Licensed under