Deploy a Custom Docker Image And Save The Container Data in AWS ECR

Welcome!

Today we're going to be creating a custom Docker image that outputs the date the container was deployed.

Must-Haves:

  • AWS account with the proper permissions
  • If you're doing this project in cloud9 like me you'll want to add an inbound rule to the security group that's attached to your environment. "All Traffic", from "My IP"

Step 1: Create your directory and file(s)

Create a directory specifically for this tutorial and change into that directory. Inside of it create an "index.html" file and a "Dockerfile" file. Make sure to capitalize the D in "DockerFile" so that Docker will recognize it.

Other than my typo we're looking perfect so far!

Step 2: Pull the Nginx image

From your CLI, run "docker pull nginx" or "docker pull nginx:latest".

Step 3: Building the container

Before we build this container we have to jump into our "Dockerfile" and add a script to open it up on port 8080.

Then we'll hop into our index.html file and add our main script.

Using the command "docker build -t <your image name> ." start your build. Do not forget to put the "." at the end of your command to ensure that it is built in your CWD. The "-t" is for tagging our image so we don't lose track of it.

Now we can run a "docker run -d — name <your container name> -p 8080:80 n ginx".

The -d is used to run the container in the background and print the container id.

-p Is used to publish the container's port(s) to the host.

Step 4: Validation

To ensure our image is up and running, let's head over to the ec2 dashboard and select the instance running our cloud9 environment. Copy the public IP into your browser adding ":8080" after.

Boom! Up and running. Let's also run a "docker ps -a" from our command line to make sure everything looks good.

Step 5: Save the data to ECR

From the AWS ECR dashboard let's choose to create a new repository.

Give your repo a name and leave the rest of the settings as default. After creating your repo, select it and click "View push commands".

Run command #1 to login. Since we've already created the image we don't need to run command #2. For command 3 we'll change the name that we made within the AWS console and replace it with our image ID.

Then we'll just run command #4. (Push)

Let's go back to the ECR dashboard let's check our repo to see if our image was succe ssfully pushed.

Nice! If done correctly you should see our new image in our ECR repo.

Step 6: Clean-up

To remove our containers we'll run "docker rm -f $(docker ps -a -q)". And to remove the image(s) we'll run docker rmi -f $(docker images -a -q). Then just delete your ECR repo and we're all good to go!

I hope this article helps you along your cyber journey! :D

Best Wishes.


Deploy a Custom Docker Image And Save The Container Data in AWS ECR was originally published in Towards AWS on Medium, where people are continuing the conversation by hi ghlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more