Deploy your Docker App on AWS EC2 with CI/CD
Prerequisites:
1. Running Application: Ensure your app is up and running on your local machine.
2. Dockerfile: A Dockerfile should be created for your app.
4. Docker Hub Repository: Create a Docker Hub repository where your image will be pushed,.
3. AWS Account: You need an active AWS account. you’re good to go!
steps to deploy
- Set Up GitHub Actions Workflow:
- In the root directory of your project, create a
.githubfolder. - Inside
.github, create aworkflowsfolder. - In the
workflowsfolder, add a file nameddeploy.yml.
The deploy.yml file will automatically log in to your Docker Hub account, build the Docker image, push it to your Docker Hub repository, access the EC2 server, pull the Docker image, and run it.
name: Build and push on Docker Hub
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: check Out Repo
uses: actions/checkout@v4
- name: login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/Dockerfile.user
push: true
tags: {yourdockerhubusername}/{yourappname}:latest # Update with your Docker image name and username
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
sudo docker pull yourdockerhubusername/yourappname:latest
sudo docker stop web-app || true
sudo docker rm web-app || true
#Update with your Docker image name and username
sudo docker run -d --name web-app -p 3005:3000 {yourdockerhubusername}/{yourappname}:latest
3. Create an EC2 Instance:
- Launch an EC2 instance and ensure it’s set up correctly.
- Add the EC2 instance details to your GitHub secrets.
4. Add Secrets to GitHub:
- Go to your repository’s settings on GitHub.
- Under Secrets, add the necessary secrets (
DOCKER_USERNAME,DOCKER_PASSWORD,SSH_HOST,SSH_USERNAME,SSH_KEY).
make sure your secrets name is same as your deploy.yml file
For Docker Hub credentials:
- Go to Docker Hub settings, generate a new access token with read, write, and delete permissions, and add these credentials to GitHub secrets.
For EC2 credentials:
- Use your EC2 connection string to fill in the secrets:
- SSH_HOST: The EC2 instance’s public DNS (e.g.,
ec2-xx-xxx-xxx-xxx.compute.amazonaws.com). - SSH_KEY: The contents of your
.pemfile. - SSH_USERNAME: Typically
ubuntufor Ubuntu instances.
5. Configure Security Groups:
- Ensure the required ports are open in your EC2 security groups. Add a custom TCP rule for both IPv4 and IPv6.
6. Test the Deployment:
- Make a small change in your repository and push the code.
- The CI/CD pipeline will trigger, and you can watch the deployment process in the GitHub Actions tab.
- Once deployed, you can access your application through your EC2 instance’s public IP address on the specified port (e.g.,
http://<your-ip-address>:3005).
Congratulations!
You’ve successfully deployed your Dockerized app on AWS EC2.
also you can pass your .env on the docker run command
To further enhance your deployment, you can pass environment variables via the docker run command. Additionally, you can configure a custom domain using NGINX.