Installing Docker and Docker Compose on an Ubuntu server:
Installing Docker and Docker Compose on an Ubuntu server:
Installing Docker on Ubuntu:
Update Package Index:
sudo apt updateInstall Dependencies: Install the packages necessary to allow apt to use a repository over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-commonAdd Docker's Official GPG Key: Add Docker’s official GPG key to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -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"Update the Package Database: Update the package database with Docker packages from the newly added repo:
sudo apt updateInstall Docker CE: Install Docker Community Edition (CE):
sudo apt install docker-ceVerify Docker Installation: Check that Docker is installed correctly by running the
hello-worldimage:sudo docker run hello-world
Installing Docker Compose:
Download Docker Compose Binary:
Check the latest release of Docker Compose from the official GitHub repository.
Replace
VERSIONwith the latest release version:
sudo curl -L "https://github.com/docker/compose/releases/download/VERSION/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composeApply Executable Permissions:
sudo chmod +x /usr/local/bin/docker-composeCreate Symbolic Link:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-composeVerify Docker Compose Installation:
docker-compose --version
Post-Installation Steps:
Manage Docker as a Non-root User (Optional):
If you want to run Docker commands without
sudo, add your user to thedockergroup:
sudo usermod -aG docker $USERLog out and log back in to apply the group membership changes.
Restart Docker Service:
sudo systemctl restart dockerTest Docker Compose:
Create a
docker-compose.ymlfile in your project directory.Define services and configurations in the
docker-compose.yml.Run
docker-compose upto start the services defined in the YAML file.
Automation Script:
#!/bin/bash
# Install Docker
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker.io
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Add current user to the docker group
sudo usermod -aG docker $USER
# Prompt user to log out and log back in to apply group membership changes
echo "Please log out and log back in to apply group membership changes."Save the script to a file, for example, install_docker.sh, then make it executable:
chmod +x install_docker.shTo execute the script, run:
./install_docker.shConclusion:
You have now successfully installed Docker and Docker Compose on your Ubuntu server. This setup enables you to manage and deploy containerized applications efficiently.
Last updated