Custom Image Deployment with Docker

Tucker Clinton
6 min readMay 3, 2022

Imagine your team needs you to deploy a custom image quickly and write a quick script in a file to accomplish a task. You will do a task that you may not have done before so make sure you research if you do not know how.

Objectives:

1. Create your own image using nginx. Find the time of day the container has been deployed.

2. Deploy your container with port 8080 open

3. Save your container data in ECR

We will be using Docker for this lab. If you do not have Docker Desktop installed, please follow the link below and follow the installation steps for your respective operating system.

https://www.docker.com/products/docker-desktop

Docker is a platform that allows you to develop, test, and deploy applications as portable, self-sufficient containers that run virtually anywhere.

Before we run a docker container, we need to download an image for that container. A docker image is essentially a file that contains all of the necessary configurations to run an application. In this case we’ll be using a nginx image, but we want to make some adjustments to it, so we won’t be pulling it directly from the docker hub. We can make adjustments to an image through a Dockerfile. Dockerfile is a text file with instructions to build and image. Follow the following 4 steps to create your docker container with a Dockerfile.

Step 1: Create a file named Dockerfile in the directory of your choice

Navigate to the directory and run:

touch Dockerfile

Step 2: Add instructions in Dockerfile

Use your favorite text editor to edit the Dockerfile. In this example we’ll use vim. Add the instructions to your Dockerfile to

vim Dockerfile

FROM: This tells Docker that we want to start from the official nginx base image

RUN: The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. We’ll update and install the latest version of nginx.

EXPOSE: The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. Objective 2 is to deploy the container with port 8080 open , so we’ll give that instruction here.

CMD: The main purpose of a CMD is to provide defaults for an executing container. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. The “daemon off” parameter will run nginx in the foreground and prevent the container from stopping after the command is run.

Press ESC and :wq to save and exit the file.

Now that we have our dockerfile we can create our image.

Step 3: Build dockerfile to create image

Now use docker build to build the image according to the contents of our Dockerfile.

Enter the docker images command in the terminal to bring up our running images:

Step 4: Run the newly built image to create a container

docker run —-name <name your container> -d <image name>

Now that we’ve created our container, we can move on to the objective of finding the time that the container was deployed. There are different ways that we can accomplish this, but let’s go over two of them.

We can run the docker logs timestamps command to add a RFC3339Nano timestamp to each log entry. This will tell you the date and time of each entry including when the container was deployed.

You can also achieve this by using the docker inspect command. Inspect will show all the info about a container. Since it is a lot of information and can be pretty overwhelming, let’s filter down to just what we need. To do this, pipe the docker inspect <container name> command to grep “Created”. As you can see this will return just the date and time the container was created.

We no can move on to the final objective, save your container data in ECR.

ECR (EC2 Container Registry) is an AWS managed container image registry service. Amazon ECR supports private container image repositories with resource-based permissions using AWS IAM. This is so that specified users or Amazon EC2 instances can access your container repositories and images.

Prerequisites: Have an AWS account, Have an IAM user created, installed the AWS CLI

AWS does a good job of laying out the steps to push an image to ECR, so let’s follow their steps below.

From the navigation bar, choose the Region to create your repository in.

In the navigation pane, choose Repositories.

On the Repositories page, choose Create repository.

For Repository name, enter a unique name for your repository. The repository name can be specified on its own (for example nginx-web-app). Alternatively, it can be prepended with a namespace to group the repository into a category (for example project-a/nginx-web-app).

Choose Create repository.

Select the repository that you created and choose View push commands to view the steps to push an image to your new repository.

Run the login command that authenticates your Docker client to your registry by pasting the command from the console into a terminal window. This command provides an authorization token that is valid for 12 hours.

(Optional) If you have a Dockerfile for the image to push, build the image and tag it for your new repository. Pasting the docker build command from the console into a terminal window. Make sure that you are in the same directory as your Dockerfile.

Tag the image with your Amazon ECR registry URI and your new repository by pasting the docker tag command from the console into a terminal window. The console command assumes that your image was built from a Dockerfile in the previous step. If you did not build your image from a Dockerfile, replace the first instance of repository:latest with the image ID or image name of your local image to push.

Push the newly tagged image to your repository by pasting the docker push command into a terminal window.

Refresh the ECR repository in the aws console and your latest image tag will show up:

Thanks for following this walkthrough.

--

--

Tucker Clinton

Working toward a career in cloud engineering. I’ll be documenting some of my progress here!