Files
nexus/raw/Home Office/如何在Ubuntu Server安装 docker & docker compose.md
2026-04-27 16:26:34 +08:00

3.2 KiB

title, source, author, published, created, description, tags
title source author published created description tags
shenwei
docker
ubuntu

#docker #ubuntu

Installing Docker and Docker Compose on Ubuntu involves a few straightforward steps. It's generally best to install from Docker's official repositories to ensure you have the latest version.


🐋 Step 1: Uninstall Old Versions (If Applicable)

First, remove any existing, potentially conflicting Docker packages:

Bash

for pkg in docker.io docker-engine docker-ce docker.io docker-compose docker-compose-v2; do sudo apt-get remove $pkg; done

🛠️ Step 2: Set Up Docker's Repository

You need to set up the repository to allow apt to use a repository over HTTPS:

  1. Update the apt package index:

    Bash

    sudo apt-get update
    
  2. Install necessary packages:

    Bash

    sudo apt-get install ca-certificates curl
    
  3. Add Docker's official GPG key:

    Bash

    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
    
  4. Add the repository to apt sources:

    Bash

    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
    

🚀 Step 3: Install Docker Engine

Now that the repository is set up, you can install the Docker Engine packages:

  1. Update the apt package index again:

    Bash

    sudo apt-get update
    
  2. Install the Docker packages:

    Bash

    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

    (Note: The docker-compose-plugin installs Docker Compose V2, which is used via the command docker compose instead of docker-compose).


Step 4: Verify the Installation

Check that the Docker Engine is running and that the installation was successful by running the test image:

Bash

sudo docker run hello-world

If successful, this command downloads a test image and runs it, printing an informational message before exiting.


By default, running Docker commands requires sudo. To run Docker without sudo, you can add your user to the docker group:

  1. Add your user to the docker group:

    Bash

    sudo usermod -aG docker $USER
    
  2. Log out and log back in (or restart your terminal session, or run newgrp docker) for the changes to take effect.

  3. Verify without sudo:

    Bash

    docker run hello-world
    

You should now have Docker Engine and Docker Compose (V2) installed and ready to use!

Would you like to know some basic Docker commands or learn how to write a simple docker-compose.yml file?