Intro
Docker is a popular containerization platform for building, deploying, and managing applications in lightweight containers. Docker allows developers to build reproducible and portable development and production environments.
However, managing Docker can be tedious, especially when it comes to deploying multiple containers for an application. This is where Portainer comes in. Portainer is a graphical user interface for managing Docker containers. In this article, we'll explain how to use Portainer to deploy a stack of Docker containers.
Prerequisites
Step 1: Install Portainer
The first step is to install Portainer. To do this, we will execute the following command:
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
This command creates a volume to store Portainer data, then launches the Portainer container by binding the data volume, Docker socket, and necessary ports. Portainer will be accessible at http://localhost:9000.
Step 2: Create a stack
Now that Portainer is installed and running, we can create a stack. A stack is a set of services that interact to provide a complete application. In our example, we'll create a stack for a WordPress-based blog.
Create a docker-compose.yml file in a new wordpress directory, then add the following code:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
This file consists of two services: a MySQL database service and a WordPress service.
The database service stores data in a volume named db_data.
The WordPress service depends on the database service and can be accessed at http://localhost:8000 .
Stack deployment with Portainer
Now that we have created our docker-compose.yml file, we can deploy it with Portainer. For this we will use Portainer's web interface.
Open your web browser and navigate to the Portainer URL (for example, http://localhost:9000).
Log in to Portainer with your username and password.
Click on "Stacks" in the left menu.
Click on "Add stack".
In the window that opens, enter a name for your stack (for example, "webapp") and paste the contents of your docker-compose.yml file into the "Web editor" text box.
Click on "Deploy the stack".
Portainer will now deploy the stack to your machine. Once the deployment is complete, you can access your web application by opening your web browser and navigating to the URL http: //localhost:8080.
Conclusion
Portainer is a handy graphical user interface for Docker. It makes it easy to manage containers, images, and Docker volumes. In this article, we saw how to use Portainer to deploy a Docker stack. We have created a docker-compose file
Comments