wordpress

Deploy a Container Stack to Portainer from a GitHub Repository – The New Stack


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.

Preparing Github

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:

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).

Added our sample WordPress compose file to GitHub.

Figure 1: Adding our sample WordPress compose file to GitHub.

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.

Generating a new personal access token in GitHub.

Figure 2: Generating a new personal access token in GitHub.

On the resulting page, enter a note for the token, then select the necessary scopes you want to add (picture 3).

Figure 3: Access token configuration in GitHub.

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.

Configuring our new stack which will be pulled from GitHub.

Figure 4: Configuring our new stack that will be pulled from GitHub.

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).

Our wordpress3 stack has been successfully deployed.

Figure 5: Our wordpress3 stack has been successfully deployed.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button