This walk through will contain all the code I use to install my Docker containerization setup. Personally, I use Ubuntu Server however these commands should work for most OS’s.
Once your server OS is installed, configured and up to date, we want to start by downloading the packages needed to install Docker:
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
Next, we’ll want to add the official Docker GPG key to your server. This allows for a secure transmission of information between you and the repository:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Then we’ll be ready to add the repository to the server. I always stick with the “Stable” version, if you want to try the version that gets updated nightly or want to use the test (beta version), you can simply substitute the word “nightly” or “test” where it says “stable”.
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
Installing Docker
Now that we have everything we need to in place to get it installed, lets install it!
Start by updating your package index after adding the new repositories and then run the instal command:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Next, we want to give our user account permission to execute Docker commands:
sudo usermod -aG docker {your username}
Then, I like to do a clean restart of the server to make sure we start with a clean slate:
sudo reboot now
After the server goes through a full restart and you log back in, we will want to run the test container to make sure everything is working correctly:
docker run hello-world
You should see something like this on the screen:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Installing Docker-compose
What is Docker-compose? Its a tool for scripting and running multi-container Docker applications. You can script your container deployment in a YAML file and launch multiple containers as if they are one application (ex. running a MySQL database with your web application). This is huge when it comes to duplicating the same application to different servers with the same settings or customizing your deployment before you even launch it.
First we need to pull the nessary files:
sudo curl -L "https://github.com/docker/compose/releases/download/1.28.6/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next we need to make it executable:
sudo chmod +x /usr/local/bin/docker-compose
Finally, we will want to run the following command to make a symbolic link:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
You should now be able to run the following command and it will return a version and build number, as long as you get that, it is installed and working:
docker-compose --version
Installing Portainer
Portainer is a lightweight webbased GUI that allows you to easily manage different Docker Environments. Even though Docker makes it easy to manage your containers throught CLI on the server, this is my prefered way of managing my servers because it makes it easy to see all your containers, images, volumes, and stacks (Portainers name for YAML scripts).
First step of installing Portainer, we want to create a volume for the files to be stored:
docker volume create portainer_data
Since Portainer runs inside a Docker container itself, all we need to do to get it up and running is execute this single Docker command to pull the image and start the service. You will want to change the command to best suit your environment by changing the Port for the web interface if you would like (I use 8001, if you want something different then just change that single number but leave the “:9000”) or the name of the container and if you want it to restart “always”. Also, you can use the “latest” image to pull the latest updated container image or change that to a specific version if you want to do that.
docker run -d -p 8001:9000 -p 8000:8000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
You should now be able to open a web browser and go to “http://{server hostname or IP}:8001” or whatever port you set in the command.
You will be greated with a basic walk through of getting your username and password setup. Follow the instructions and thats it! You are all setup and ready to run Docker containers.
Stay turned, I will be posting my tips and tricks to really use Docker and Portainer to its full potential and some of my Stacks that I use to deploy applications.