Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab ci/cd deploy docker to aws ec2

We are developing spring boot application which is currently deploying in AWS manually. For that, first we build docker image through Dockerfile and then connect to AWS EC2 instance from laptop & then pull the image and then we use docker run to start it. But we want to automate the process using gitlab CI/CD. We created .gitlab-ci.yml, build stage builds spring-boot application and generates jar file. Package stage then build docker images using Dockerfile from source code and then push the image to registry.

Now i don't know how to finish deploy stage. Most of the tutorials explains only about deploying into Google cloud provider. I use below steps to deploy the docker image...

ssh -i "spring-boot.pem" ubuntu@ec2-IP_address.compute-2.amazonaws.com

sudo docker pull username/spring-boot:v1

sudo docker run -d -p 80:8080 username/spring-boot:v1

Can anybody help me to add above steps into deploy stage. Do I need to add pem file into source to connect to ec2 instance. Or is there any easy way to deploy docker in ec2 from gitlab ci/cd.

like image 859
nani21984 Avatar asked Aug 12 '26 17:08

nani21984


1 Answers

First thing, If there is ssh then it's mean you must provide the key or password by default unless you allow access to everyone.

Do I need to add pem file into source to connect to ec2 instance?

Yes, you should provide the key for ssh.

Or is there any easy way to deploy Docker in ec2 from gitlab ci/cd?

Yes, there is the easiest way to do that but for that, you need to use ECS, the specially designed for Docker container and you can manage your deployment through API instead of doing ssh to the ec2 server.

ECS is designed for running Docker container, Some of the big Advantage of ECS over ec2 is you do not need to worry about container management, scalability and availability, ECS will take care of it. provide ECR which is like docker registry but it's private and with in-network.

enter image description here

deploy-docker-containers

like image 179
Adiii Avatar answered Aug 15 '26 01:08

Adiii