The title speaks for itself, let’s jump right in!
As a preliminary step, we start from a user registered in DigitalOcean with a validated account.
Use the doctl tool for the entire communication process with DigitalOcean.
Step 1: Install doctl
$ cd ~ |
Step 2: Create an API token
Go to https://cloud.digitalocean.com/account/api/tokens
Generate new token for read and write and save apart the value of token generated
<TOKEN NAME>: personaltoken
<TOKEN VALUE>: 6e981fc2a674dbb7a610b9b85d0c8b00
Step 3: Use the API token to grant account access to doctl
$ doctl auth init --context personaltoken |
Prompt for <TOKEN VALUE>, then enter it and press return
Step 4: Validate that doctl is working
|
Prompt for <TOKEN VALUE>, then enter it and press return
Validate by obtaining the account information
|
Step 5: Create a Container Registry with doctl
<MY-REGISTRY-NAME> : container-nyc-795
<REGION> : nyc3
|
Important: the region of the Container registry and Kubernetes cluster MUST be the same
Keep in mind that container names must be unique, must be lowercase, and only accepts alphanumeric characters and hyphens.
Step 6: Login to authenticate docker with your registry
|
Step 7: Create kubernetes cluster
|
Important: the region of the Container registry and Kubernetes cluster MUST be the same here as well.
This operation isn’t a fast process.
Step 8: Integrate kubernetes cluster in Container register
|
Step 9: Get token certificate and connect to cluster
|
To validate this, use the kubectl tool to get context. If is not installed, get the last version you find - for example in googleapis
|
Check with
|
Here, you see: bash prompt do-ny3-cluster-sttic-example. That is the context you created in Step 7
Step 10: Generate docker image, tag and push to DigitalOcean
We assume that the user already has docker installed
|
Inside Dockerfile put
|
We create simple Dockerfile with NGINX Server and copy the file hello.html in the default html of nginx
|
Inside hello.html put
|
Then build a docker image file tag with repository and push with docker
|
Step 11: Create app
Create file example-static-app.yaml and insert:
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
name: example-static-app
region: nyc
services:
- http_port: 80
image:
registry_type: DOCR
repository: static-app
tag: latest
instance_count: 2
instance_size_slug: professional-xs
name: static-service
routes:
- path: /
source_dir: /
Validate file with
|
Create app
|
If you access https://cloud.digitalocean.com/apps
And in the live url + /hello.html you can see:
Step 12: Deploy app
If you modify the code of your app, you need to generate a new image with docker and push (see step 10). Then you don’t need to create a new app, you need to deploy the image in the already created app, with the id executing the command to deploy in bash.
|
How to generate deploy of Docker image to Container Registry on DigitalOcean Platform Apps
Once you have created the application (and if you have the code in gitlab), you can create a direct deployment of your code in the DigitalOcean container and deploy on top of your application.
Step 1: Define variables
First two variables are defined in gitlab. You can find these inside project in the left menu - enter in Settings > CI/CD > Variables
$DIGITALOCEAN_API_KEY = token generated in DigitalOcean dashboard
$APP_ID = previously generated application identifier
More can be defined as the name of the repository. The value of these variables will be injected into the file that we will create below in the Step 3
Step 2: Register runner in gitlab
In your project in the left menu, go to Settings > CI/CD > Runners
Create a specific runner for the project in the URL with registration token. Register GitLab Runner from the command line. It is important to use docker and privileged
# Download the binary for your system |
Step 3: Create file gitlab-ci.yml
The gitlab-ci.yml file takes care of
-
Authentication and identification using doctl in DigitalOcean
-
Generating and sending the docker image to the DigitalOcean container
-
Deploying the container image to an existing app
Create the file gitlab-ci.yml in the root of your project as
|
Now, every time you do a git push in your project, the runner will automatically inject the variables defined previously in the gitlab-ci.yml file. Then, it will generate a docker image with docker (docker-in-docker) to create an image of your project, send it to the digitalocean repository and deploy it in the app configured.
That’s it!