In this post, we will be deploying docker image of a GO application on AWS EC2 instance.
This involves below steps
Launching an EC2 instance
SSH to EC2
Installing Docker on EC2
Starting Docker service
Pulling image from docker hub
Running the docker image on EC2
Testing the application
For this post, I have created docker image of a GO rest api and pushed to my docker hub repository. We will be pulling the same go app image from docker hub for deploying onto EC2.
If you want to know how to create docker image of a go application, please have look at this post - Creating docker image of a Go application
Launching an EC2 instance
Once you launch your EC2 instance, you will have a key pair. We will use the key pair for SSH to EC2 in the next step.
Please go through this article to know about, how to launch an EC2 instance
SSH to EC2
If you are using windows powershell, you need to make sure that owner of the keypair file is the user who is executing the command.
In linux or Mac, we change .pem
file permission using he chmod
but in windows chmod won’t work, so we will modify keypair file by manually updating the file by going to file properties->security tab->Advanced button->
and making sure that owner of the file is the user who is executing the ssh to EC2 command.
Click for file permission settings
Once the keypair file permission is set, run below command to connect to EC2 from powershell on windows >= 10
ssh -i <path to keypair file of EC2 instance> ec2-user@<Public IPv4 address of EC2 instance>
example:-
ssh -i c:\firstEC2KeyPair.pem ec2-user@3.4.555.666
Once you are successfully connected to EC2, let’s continue with next steps.
Installing Docker on EC2
simply run the command
sudo yum install -y docker
Once docker install is success, start the docker service.
Starting Docker Service
sudo service docker start
Now, in order to run docker command without sudo, we will need to add ec2-user
to the docker group. Please run the below command.
sudo usermod -a -G docker ec2-user
After this, please logout and login back again to EC2 instance so that your group membership is re-evaluated.
We will now verify if we are able to run docker commands without sudo.
docker info
Pulling Image from Docker hub
We have go application docker image on docker hub. Will will now download it
docker pull bethecodewithyou/go-images:go-rest-v1.0
Running the Docker Image on EC2
listing docker images
docker images
running the docker image of go application
docker run -d -p 8080:7070 bethecodewithyou/go-images:go-rest-v1.0
checking the running container
docker ps
Testing Application
simply run the curl to call product api
curl localhost:8080/products | jq
| jq
in above command is just to format the json.
you can install the jq using command
sudo yum install jq