Docker Compose and Portainer Stacks (Docker Day #1.5)
Today isn't really Docker day, but I would like to introduce you really quick to Docker compose and why its great in combination with Portainer.
Just recently, I posted the first Docker Day guide on how to install Portainer to manage your Docker containers. What I didn't talk about in detail was Docker Compose and why it is so useful in combination with Portainer. And that's what we are going to do today - just in time for Christmas! 🎅
But first things first...
What is Docker Compose ?
Docker Compose is a tool for defining and running multi-container Docker applications - although you can also run a single container from it. 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. Using Docker Compose has several advantages over using the Docker CLI directly:
- It allows you to define your entire application's stack in a single file, rather than having to manually start each service using the docker run command. This makes it much easier to manage your application and its dependencies.
- It automatically handles networking for your application's services. With the Docker CLI, you would have to manually create and connect networks for each service. Compose also allows you to define the networks that your services should be connected to, so you can easily control how they communicate with each other.
- It makes it easy to scale your application. With the Docker CLI, you would have to manually start multiple instances of a service and ensure that they are connected to the right networks. With Compose, you can simply use the docker-compose scale command to quickly scale up or down the number of instances of a service that are running.
In short, using Docker Compose can save you a lot of time and effort when managing multi-container Docker applications. It allows you to define your entire application stack in a single file and manage it with a few simple commands. It also simplifies networking and scaling for your application's services.
The Docker Compose file for installing Portainer
Below you see a Docker compose file to start up a Portainer container. Note that this YAML file is simply a starting point. Feel free to add whatever settings you like or need for your environment. A good starting point for figuring out what you may need or want to add is either the Docker Documentation more generally or the Docker Compose Documentation more specifically.
Docker-compose.yml
In this example, the portainer
service uses the portainer/portainer-ce
Docker image, and it mounts the Docker socket (/var/run/docker.sock
) so that it can communicate with the Docker daemon running on the Synology NAS. The portainer service also exposes port 9000
so that you can access the Portainer web interface. You will need to replace <timezone>
in the environment
section with the time zone that you want to use for Portainer.
Once you have saved the docker-compose.yml
file, you can use the docker-compose
command to deploy Portainer in your CLI/Terminal like so:
What does this have to do with Portainer and why should I care?
One of the great things about Portainer is how it integrates Docker compose. Docker compose file can be easily pasted into a Portainer stack which will nicely organize all the containers your Docker compose file creates when called.
What are Portainer stacks?
Portainer stacks are groups of containers that are defined in a Docker Compose file and deployed as a unit. Stacks allow you to manage multiple containers as a single entity, making it easier to manage and deploy complex applications that consist of multiple containers. With Portainer, you can create, deploy, and manage stacks using the Portainer web interface, making it easy to define and manage your containerized applications.
To create a stack in Portainer, you first need to create a Docker Compose file that defines the containers that make up your application. This file specifies the containers that make up your stack, as well as the services that each container provides and the dependencies between the containers. Once you have created your Docker Compose file, you can use the Portainer web interface to deploy the stack.
Once your stack is deployed, Portainer will automatically create and start the containers that make up the stack. You can then use the Portainer web interface to manage your stack, including starting and stopping the containers, scaling the number of containers in the stack, and viewing the logs for each container. By using Portainer stacks, you can easily manage and deploy complex applications that consist of multiple containers.
An example: Portainer container in a portainer container
Let's build off of our Docker Day #1 result, where we installed Portainer as a Docker container, and setup another Portainer instance as a Docker container from inside Portainer using Portainer stacks and Docker Compose. I know, INCEPTION! Note: this is just an exercise, not something I'd actually recommend doing... 😉
Here are the steps to start a Portainer stack with Docker Compose using the Portainer GUI
- Open the Portainer GUI in a web browser and log in using the default username and password (both are "admin") or the user account you've created earlier.
- Click on the "Stacks" menu item on the left-hand side of the screen, and then click on the "Add Stack" button.
- In the "Name" field, enter a name for your Portainer stack. This can be any name that you choose.
- In the "Stack File" field, click on the "Choose File" button and select the
docker-compose.yml
file that defines your Portainer stack. Just use the yaml file we made earlier.
That's it, you are done!
Now you know how about Docker Compose and how to use it - also in the context of Portainer stacks. This will become handy in many upcoming Docker Days!
I hope this helped! Let me know if you have any other questions.