DISCLAIMER!
This guide is provided AS-IS, in the spirit of sharing knowledge!
This gives notice to the following:
DedicatedMC is not responsible nor gives support for the content explained and covered in this guide.
Should you proceed, do so at your own risk.

This guide will explain how to install and setup Docker and Portainer. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers are isolated from each other and offers increased security. Portainer is a web-based GUI which allows you to manage Docker in the browser.


Pre-requisites

In order to follow this guide you will need:
A VPS running Debian or Ubuntu. In this example, I will be using Debian 10 but as Ubuntu is based on Debian, the commands will be the same.


Installing Docker

I’m going to assume you are already SSHed into your machine.

First things first let’s get our system up to date. Running the command below will update all the Linux packages on your machine and the Linux kernel.

# Update our VPS
sudo apt update && sudo apt upgrade -y

Now lets uninstall any old versions of Docker that may already be installed on the system.

# Uninstall the old version of Docker (if there is any)
sudo apt-get remove docker docker-engine docker.io containerd runc

sudo apt-get remove docker docker-engine docker.io containerd runc

It’s ok if apt-get reports that none of these packages are installed.

Next we need to setup the repository that Docker will be installed from. Run these command to do this.

# Setup the repository that Docker will be installed from
sudo apt-get install -y \ apt-transport \ ca-certificated \ curl \ gnupg-agent \ software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add
sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/debian \$(lsb_release -cs) \stable"

Now its time to install Docker. Run these two commands to install Docker.

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

To test that Docker was installed correctly, lets start a Docker container to test it.

sudo docker run hello-world

If all is well, it should output something like this:


Installing Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. It is a very useful tool and requires just 3 commands to install it.


Run this command to download the current stable release of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the installation binary:

sudo chmod +x /usr/local/bin/docker-compose

Run this command to test that Compose is installed correctly:

docker-compose --version

It should output something like this:


Installing Portainer

Portainer allows you to create and manage your Docker containers and images in a easy to use GUI. If you would prefer to just manage Docker through the command line, you can skip this section.

Firstly, we need to ‘pull’ the Docker image that is needed to create the Portainer Docker container.

sudo docker pull portainer/portainer

Now lets run the Docker container using the Portainer image.

sudo docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

Lets check that the container is running with this command.

sudo docker ps

It should output something like this:

It should say `Up` under `STATUS`. If so, it means Portainer is now running!

Visit `http://your-machines-ip:9000` and you should be presented with a screen like this:

Enter a password for the `admin` user and hit `Create user`.

On this screen, select `Local` and then hit `Connect`.

You should then be presented with a screen like this:

Deploying Your First Docker Container with Portainer

Now lets look at how we can deploy our first Docker container. In this example I will be using Wordpress from the default Portainer app templates. You can of course run Docker images from https://hub.docker.com in Portainer, however I won’t be covering how to do this in this guide as how this is done varies depending on how the Docker image is setup.

Note: This is just a demonstration of how to deploy a Docker container, you can choose to run any container you like however I am just using Wordpress in this example.

On the main Portainer page, once you are logged in, click on `local` and then `App Templates` on the left of the page.

In the searchbar, type `Wordpress`, you should see something like this:

Click on the Wordpress template. You should then see a page like this:

All you need to set here is a `Name` for the container (eg. Wordpress) and a password for the MySQL database.

Once you have set these, press `Deploy the stack`. It will then say `Deployment in progress…` do not refresh the page, just wait until it redirects you.

Once deployment has completed, you will be taken to the `Stacks` page. Click on `Containers in the left hand side of the page. This is where you will control your Docker containers.

You should now check that you have 3 `Running` containers and one `Stopped` one from the initial ‘Hello world’ container we ran when we first installed Docker.

You can see that the main Wordpress container, Wordpress MySQL container and the Portainer container are all `Running`. If you see this, it means your good to go!

Under `Published ports` it’s important to make a note of the number before the `:` of your main Wordpress container. This is the port you will need to visit to install Wordpress. In my case this is `49153` however it may be different for you.

If you visit `http://your-machines-ip:container-port` you should now see that you are presented with the Wordpress install screen! Just follow the instructions to install Wordpress.

Once you have filled out the details and hit `Install Wordpress` you should see a message like this.

Now if you visit `http://your-machines-ip:container-port` you should be presented with a fresh Wordpress install! To login to Wordpress and start editing your site, go to `http://your-machines-ip:container-port/wp-admin`.

Congratulations! You’ve just installed Docker and Portainer and deployed your first Docker container!!