Docker: Building A Custom Image And Storing It In An AWS S3 Bucket

Matthew Mendez
DevOps Engineer Documentation
5 min readJun 23, 2021

1. What Is Docker?

1a. What are containers?

1b. What is S3?

2. Prerequisites

3. Create Your Own Image Using NGINX.

4. Deploy your container.

5. Save Your Container Data In An S3 Bucket.

6. Clean up.

1. What is Docker?

Docker is an open source containerization platform. Docker enables developers to package applications into containers — standardized executable components that combine application source code with all the operating system (os) libraries and dependencies required to run the code in any environment.

For information on how to install Docker on your OS, check out the link below.

1a. What are containers?

Containers are made possible by operating system (OS) process isolation and virtualization, which enable multiple application components to share the resources of a single instance of an OS kernel in much the same way that machine virtualization enables multiple virual machines to share the resources of a single hardware server.

Containers offer all the benefits of VMs, including application isolation, cost-effective scalability, and disposability. But the additional layer of abstraction (at the OS level) offers important additional advantages:

Lighter weight

Greater resource efficiency

And Improved developer productivity.

1b. What Is S3?

Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

2. Prerequisites.

For this project, you will need to have Docker installed on your OS. I pasted the link above for instruction on how to install Docker. You will also need to have the AWS CLI installed. Here is the link for instructions on how to install the AWS CLI. https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html. I also recommend using the Ubuntu 20.04 server.

Before we begin creating our custom image, lets begin by creating a directory. To keep it as simple as possible, let’s name our directory index.html. Type in the command below.

mkdir index.html

Again, to keep it as simple as possible, within that directory, use the command vim index.html to create a file in that directory and write some html for our webpage.

vim index.html

Here is some Html I wrote for this web page. You can use this example.

After typing out this HTML.

Press esc

Than hold shift + :

Than press wq! to save and quit.

3. Create Your Own Image Using NGINX.

We’re now ready to build our custom image using NGINX.

Within you’re index.html directory, (not to be confused with the index.html file where we wrote out our html) let’s create a Dockerfile.

Type the command vim Dockerfile to get started with writing out our custom image.

vim Dockerfile

Within this Dockerfile we can use the command to create our custom Nginx image.

FROM: This tells Docker that we want to start from the official ubuntu image/latest version.

RUN: The run instruction executes any commands in a new layer on top of the current image and commits the results. This is where we install nginx.

COPY: The copy instruction lets us copy a file (or files) from the host system into the image.

EXPOSE: The expose instruction informs docker that the container listens on the specified network ports at runtime.

CMD: Nginx uses the daemon directive to run in the foreground. We can specify it directly on the command line. This makes it easy to run in debug mode (foreground) and directly switch to running in production mode (background) by changing command line args.

Let’s save and quit with the same command as we did with the index.html file

:wq!

4. Deploy your container.

We can now build our docker container using the docker build command which will pull from our Docker File instructions that we wrote out.

docker build -t (the name that you want to give your container) . <- (don’t forget the period at the end. See my example below.

You will see the container being built with all the instruction that were given in our dockerfile.

My image id is 2d7b65bf83f5

Your image might be different.

Now let’s run our container with the command below.

docker container run -d -p 8080:80 (your container name):latest

See my example below.

  • -d:
  • detached mode:
  • means that a Docker container runs in the background of your terminal.
  • -p:
  • port mapping
  • 8080 hostname(me)/ 80 mapping the port inside the containter.

Next we can open up our pubic ip address with port 8080 open and see if it works.

ip address:8080

It works!

5. Save Your Container Data In An S3 Bucket.

In the AWS console, you can create a bucket manually.

Here I created a bucket.

Next we can compress our image into a tar file.

See my example below

Next we’ll need to enter in our credentials via the AWS CLI.

Here is the link for documentation on how configure your credentials via the AWS CLI.

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

Next we’ll copy our tar file into our s3 bucket.

See my example below.

We can confirm that we copied our tar file into our s3 bucket by going back to the AWS console.

There it is!

6. Clean up.

We can now clean up our environment.

First lets stop our container.

docker stop (container id)

Next lets remove the container

docker rm (container id)

Last lets remove our image

docker rm (container id)

Thanks for reading!

--

--

Matthew Mendez
DevOps Engineer Documentation

Documenting my journey from bartender to a career as a devops engineer