Utilizing Python and Boto3 to create Ubuntu Docker Containers.

Abidoye Joshua mayowa
6 min readApr 29, 2023

--

Welcome to Week 16 with LUIT, For this week’s project, we are going to create three Ubuntu docker containers using Python Boto3 to build the docker file on AWS Cloud9(IDE).

Project Scenario:

A software development team is working on a Python-based application. The team needs to simplify their development environment setup and ensure consistent execution of their code across different machines.

The team can create a Dockerfile for Boto3/Python that utilizes either the Ubuntu or Python official images available on Dockerhub. The Dockerfile can be customized to include any necessary libraries or dependencies required by the team’s Python application.

The team can then download three repositories to their local host and create three Ubuntu containers using the customized Dockerfile. Each container can be configured with a bind mount to one of the repo directories, allowing the team to access the code and make changes as needed easily.

Finally, team members can log into each container and verify access to each repo directory, ensuring that the containers have been properly configured and that the team can effectively collaborate on their codebase. By utilizing Docker, the team can also ensure that their development environment is portable and easily replicated on different machines, streamlining their workflow and enabling efficient collaboration.

What is Docker?

Docker is a platform for developing, shipping and running applications. In simple terms, you can build your application, package them along with their dependencies into a container, and then these containers can be shipped to run on other machines.

Basic Docker Terminologies

  • Dockerfile- A text file that consists of instructions and arguments. It informs Docker how the image should get built.
  • Image- It is a read-only template with instructions for creating a container. Each instruction in a Dockerfile creates a layer in the image.
  • Container- It is a running instance of an image. It is well defined by its image as well as any configuration options provided to it create or start.

Prerequisite:

Step 1: Install Docker in AWS Cloud9

To install docker using AWS CLI, we need to use the codes below, type them line by line, and run the codes one after the other to achieve our desired aim.

sudo yum update -y
sudo yum install docker -y
curl -sfL https://get.k3s.io | sh -
docker version

If you use the codes above, you should get something like what's in the image below.

Great, we have successfully installed the latest docker version on AWS Cloud9.

Step 2: Build a Docker file for Boto3/Python

In IDE, navigate to File> New From Template > Python File> Save a “Dockerfile” without an extension.

we will use this code below in our docker file above and click on save to save the file.

#Week 16_Project
#Build Dockerfile for Boto3/Python
#Use Ubuntu official image on DockerHub

#syntax-docker/Dockerfile
FROM ubuntu:22.04

#Install app dependencies
RUN apt-get update
RUN apt-get -y install python3-pip \
&& pip install boto3
#set the default command to tail the /dev/null file
ENTRYPOINT ["tail", "-f", "/dev.null'"]

After setting up the Dockerfile, the next thing is to use the command below to build the Ubuntu image in the CLI.

docker build -t (name)/ubuntu.22.04 .

It will take about a minute to complete the image build.

Let’s confirm the image was successfully built by running the command below.

docker image ls

congrats, we can see the image from the image above Joshua/ubuntu. 22.04.

Step 3: Pull Ubuntu Docker official image

Let’s head to hub.docker.com and search for supported Ubuntu official images.

Use the Pull command copied from the Ubuntu Docker official image & run the following command in the AWS CLI.

docker pull ubuntu

Great! We see the latest image is up to date.

Step 4: Download 3 repositories to the local host

I Created 3 repositories initially to my own GitHub account. I am going to choose the 3 repos there. In order to clone the repos, run the command for all 3 repositories in AWS CLI.

Use the command below to clone the repositories.

git clone <repo URL>

git clone https://github.com/Abidoye95/Ubuntu1.git
git clone https://github.com/Abidoye95/Ubuntu2.git
git clone https://github.com/Abidoye95/Ubuntu3.git

Let’s run ls, to confirm all three repositories are there and cloned.

The image above shows the three repositories, Ubuntu1, Ubuntu2, and Ubuntu3, all present in the folder.

Step 5: Create 3 Containers with bind mount to a repository.

What are bind mounts?

Bind mounts are a way to achieve persistent data. Bind mounts map a host file/directory to a container's 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.

We need to login to our dockerhub account and input the login details.

In order to create our three containers, run the following command thrice:

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

Description of the tags

  • -name: To the container
  • -v: to bind mount a volume
  • -d: to run the container in the background and print the container ID
  • -t: to allocate a pseudo — TTY. It opens a terminal inside the container.

Let’s check to see if all three containers are running.

docker container ps

From the image above, We can see our three containers, namely Containers A, B and C.

Step 6: Inspect all 3 containers to check the mounts of the containers.

We’ll use the code below for inspection.

docker container inspect <containerName>

docker container inspect containerA
docker container inspect containerB
docker container inspect containerC
ContainerA
ContainerB
ContainerC

Step 7: Verify access to each repository directory by login into each container.

We’ll use the code below for verification.

docker exec -it <container_name_or_id> bash

docker exec -it containerA bash
docker exec -it containerB bash
docker exec -it containerC bash

Great!!

we need to delete our containers. To do that, let’s run the following commands below.

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

By typing Y, it will remove all stopped containers.

We can see that all three containers have been successfully deleted.

Thanks for reading along, guys. We have completed the project.

If you enjoy cloud engineering content like this and want to see more, follow me at: https://www.linkedin.com/in/joshua-abidoye-0ab796195/

--

--

Abidoye Joshua mayowa

DevOps Engineer. I'm interested in collaborating with anyone interested in cloud engineer or cloud DevOPs.