Install Docker & Docker Compose on Ubuntu 24.04

{"time":1776378046149,"version":"2.28.2","blocks":[{"type":"paragraph","data":{"text":"Platform: Ubuntu Server 24.04.4 LTS — Intel / AMD (x86_64)"}},{"type":"paragraph","data":{"text":"This guide walks you through installing Docker Engine, Docker Compose v2, and Docker Buildx on Ubuntu Server 24.04.4 LTS (Intel / AMD). It also configures Docker to start automatically at boot, so your containers are always ready after a reboot."}},{"type":"header","data":{"text":"Overview","level":2}},{"type":"paragraph","data":{"text":"By the end of this guide, you will have the following installed and running:"}},{"type":"list","data":{"style":"unordered","items":["Docker Engine","Docker Compose v2","Docker Buildx","Automatic Docker startup on boot"]}},{"type":"header","data":{"text":"Step 1 — Update Ubuntu","level":2}},{"type":"paragraph","data":{"text":"Start by updating the package list and upgrading any outdated packages on your system."}},{"type":"code","data":{"code":"sudo apt update\nsudo apt upgrade -y"}},{"type":"header","data":{"text":"Step 2 — Install Required Dependencies","level":2}},{"type":"paragraph","data":{"text":"Install the packages needed to securely download and add Docker’s official repository."}},{"type":"code","data":{"code":"sudo apt install -y ca-certificates curl gnupg"}},{"type":"header","data":{"text":"Step 3 — Create the Docker Keyring Directory","level":2}},{"type":"paragraph","data":{"text":"Create the directory where Docker’s GPG key will be stored. This directory is used to verify the authenticity of packages downloaded from Docker’s repository."}},{"type":"code","data":{"code":"sudo install -m 0755 -d /etc/apt/keyrings"}},{"type":"header","data":{"text":"Step 4 — Add Docker’s Official GPG Key","level":2}},{"type":"paragraph","data":{"text":"Download Docker’s official GPG key and save it to the keyring directory you just created."}},{"type":"code","data":{"code":"curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \\\nsudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg"}},{"type":"paragraph","data":{"text":"Then set the correct file permissions so the key is readable by the system:"}},{"type":"code","data":{"code":"sudo chmod a+r /etc/apt/keyrings/docker.gpg"}},{"type":"header","data":{"text":"Step 5 — Add the Docker Repository","level":2}},{"type":"paragraph","data":{"text":"Add Docker’s official APT repository to your system’s package sources. This tells apt where to find Docker packages."}},{"type":"code","data":{"code":"echo \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \\\nhttps://download.docker.com/linux/ubuntu \\\n$(. /etc/os-release && echo $VERSION_CODENAME) stable\" | \\\nsudo tee /etc/apt/sources.list.d/docker.list > /dev/null"}},{"type":"header","data":{"text":"Step 6 — Update the Package Index","level":2}},{"type":"paragraph","data":{"text":"Refresh the package list so your system recognizes the newly added Docker repository."}},{"type":"code","data":{"code":"sudo apt update"}},{"type":"header","data":{"text":"Step 7 — Install Docker Engine and Components","level":2}},{"type":"paragraph","data":{"text":"Install Docker Engine along with all required plugins and tools."}},{"type":"code","data":{"code":"sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"}},{"type":"paragraph","data":{"text":"Here is what each component does:"}},{"type":"table","data":{"withHeadings":true,"content":[["Package","Description"],["docker-ce","Docker Engine — the core service that runs containers"],["docker-ce-cli","Docker command-line interface for running Docker commands"],["containerd.io","Low-level container runtime that Docker depends on"],["docker-buildx-plugin","Advanced image build tool with multi-platform support"],["docker-compose-plugin","Docker Compose v2 — runs multi-container applications"]]}},{"type":"header","data":{"text":"Step 8 — Enable Docker to Start Automatically","level":2}},{"type":"paragraph","data":{"text":"Enable the Docker service so it starts automatically every time the server boots, then start it now."}},{"type":"code","data":{"code":"sudo systemctl enable docker\nsudo systemctl start docker"}},{"type":"paragraph","data":{"text":"Verify that Docker is running:"}},{"type":"code","data":{"code":"systemctl status docker"}},{"type":"paragraph","data":{"text":"You should see active (running) in the output. Press Q to exit the status view."}},{"type":"header","data":{"text":"Step 9 — Verify the Installation","level":2}},{"type":"paragraph","data":{"text":"Confirm that Docker Engine and Docker Compose were installed correctly by checking their versions."}},{"type":"paragraph","data":{"text":"Check the Docker version:"}},{"type":"code","data":{"code":"docker --version"}},{"type":"paragraph","data":{"text":"Expected output:"}},{"type":"code","data":{"code":"Docker version 26.x.x, build xxxxxxx"}},{"type":"paragraph","data":{"text":"Check the Docker Compose version:"}},{"type":"code","data":{"code":"docker compose version"}},{"type":"paragraph","data":{"text":"Expected output:"}},{"type":"code","data":{"code":"Docker Compose version v2.x.x"}},{"type":"header","data":{"text":"Step 10 — Test the Docker Installation","level":2}},{"type":"paragraph","data":{"text":"Run the official Docker test container to confirm everything is working end-to-end."}},{"type":"code","data":{"code":"sudo docker run hello-world"}},{"type":"paragraph","data":{"text":"If Docker is installed correctly, you will see a message that starts with:"}},{"type":"code","data":{"code":"Hello from Docker!"}},{"type":"header","data":{"text":"Step 11 — (Optional) Run Docker Without sudo","level":2}},{"type":"paragraph","data":{"text":"By default, Docker requires sudo to run commands. You can add your user to the docker group to avoid typing sudo every time."}},{"type":"code","data":{"code":"sudo usermod -aG docker $USER"}},{"type":"paragraph","data":{"text":"Log out and log back in (or reboot the server) for the group change to take effect. Then test it:"}},{"type":"code","data":{"code":"docker ps"}},{"type":"paragraph","data":{"text":"If no error appears, you can now run Docker commands without sudo."}},{"type":"header","data":{"text":"Example — Test Docker Compose","level":2}},{"type":"paragraph","data":{"text":"This is a quick end-to-end test to confirm Docker Compose is working correctly. It spins up an Nginx web server using a simple Compose file."}},{"type":"header","data":{"text":"1. Create a Test Directory","level":3}},{"type":"code","data":{"code":"mkdir ~/docker-test\ncd ~/docker-test"}},{"type":"header","data":{"text":"2. Create the Compose File","level":3}},{"type":"code","data":{"code":"nano docker-compose.yml"}},{"type":"paragraph","data":{"text":"Paste the following content into the file:"}},{"type":"code","data":{"code":"services:\n nginx:\n image: nginx:latest\n ports:\n - \"8080:80\""}},{"type":"paragraph","data":{"text":"Save and exit: press Ctrl+X, then Y, then Enter."}},{"type":"header","data":{"text":"3. Start the Container","level":3}},{"type":"code","data":{"code":"docker compose up -d"}},{"type":"header","data":{"text":"4. Verify the Container Is Running","level":3}},{"type":"code","data":{"code":"docker ps"}},{"type":"paragraph","data":{"text":"You should see the nginx container listed with a status of Up."}},{"type":"header","data":{"text":"5. Open in Your Browser","level":3}},{"type":"paragraph","data":{"text":"Navigate to the following URL, replacing SERVER-IP with your server’s actual IP address:"}},{"type":"code","data":{"code":"http://SERVER-IP:8080"}},{"type":"paragraph","data":{"text":"You should see the default Nginx welcome page. This confirms Docker Compose is running correctly."}},{"type":"header","data":{"text":"6. Stop the Container","level":3}},{"type":"code","data":{"code":"docker compose down"}},{"type":"header","data":{"text":"Useful Docker Commands","level":2}},{"type":"paragraph","data":{"text":"Here are some everyday Docker commands you will use often:"}},{"type":"table","data":{"withHeadings":true,"content":[["Command","Description"],["docker ps","List all currently running containers"],["docker ps -a","List all containers, including stopped ones"],["docker logs CONTAINER_NAME","View the logs for a specific container"],["docker stop CONTAINER_NAME","Stop a running container"],["docker rm CONTAINER_NAME","Remove a stopped container"],["docker images","List all locally downloaded Docker images"],["docker pull IMAGE_NAME","Download a Docker image from Docker Hub"]]}},{"type":"header","data":{"text":"Recommended Tools for Docker Servers","level":2}},{"type":"paragraph","data":{"text":"Once Docker is up and running, these tools are highly recommended for managing your server environment:"}},{"type":"table","data":{"withHeadings":true,"content":[["Tool","Purpose"],["Portainer","Web-based UI for managing Docker containers, images, and volumes"],["Nginx Proxy Manager","Easy reverse proxy management with built-in SSL support"],["Watchtower","Automatically keeps your running containers updated"]]}}]}