How to upload spring boot project in ec2 instance with docker-compose
To deploy your application on an EC2 instance. I'll assume you have an EC2 instance set up and have Docker and Docker Compose installed on it.
For inbound and outbound rules you may refer
If you don't have docker and docker-compose then follow the below instructions.
Step 1: SSH into Your EC2 Instance
ssh -i your-key.pem ec2-user@your-ec2-instance-ip
Replace your-key.pem
with the path to your SSH key file and your-ec2-instance-ip
with your EC2 instance's public IP address.
Step 2: Install Docker
Run the following commands to install Docker on your EC2 instance. These commands are for installing Docker on a typical Amazon Linux instance. If you're using a different distribution, you may need to adjust the commands accordingly.
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
The last command adds the ec2-user
to the docker
group, allowing you to run Docker commands without sudo
. However, you'll need to log out and log back in or run newgrp docker
for the group changes to take effect.
To check the docker version and run the command docker -v
Step 3: Install Docker Compose
Run the following commands to install Docker Compose on your EC2 instance:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
Step 4: Create a Docker Compose File
Create a docker-compose.yml
file on your EC2 instance to define your application and PostgreSQL service, just like you did locally. You can use a text editor like nano
or vim
to create the file.
nano docker-compose.yml
Then, paste the contents of your docker-compose.yml
file that you've configured to use your remote Docker image from Docker Hub.
Alternatively,
Upload your docker-compose.yml
file to your EC2 instance. You can use the scp
command to transfer the file from the local to the ec2 instance. open a new terminal in your local file directory and follow the following command.
scp -i your-key.pem docker-compose.yml ec2-user@your-ec2-instance-ip:/path/to/your/project
Step 5: Start the Docker Containers
Run the following command to start your Docker containers using Docker Compose:
docker-compose up -d
The -d
flag runs the containers in detached mode, meaning they will continue running in the background.
Step 6: Access Your Application
If you have configured the security group of your EC2 instance to allow incoming traffic on the necessary ports (e.g., port 8080 for your Spring Boot app), you should be able to access your Spring Boot application in a web browser using your EC2 instance's public IP address. For example: http://your-ec2-instance-ip:8080
Step 7: Monitor the Logs (Optional)
You can monitor the logs of your running Docker containers to check for any issues or debug information. Use the following command to view the logs of your containers:
docker-compose logs -f
The -f
flag allows you to follow the logs in real time.
To verify that your application is running, you can check the logs or use Docker commands to inspect containers:
# Check running containers
docker ps
# View logs for a specific service (replace 'service-name' with the actual service name)
docker-compose logs <service-name>
Step 8: Stop and Clean Up (Optional)
If you ever need to stop and remove your Docker containers, you can do so using the following command:
docker-compose down
This will stop and remove the containers but retain your data volumes.