top of page
Writer's pictureVaibhav Deshpande

Tutorial blog to create a Docker image & host it on Amazon EC2 instance

Learning Objectives:

  • Learn to create a Docker image of a simple web application.

  • Learn to host a Docker container on EC2 instance.

  • Learn to register a Docker image to Amazon Elastic Container Registry.

Step 1: In AWS management console, go to Amazon EC2 service and Launch an Instance. To perform the following procedure, refer this blog from step no.1 to 7. If you have existing key pair and Security group, you can skip step no.5 & 7.

Name: my-EC2-docker

Operating System: Amazon Linux

Amazon Machine Image (AMI): Amazon Linux 2023 AMI

Instance type: t2.micro

Key Pair: Choose the existing Key Pair, if there is no existing key pair then create a new key pair.

Security group name: WebServer-SG

Description: Security group for Web servers

Inbound Security Group Rules: Keep SSH rule default and click on add security group rule.

Type: HTTP

Port Range: 80

Source: 0.0.0.0/0

Keep the rest as default and click on Launch Instance.

Check your Instance is in running state.

Step 2: Select your EC2 instance and connect to your Linux Instance using SSH.

Now execute the following commands one-by-one given below.

sudo yum update -y

sudo dnf update

sudo dnf install docker

sudo service docker start

sudo systemctl enable docker

sudo usermod -a -G docker ec2-user

Now log out and log back in again to pick up the new docker group permissions.

Execute the following command.

docker info

Now we will create a docker image.

1. To create a docker image, execute the following command given below.

touch Dockerfile

2. Execute the following command to edit the Dockerfile you just created and add the following content. To open nano editor to edit Dockerfile run the following command.

nano Dockerfile

Copy the following script and past into file using CTRL+SHIFT+V

FROM ubuntu:18.04


# Install dependencies

RUN apt-get update && \

apt-get -y install apache2


# Install apache and write hello world message

RUN echo 'Hello from Cloud-PlusPlus World!' > /var/www/html/index.html


# Configure apache

RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \

echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \

echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \

echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \

chmod 755 /root/run_apache.sh


EXPOSE 80


CMD /root/run_apache.sh

To save the file use Ctrl+X then choose Y and Enter.

3. Build the Docker image from your Dockerfile.

docker build -t hello-world .

4. Run docker images to verify that the image was created correctly.

docker images --filter reference=hello-world

5. Run the newly built image

docker run -t -i -p 80:80 hello-world

6. Open a browser and point to the server that is running Docker and hosting your container.

To open the browser, go to your EC2 Instance and copy the public DNS value and run it in a new tab.

You can see the output as “Hello from Cloud-PlusPlus World!”

7. Stop the Docker container by typing Ctrl + c

Step 3: Push your image to Amazon Elastic Container Registry.

Execute the following to configure aws commands.

Note: Here, give your access key id and secret access key from you admin credentials, and specify your region.

aws configure

Access Key Id: AKIAVHXZDFGDGGGHHHW

Secret access key: /n1Zxus+wKCdg56gdgg65dg5g5d6

Region: us-east-1

File type: json

1. Create an Amazon ECR repository to store your hello-world image. Note the repositoryUri in the output.

Replace region, with your AWS Region, for example, us-east-1.

aws ecr create-repository --repository-name hello-repository --region region

2. Replace the aws_account_id and specify your region name in the command highlighted in red.

docker tag hello-world aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

3. Run the aws ecr get-login-password command.

4. Replace the aws_account_id and specify your region name in the command highlighted in red.

aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com

You will get the output as Login Succeeded.

5. Push the image to Amazon ECR with the repositoryUri value from the earlier step.

6. Replace the aws_account_id and specify your region name in the command highlighted in red.

docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

You can go to Amazon ECR service and check your repository is created.

Open repository to see the image we push.

Note: Delete the repository from Amazon ECR and terminate the EC2 instance if no longer needed.



Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.


For your aws certification needs or for aws learning contact us.











Recent Posts

See All

12 Comments


Easy to understand

Like

Easy to understand sir

Like

Atchaya B
Atchaya B
Mar 18

Useful blog sir.

Like

Gokul M
Gokul M
Mar 18

well defined

Like

Gokulnath
Gokulnath
Mar 18

Good blog

Like
bottom of page