Setting Up Docker with Portainer on Ubuntu

Docker is an open-source platform that enables developers to automate the deployment, scaling, and management of applications within lightweight, portable containers. Containers encapsulate everything an application needs to run—code, runtime, libraries, and dependencies—making it easy to create, deploy, and run applications in any environment. With Docker, you can ensure that your software runs reliably regardless of where it’s deployed.

In this post, I’ll teach you how to set up a basic Docker installation with Portainer on Ubuntu. Portainer is a powerful management tool that provides a user-friendly interface for managing your Docker containers, making it easier to monitor and control your containerized applications. Whether you’re a seasoned pro or just starting out, this guide will help you get everything up and running smoothly.

Step 1: Remove Conflicting Packages

First, we need to ensure that no conflicting packages are interfering with our Docker installation. Run the following command to remove any previously installed Docker packages:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

Step 2: Install Docker

Now, let's add the official Docker APT repository. Run these commands line by line:

  1. Update the package index: sudo apt-get update
  2. Install necessary packages: sudo apt-get install ca-certificates curl
  3. Create the keyrings directory: sudo install -m 0755 -d /etc/apt/keyrings
  4. Add Docker's official GPG key: sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  5. Change the permissions for the key file: sudo chmod a+r /etc/apt/keyrings/docker.asc
  6. Add the Docker repository to APT sources:
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
  • Update the package index again: sudo apt-get update
  • Finally, install Docker: sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 3: Verify the Installation

To ensure everything is working correctly, you can run the following command:

sudo docker run hello-world

This command should pull a test image from Docker Hub and display a message confirming that Docker is installed correctly.

Step 4: Set Up Portainer

Now let’s move on to setting up Portainer, which provides a user-friendly interface for managing Docker containers.

  1. Create a Docker volume for Portainer: docker volume create portainer_data
  2. Run the Portainer container:
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:2.21.3

Step 5: Accessing Portainer

You can access Portainer by navigating to http://localhost:9443 or https://YOUR-IP:9443 if you’re using a VPS or dedicated server. You will be presented with the initial setup page for Portainer Server.

Run through the setup process, and you should have a fully functioning Docker server with Portainer!

Conclusion

Congratulations! You’ve just set up Docker and Portainer on your Ubuntu system. This setup will allow you to manage your containers with ease and streamline your development workflow.