DEV Community

ADITYA OKKE SUGIARSO
ADITYA OKKE SUGIARSO

Posted on

Simple docker deploy on gcp

Setup git on gcp
Set up personal SSH keys on Linux
Use Git cmd to manage repo
Install docker so we can use docker compose
Create SSL certificate
GCP IP Address
Interact with postgres docker container

Setup git on gcp

To install Git on Linux, follow these steps:

Update the package index:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Git:

sudo apt install git
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

git --version
Enter fullscreen mode Exit fullscreen mode

You should see the installed Git version.

Set up personal SSH keys on Linux

To clone a repo on bitbucket, you need to set up personal SSH keys

sudo apt update && sudo apt install openssh-client
ssh -V
Enter fullscreen mode Exit fullscreen mode
ps -ax | grep ssh-agent
Enter fullscreen mode Exit fullscreen mode

output should be like this

19998 ??         0:00.20 /usr/bin/ssh-agent -l
Enter fullscreen mode Exit fullscreen mode

To start the agent, run:

eval $(ssh-agent)
Enter fullscreen mode Exit fullscreen mode

You may need to add this command to your ~/.bashrc, ~/.zshrc, ~/.profile, or equivalent shell configuration file.
use

nano ~/.profile
Enter fullscreen mode Exit fullscreen mode

then put eval $(ssh-agent) on it

Image description

cd ~
ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f ~/.ssh/{ssh-key-name}
Enter fullscreen mode Exit fullscreen mode

{username@emaildomain.com} is the email address associated with the Bitbucket Cloud account, such as your work email account.

{ssh-key-name} is the output filename for the keys. We recommend using a identifiable name such as bitbucket_work.

Once complete, ssh-keygen will output two files:

{ssh-key-name} β€” the private key.

{ssh-key-name}.pub β€” the public key.

ssh-add ~/.ssh/{ssh-key-name}
Enter fullscreen mode Exit fullscreen mode

To ensure the correct SSH key is used when connecting to Bitbucket, update or create your SSH configuration file (~/.ssh/config) with the following settings:

Host bitbucket.org
  AddKeysToAgent yes
  IdentityFile ~/.ssh/{ssh-key-name}
Enter fullscreen mode Exit fullscreen mode
ssh -T git@bitbucket.org
Enter fullscreen mode Exit fullscreen mode

output should be like this

authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled
Enter fullscreen mode Exit fullscreen mode

Use Git cmd to manage repo

  • clone the repo using clone command
git clone git@bitbucket.org:{org-name}/{repo-name}.git
Enter fullscreen mode Exit fullscreen mode

above is just sample, you can get the command from clone button in bitbucket repository

  • move to repository folder using cd command
cd {repo-name}
Enter fullscreen mode Exit fullscreen mode
  • use git status to check changes and your current branch
git status
Enter fullscreen mode Exit fullscreen mode
  • change current branch using git checkout
git checkout {branch-name}
Enter fullscreen mode Exit fullscreen mode
  • update your local branch and local commit history according to remote
git fetch
Enter fullscreen mode Exit fullscreen mode
  • pull the changes from remote using git pull
git pull
Enter fullscreen mode Exit fullscreen mode

Install docker so we can use docker compose

full step to install docker on debian are in here

summarize steps:

  • Run the following command to uninstall all conflicting packages:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
Enter fullscreen mode Exit fullscreen mode
  • Set up Docker's apt repository. the step are divided by 2:

First, Add Docker's official GPG key, run these commands:

sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
Enter fullscreen mode Exit fullscreen mode

Second, Add the repository to Apt sources, run these commands:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Install the Docker packages.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

Verify that the Docker Engine installation is successful by running the hello-world image.

docker --version
Enter fullscreen mode Exit fullscreen mode

Create SSL certificate

Step-by-step instructions to install Certbot on linux

Install snapd

sudo apt update
sudo apt install snapd
Enter fullscreen mode Exit fullscreen mode

Update snapd

sudo snap install core
Enter fullscreen mode Exit fullscreen mode

Remove previous Certbot packages

sudo apt-get remove certbot
Enter fullscreen mode Exit fullscreen mode

Install Certbot

sudo snap install --classic certbot
Enter fullscreen mode Exit fullscreen mode

Once certbot is installed, execute the following command to ensure the certbot command can be run:

sudo ln -s /snap/bin/certbot /usr/bin/certbot
Enter fullscreen mode Exit fullscreen mode

Generate SSL Certificate with Let’s Encrypt

sudo certbot certonly --manual --preferred-challenges dns -d *.domain.com -d domain.com
Enter fullscreen mode Exit fullscreen mode

you will get acme value to put on your DNS, in above example, you will get 2 acme that need to put orderly one by one, below are sample DNS TXT acme

Please deploy a DNS TXT record under the name:

_acme-challenge.domain.com.

with the following value:

pdUqtrqSHGKKPv3x2yDyvi0hZhMEUmJtxay82Fhd29f
Enter fullscreen mode Exit fullscreen mode

you need to put value above to your DNS dashboard, my DNS dashboard are look like this

Image description

after you add the record(s), you can enter on the cmd to continue to the next acme or the next step

Success generated SSL will have this info

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/domain.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/domain.com/privkey.pem
This certificate expires on 2024-12-17.
These files will be updated when the certificate renews.
Enter fullscreen mode Exit fullscreen mode

use both according your approach how to apply SSL on your domain

GCP IP Address

Upgrade your internal and external IP address on GCP to static, so it will not changing when the instance got restart

Image description

register your external address to your dns

Image description

Interact with postgres docker

connect to docker cli

sudo docker exec -it {docker-container-name} sh
Enter fullscreen mode Exit fullscreen mode

connect to postgres cli

psql -h localhost -U {user-name}
Enter fullscreen mode Exit fullscreen mode

create database

CREATE DATABASE {database-name};
Enter fullscreen mode Exit fullscreen mode

connect to the database

\c {database-name}
Enter fullscreen mode Exit fullscreen mode

or you can immediate put the database name as parameter while connect to postgres cli

psql -h localhost -U {user-name} -d {database-name}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)