I made no secret that Portainer is my go-to container manager for standalone Docker instances as well as Docker Swarm. This web-based platform is by far the most powerful, flexible and reliable graphical tool to manage your containerized deployments.
I’ve already introduced you to Portainer, I’ve shown you how to deploy a full-stack application, how to create and use volumes, manage secrets, how to add a Kubernetes environment, and even how to deploy the latest version of Portainer.
This time I’m going to show you a handy trick that you’re sure to add to your Docker container workflow. What we’re going to do is add a Docker compose file to a GitHub repository, then use that repository to deploy a full-stack container to Portainer.
To achieve this, you will need a running instance of Portainer and a GitHub account. Make sure you have these two things in place and ready to go before proceeding.
And, without further ado, let’s get down to business.
The first thing we are going to do is prepare GitHub. There are a few things to take care of. Log into your GitHub account and we’ll add our docker-compose.yml file in a repository. Once logged in, navigate to the repository you want to use and click Add File > Create New File. We’ll use an example WordPress composition file that contains the following content:
version: “3” services: database: image: mysql restart: always environment: MYSQL_ROOT_PASSWORD: wppassword MYSQL_DATABASE: wpdb MYSQL_USER: wpuser MYSQL_PASSWORD: wppassword volumes: – mysql:/var/lib/mysql wordpress: depend_on: last restart: always ports: – “8021:80” environment: WORDPRESS_DB_HOST: database: 3306 WORDPRESS_DB_USER: wpuser WORDPRESS_DB_PASSWORD: wppassword WORDPRESS_DB_NAME: wpdb volumes:
to restart: still
to restart: still
Feel free to use the composition file above, modify it to suit your needs, or use your own composition file. Either way, once you’re satisfied, be sure to enter the necessary commit information and click Commit Changes (Figure 1).
After saving the file, you will need to go back to the root of the repository and click on the Code dropdown menu, then copy the HTTPS link for the repository. This link will be used later in Portainer.
Before leaving GitHub, you must create a personal access token, as Portainer cannot authenticate to your GitHub account with standard username/password credentials.
To create the personal access token, click on your GitHub profile image in the upper right corner and select Settings from the drop-down menu. On the Settings page, click Developer Settings at the bottom left of the navigation, then click Personal Access Tokens. On the resulting page (Figure 2), click Generate New Token.
On the resulting page, enter a note for the token, then select the necessary scopes you want to add (picture 3).
Once your token is configured, scroll down and click Generate Token. You will then be presented with the token, which you will need to copy and use in a moment.
So far you have an HTTP link for the repository and a personal access token.
Configure your Stack in Portainer
Log in to Portainer and select the environment you want to work with. Click Stacks in the left navigation, then click Add Stack. In the resulting window (Figure 4), give the stack a unique name.
In the Repository URL field, paste the HTTPS link to your GitHub repository. Click the Authentication slider until it is in the ON position, then enter the email address you use to log in to GitHub in the Username field. Finally, paste your personal access token into the Personal Access Token field.
You can also enable automatic updates by clicking the slider to the ON position and then setting a fetch interval (the default is 5 minutes). This may be necessary for your project if you regularly update the docker-compose.yml file in your GitHub repository.
By default, this new stack will only be available to administrators. If you prefer to make it available only to specific users, Restricted (near the bottom), then select the users you want to give access to the stack. If you forget to do this at first, you can always change the property by editing the stack and clicking Edit Property at the bottom of the stack configuration page.
Once everything is taken care of, click Deploy Stack and wait for the deployment to complete. You should receive no errors and your stack will be listed on the Stacks page (Figure 5).
And that, my Docker-loving friends, is all there is to deploying a container stack from a Docker compose file in your GitHub repository. Test this feature and see if it helps make your deployments a bit more efficient.
The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.
Featured image via Shutterstock.