跳转至

Docker Instruction at Ubuntu Installation

Here are the commands that directly address the potential issues you might face when installing Docker on Ubuntu:


1. Update Your System

This ensures you have the latest package lists and installed software, which can prevent dependency conflicts.

sudo apt update && sudo apt upgrade -y

2. Install Prerequisite Packages

These packages are necessary for apt to use repositories over HTTPS and are common dependencies for Docker.

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

3. Add Docker's Official GPG Key and Repository

This step is crucial for trusting the Docker packages you'll be downloading. The commands below will add the key and then configure the repository for your Ubuntu version.

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add the Docker repository to your sources list
# Replace $(lsb_release -cs) with your Ubuntu version's codename if needed (e.g., focal, jammy, noble)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

4. Install Docker Engine

After setting up the repository, you can install Docker.

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

(Note: docker-ce is the Docker Engine, docker-ce-cli is the command-line interface, containerd.io is a container runtime, and docker-compose-plugin is for managing multi-container applications. -y automatically confirms installations.)

5. Verify Docker Installation

This command downloads and runs a test image to confirm Docker is working correctly.

sudo docker run hello-world

6. Manage Docker as a Non-Root User (Avoid sudo for Docker Commands)

This is a critical step for convenience and common practice. It adds your current user to the docker group.

# Add your current user to the 'docker' group
sudo usermod -aG docker $USER

# To apply the group changes, you need to either:
# a) Log out and log back in.
# b) Run the 'newgrp' command:
newgrp docker
  • Explanation:
    • sudo usermod -aG docker $USER: This command modifies your user account ($USER) by appending (-a) it to the docker group (-G docker).
    • newgrp docker: This command is used to change the current user's primary group or to switch to a different group temporarily. Running it with docker makes your shell use the docker group context, activating the permission changes immediately without needing to log out.

7. Check Docker Service Status

If Docker isn't running or you suspect an issue, you can check its service status.

sudo systemctl status docker
  • If it's not active, you can start it with:
    sudo systemctl start docker
    
  • And enable it to start on boot:
    sudo systemctl enable docker
    

8. Check Docker Daemon Logs (for Troubleshooting)

If you encounter installation or runtime errors, the Docker daemon logs can provide crucial details.

sudo journalctl -u docker.service
  • You can follow the logs in real-time by adding -f:
    sudo journalctl -fu docker.service
    

By following these commands, you'll be installing Docker correctly, setting up the necessary permissions, and have the tools to diagnose issues if they arise.