Use AWS to Build Docker Containers with Custom Images | by Darcy Wood | Jul, 2022 | Medium

2022-07-30 13:54:08 By : Ms. Max Zhang

We will quickly deploy a custom docker image using NGINX that will display the current date and time. Then we will save the data to AWS ECR for later use.

If you are new to Docker, buckle up and enjoy the ride! Docker is a great tool for developing, shipping, and running applications. It’s open source, easy to use, and fast. Docker enables you to package your applications into containers that are portable, scalable, flexible, and self-sufficient.

A container holds all the important content like files and programs that an application needs. Multiple containers can run at the same time and each of them could have the same or different images.

An image is an executable template that contains all the directions for your container. You can add your libraries and other dependencies and deploy it all in a single package. Today we’ll start with a base image, NGINX, and then we’ll customize it. Docker is capable of so much more than we’ll cover today, but let’s get started with some basics.

For this step-by-step, I’ll be using AWS, so you’ll need an account with the appropriate permissions. We will use Cloud9 for our IDE and will use AWS ECR to save the data. You’ll want to have the AWS CLI installed and configured.

You will also need a Docker account and will need Docker installed in Cloud9. Access my scripts for both the Dockerfile and index.html on my GitHub account. Links are posted below the pictures of those files.

First, we will create a directory for the files in this project. We’ll head over to Cloud9 and create a folder, or directory, for our project. This is where we’ll store our Docker file and our index.html file. In Cloud9 you can simply right-click on the folder name of your IDE and then select New Folder. I named my folder “customnginx” as you can see below.

Then, in the editor, we can create a new file and name it “Dockerfile”. Be sure to use this specific file name with capitalization so that Docker can easily find this file.

Docker runs instructions in a Dockerfile and runs these instructions in order. To create Dockerfiles begin with a FROM instruction that tells Docker the base image, or parent image, it will be using.

Next, we’ll give a COPY instruction that allows us to copy all files from inside the directory we just created to our current working directory inside the docker container.

We will also add the EXPOSE instruction to let the container listen to the specified port in the network.

And finally, we’ll use CMD, or default command, that will be executed for all the containers of the image. Below you’ll see the CMD command I used that will run NGINX in the foreground and track the process.

If you’d like to dive a little deeper into the Dockerfile documentation, check this out. Here is what our quick and simple Dockerfile will look like.

Up next, we create an index.html file that will display the current date and time on our HTML site. To do that, we’ll want to create an index.html file and then add a code similar to this:

Now that we have created the Dockerfile and the index.html file we are ready to pull our base NGINX image from Docker.

To do this you’ll want to use your terminal in Cloud9. Be sure to get into your project’s directory using: cd <the_name_of_your_directory>

Then, we will use the command docker pull nginx to pull the latest NGINX image. A best practice would be to go to Docker, read through the image information, select the version we’d like to use, and then add that specific version in the command when pulling it down, but for this demo we just need to get the latest version of NGINX and quickly move on.

Now, let’s use the build command to create our custom image. We’ll use a “-t” flag to tag our new image and the “.” at the end of this command tells Docker to build the image from our current directory. Our command to build is: docker build -t <your_custom_image_name> .

Now if we run the docker images command we will be able to see our newly created custom image. It will use the Dockerfile and the index.html file we created earlier when we run the container.

To get our container up and running with this new image we’ll use docker run -d --name <your_container_name> -p 8080:80 <your_custom_image_name>

The “-d” will run the container in a detached state so that it will run in the background and the “-p” will allow us to specify the port we want to open up.

Side note: because we are using an instance on Cloud9, you’ll want to be sure to open up port 8080 on that instance, but be sure to close it back up once you are done with this project.

If you use docker ps you will be able to see information like the container ID, the image it’s using, and even the ports it’s using.

Let’s check to see if our container is set up correctly and working by checking the IP and port in a web browser. Here, I’ll go to my EC2 instance in AWS and copy the Public IPv4 address for the instance associated with the Cloud9 IDE I’m using. I’ll put that IP address, along with :8080 into a browser.

Our customized image is working in our container! Pretty neat.

We’ll save our data to AWS ECR so that we can access it later. To create the repository for ECR, use the command aws ecr create-repository --repository-name <your_container_name> --region <your_region>

If we head over to ECR we are able to see that our repo, has been created, but is inactive.

In order to push our image to ECR we need to authenticate with token. We can find push commands for this process if we select your repository and then select the View push commands button. We can then follow the steps AWS provides. We will just skip step 2 as we have already built our image.

If you were successful you’ll see this:

And when we go back to ECR and select our repository we can see that we have the latest version of our newly created image. We now have access to this for future use.

We successfully used AWS to build a Docker container with a custom image! If you followed along and had success — way to go! I hope you enjoyed this walk through.

Be sure to remove the container and your image when you are done. You can use the following commands:

docker stop <first_four_digits_of_container_id>

docker rm <first_four_digits_of_container_id>

docker image rm <your_custom_image_name>

Now you are all cleaned up and ready to build more containers. I’d love to hear and see what you are creating. Connect with me and let’s chat about Docker!

AWS Certified Developer | AWS | Python | Linux | Docker https://d17m42gyoqj014.cloudfront.net/

Love podcasts or audiobooks? Learn on the go with our new app.

AWS Certified Developer | AWS | Python | Linux | Docker https://d17m42gyoqj014.cloudfront.net/