Search This Blog

2023/09/07

BitBucket Pipeline for Express.js & Mysql App running in Docker

 


Create AWS UBUNTU 22 instance ,install docker,docker-compose,nginx

then create a project which has mysql & express.js running in docker container


in /etc/ngix/site-available/default

add

location /api/ {

proxy_pass http://localhost:3000/;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}


then restart nginx server.

In Bitbucket under Repository Setting/pipeline/ssh key generate a key then copy that key to your ubuntu server /home/ubuntu/.ssh/autorized_key
In Bitbucket under Repository Setting/pipeline/repository variable add variables with there value in this case
SSH_SERVER_IP & SSH_SERVER_USER.Here in aws SSH_SERVER_USER will be ubuntu & SSH_SERVER_IP will be public ip of aws server instance.


My bibucket-pipelines.yml looks as follows

image: node:lts

pipelines:
branches:
main:
- step:
caches:
- node
script: # Modify the commands below to build your repository.
- apt-get update && apt-get install -y rsync
- ssh-keyscan -H $SSH_SERVER_IP >> ~/.ssh/known_hosts
- rsync -r -v -e ssh . $SSH_SERVER_USER@$SSH_SERVER_IP:/home/ubuntu/bitbucket-pipeline --delete-before --exclude '.git'
- ssh $SSH_SERVER_USER@$SSH_SERVER_IP 'cd /home/ubuntu/bitbucket-pipeline && sudo docker-compose down && sudo docker-compose up --build -d'
- ssh $SSH_SERVER_USER@$SSH_SERVER_IP 'sudo service nginx restart'


Here i am copying code to /home/ubuntu/bitbucket-pipeline folder then i am running docker compose down & up

your api can be tested at <aws-public-ip>/api/

.dockerfile look as follows

FROM node:20
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000/tcp
CMD [ "npm", "start" ]

docker-compose.yaml looks as follows

version: '3.8'

services:
mysqldb:
hostname: mysql.example.com
image: mysql
restart: always
env_file: ./.env
environment:
MYSQL_ROOT_PASSWORD: $MYSQL_PASSWORD
MYSQL_DATABASE: $MYSQL_DATABASE
ports:
- $MYSQL_LOCAL_PORT:$MYSQL_DOCKER_PORT
expose:
- $MYSQL_LOCAL_PORT
volumes:
- db-config:/etc/mysql
- db-data:/var/lib/mysql
- ./db/backup/files/:/data_backup/data
- ./db-dump/testdump.sql:/docker-entrypoint-initdb.d/0_init.sql
networks:
- turingmysql

app:
hostname: node.example.com
build:
context: .
dockerfile: ./Dockerfile
image: sangram/apistack
ports:
- 3001:3000
expose:
- 3001
depends_on:
- mysqldb
stdin_open: true
tty: true
networks:
- turingmysql

volumes:
db-config:
db-data:

networks:
turingmysql:
driver: bridge


My Code is available at https://bitbucket.org/bitbucketsangramdesai/bitbucket-pipeline/src/main/

No comments:

Post a Comment