Create Our Own Custom Image In Docker

Docker: So what are we lookin at anyway?

Docker is an open source platform that enables developers to build, deploy, run, update and manage containers. Docker combines application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

Containers simplify the development and delivery of distributed applications. They are becoming more popular as organizations move to cloud-native development and hybrid multi-cloud environments. It’s possible for developers to create containers without Docker, by working directly with capabilities built into Linux and other operating systems. But Docker makes containerization faster, easier, and safer

For this project we will:

  • Create our own image using Nginx, then add a file that will tell you the date the container has been deployed
  • Deploy our container with port 8080 open
  • Save our container data to the AWS Elastic Container Registry (ECR)

*I will be using a t2.micro EC2 instance created in AWS running Ubuntu 22.04*

Prerequisites:

  • AWS CLI installed on Ubuntu instance >> sudo apt install awscli
  • A Docker Hub account
  • Docker installed on a Linux local machine or instance >> Install Docker, Ubuntu specific installation
  • Security group on your instance that allows inbound rules for port 22 (SSH) and port 80 (HTTP)
  • Here are some helpful post installation steps to make life more comfortable. Mainly, not having to use sudo for every docker command*

Once you have Docker installed, quickly check to see if you can see the version using this command docker --version

if you can see this Docker is installed on your instance

STEP ONE: CREATING OUR IMAGE

First create a directory we will work from and place our files…

mkdir docker-project

ls

cd docker-project

We are now inside our new directory for the project. Create a file that we will modify using the VIM editor …

touch index.html

ls

Now vim index.html

Then paste in the following HTML file. This is just a simple HTML site that will display the time and date the container was run…

Lets pull the image from Docker Hub with the following command…

docker pull nginx:1.23

*It is good practice to always include the version name when pulling images or deploying containers in a production environment*

A quick docker image ls to verify the image is there…

Notice the version tag of 1.23 we specified in the pull command

Now that we have our index.html files saved in the current directory, lets deploy the container with the following command…

docker run -d -p 8080:80 -v "$(pwd):/usr/share/nginx/html:ro" --name nginx nginx:1.23

-d will run the container in a detached state so it runs in the background

-p will open up the port 8080:80 that we specified

-v will bind mount volume into the directory we specified for the container

--namename the container to ‘nginx’ as we specified

  • and the last nginx:1.23 specifies the image version we pulled earlier to create the container from

You will see the random container name generate upon a successful run. Lets run a quick docker ps to verify our container is running…

Lets verify we can see our new container in a web browser. We should be able to see the page display our index.html file. Back at my EC2 management console I will copy the IPv4 address of my EC2 instance…

Now paste your IPv4 or local address into the browser with :8080 like so…

Success!

Now lets go into our container and make some changes. To work inside of your new container enter docker exec -it <first 3 digits of container> bash

exec lets you run or, execute a command in a running container

-it gives and interactive terminal to work with

Specify the first few digits of the CONATAINER ID

bash will start a bash session in the container

As you can see above I am now inside the container as the root user. Lets run a couple updates and install something like Python and Vim…

apt-get update
apt-get install vim -y
apt-get install python3 -y

Once you’ve you updated and installed vim and python enter exit

Now lets docker commit <container name>

You have just saved a new image of your container with the updates and installations you made. Check with docker image ls and notice the difference in size between the two images. Your new image is significantly larger with vim and python installed…

Lets add a tag to this new image so it is easier to identify with the following command…

docker tag <your new image id> nginx.v2
docker image ls
See your newly tagged image

STEP TWO: Save Our Container Data to AWS ECR

Back at our AWS console, search for Elastic Container Registry and click Get Started to create a new repo…

Make sure the repo is private, give the repo a name and leave everything else default, then click Create repository at the bottom of the page…

Once your repo is created, select the repo and click View push commands…

We will be using command 1, 3 and 4 since the second command is only if we haven’t yet built our image.

*Make sure you have AWS configured on your machine with aws configure and your credentials*

First command…

Third command…

Fourth Command…

Now if we click on our repo back at ECS management console we can see our latest image…

And with that, you have just completed all the objectives of this project.

To recap you have:

  • Created our own image using Nginx, then added a file that tells you the date the container has been deployed
  • Deployed the container with port 8080 open and tested it
  • Saved your container data to the AWS Elastic Container Registry (ECR)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store