Create A Custom Dockerfile to Build Containers With a Boto3 Environment

Shomarri Romell Diaz
DevOps Dudes
Published in
7 min readMar 6, 2023

A deeper dive into container technology, how to create a basic docker file and create containers.

Docker Overview:

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

There are three core components to Docker. The docker image, docker registry and the docker container, also known as build, ship and run.

  1. Docker Image

Is the universal packager which can be uses across operating systems and cross platform. We use the “docker build” command when creating the image

2. Docker Registry

Is used for app distribution. A Docker registry is a storage and distribution system for named Docker images. The “docker push” command pushes images to a container registry and the “docker pull” command pulls images from the registry.

3. Docker Container

Docker is an operating system for containers. Similar to how a virtual machine virtualizes (removes the need to directly manage) server hardware, containers virtualize the operating system of a server. Docker is installed on each server and provides simple commands you can use to build, start, or stop containers.

Project Prerequisites:

  • Basic Docker knowledge
  • Docker Desktop installed
  • Docker Hub account
  • GitHub repositories
  • Access to a CLI
  • Code editor like VS Code

For instructions how to install Docker Desktop, click here.

Objectives:

  • Build a Dockerfile for Boto3 using Python official image from Docker Hub
  • Download any three repos to your local host from GitHub
  • Build a Docker image
  • Create three containers from the Dockerfile, each container should have a bind mount to one of the repo directories
  • Log into each container and verify access to each repo directory
  • Clean up the containers

Step One: Build a Dockerfile for Boto3

What is a Dockerfile?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Click here to view the Dockerfile reference guide.

A Dockerfile commonly follows the following basic format:

FROM

RUN

CMD

Open a text editor

For the purpose of this project I will be using visual studio code (VSC). Click here to download.

Be sure to have Docker Desktop preinstalled on your computer if not click here to download.

Download the Python extension for VSC, click here.

Download the Docker extension for VSC, click here.

Create a new file in the directory from where you will be working. I have named my file “Dockerfile” (do not include a file extension).

Define Base image with FROM command

First define the base image you want to build from using the “FROM” command. You can search for base images on hub.docker.com. Search for Python and and select the official Python image link. There many image versions we will be using Python:3.7.16.

Your first line of code should be as follows:

FROM python:3.7.16

Install Boto3 with RUN command

We will now use the “Run” command to install boto3 onto our image and eventually our containers.

Add the following line of code:

RUN pip install --no-cache-dir boto3

“pip install boto3" installs boto3 and “ — no-cache-dir boto3” saves disk space by not saving downloaded packages locally.

The final command will be:

CMD "python:3.7.16"

The final Dockerfile should look as follows

Step Two: Download any three repos to your local host from GitHub

Download any three repositories from your GitHub or any other public GitHub repository by copying the repository URL and using the following command:

git clone <repo_URL>

Confirm you have successfully cloned your three repositories by running the “ls” command:

Step Three: Build Your Docker Image

What is a Docker Image?

A docker image simply is the binaries and dependancies needed for any application.

To build the image from the dockerfile we just created, run the following command:

docker build -t <image_name> .

The . fullstop(UK)/Period(US) at the end simply means to build in the current working directory. The “-t” stands for tag and is optional.

To verify the image creation run the command:

docker image ls

Step Four: Create three containers from the Dockerfile, with a bind mount to the cloned repo

What are bind mounts?

Bind mounts are a way to achieve persistent data. Bind mounts map a host file/directory to a containers file/directory. Bind mounts cannot be specified in a dockerfile so they must be specified at the inception of a container when executing the “container run” command.

Documentation on bind mounts can be found on docs.docker.com.

Create Containers with Bind Mounts

To create our three, containers run the following command three times:

docker run -dt --name <container_name> -v "$(pwd)"/<repository_name>:/<directory_name> <image_name>

“docker run” creates the container

“-d” is for detach, to run the container in the background

“-t” stands for TTY and allows the container to carry on running

“-v” is for mounting a volume

“$(pwd)/<repository_name>” is a shell shortcut indicating the source of the file we are mounting, it saves from having to state the full path and will simply print out the current working directory in the path specification for you.

“:/<directory_name> <image_name>” Specifies the target directory from within the container. The image name is the name of the image that was created earlier.

*IMPORTANT NOTICE*

If you find that your container still will not remain open at this stage you can add the following “sleep 100000” to the end of your docker run command instead of the “-dt”:

docker run --name <container_name> -v "$(pwd)"/<repository_name>:/<directory_name> <image_name> sleep 1000000

Verifiy Containers Created

Run the following command to verify all three containers were creating:

docker container ls

Verify Mounts were created

Run the following command on each container to verify that the mounts were successfully created and bound.

docker inspect <container_name>

The output will be a JSON file full of data about the container, scroll down until the see a section titled “Mounts” in which you should be able to verify the mount was successfully created, it should specify the mount “type” as “bind”.

Step Five: Log into each container and verify access to each repo directory

To verify that we can access the container run the following command:

docker container exec -it <container_name> bash

Simply type the command “exit” to exit the bash terminal. Continue to verify that you can access the terminal for your remaining two containers.

Step Six: Clean Up Your Environment

To stop running a container you will run the following Docker stop command:

docker container stop <container-name> 

or

docker container stop <container_id>

If you stop your container using the container ID you do not need to write out the whole ID, the first four digits will be enough.

Confirm your containers have stopped by running the command:

docker container ls -a 

To delete your containers run any one of the following commands

docker container rm <container_name>
docker container rm <container_ID>
docker container prune

Run the “docker container ls -a” command a final time to verifiy the containers are no longer there.

THE END!

If you enjoy cloud engineering content like this and would like to see more, follow me on LinkedIn.

--

--

Shomarri Romell Diaz
DevOps Dudes

DevOps ♾ | Cloud Engineer ☁️ | Linux 🐧 | AWS 🖥️ | Docker 🐳 | Find me at👇 https://www.linkedin.com/in/shomarridiaz/