【Linux】Ubuntu 安装 Docker

Posted by 西维蜀黍 on 2020-05-01, Last Modified on 2024-04-29

Installation

Method 1 - Convenience Script

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
<...>

If you would like to use Docker as a non-root user,

Approach 1

$ sudo sh -eux <<EOF
# Install newuidmap & newgidmap binaries
apt-get install -y uidmap
EOF

$  dockerd-rootless-setuptool.sh install

# Verify that you can run docker commands without sudo.
$ docker run hello-world

Approach 2

you should now consider adding your user to the “docker” group with something like:

$ sudo usermod -aG docker ${USER}

# 将当前用户切换到docker组中,且立即生效
$ newgrp docker

# Verify that you can run docker commands without sudo.
$ docker run hello-world

(depreciated) Method 2 - Using Repository

Installing Docker on Ubuntu involves several steps. The following instructions are generally applicable for most Ubuntu versions. Before you start, make sure your Ubuntu system is up-to-date.

  1. Update your system: First, update your existing list of packages:

    sudo apt update
    
  2. Install prerequisite packages: Install packages to allow apt to use a repository over HTTPS:

    bashCopy code
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
    
  3. Add Docker’s official GPG key: This ensures the software you’re installing is authentic.

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  4. Set up the stable repository: Add the Docker repository to APT sources:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  5. Install Docker Engine: Update the apt package index, and install the latest version of Docker Engine and containerd:

    sudo apt update
    sudo apt install docker-ce docker-ce-cli containerd.io
    
  6. Verify Docker Engine is installed correctly: Run the hello-world image to ensure Docker was correctly installed and is running:

    sudo docker run hello-world
    

This command downloads a test image and runs it in a container. If the container runs successfully, it indicates that Docker is installed and working on your system.

  1. (Optional) Run Docker as a non-root user: If you want to run Docker commands without sudo, you’ll need to add your user to the docker group:

    sudo usermod -aG docker ${USER}
    

    You will need to log out and back in for this to take effect.

  2. (Optional) Configure Docker to start on boot: Most current distributions use systemd to manage which services start when the system boots. To enable Docker to start on boot, use the following command:

    sudo systemctl enable docker
    

Uninstall Docker Engine

  1. Uninstall the Docker Engine, CLI, and Containerd packages:

    $ sudo apt-get purge docker-ce docker-ce-cli containerd.io
    
  2. Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:

    $ sudo rm -rf /var/lib/docker
    $ sudo rm -rf /var/lib/containerd
    

You must delete any edited configuration files manually.

管理

Start the Docker daemon

Start manually

Once Docker is installed, you need to start the Docker daemon. Most Linux distributions use systemctl to start services.

$ sudo systemctl start docker

Reference