Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Build a Boto3 Development Environment Using Docker

Before Docker was created, if a Developer built their code on their machine and sent it to a tester for testing, at times it wouldn’t work on their machine. The reason for this is the difference in computer environments. Docker eliminates this issue by having all of the code and its dependencies in an isolated environment so the type of operating system is no longer a factor. Docker is also more light weight, portable, and very efficient.

Docker Hub has thousands of images you can choose from that you can use to create your container from but at times you may want to create or customize your own image which is what we will do step-by-step.

In this tutorial we are going to build a docker file from an Ubuntu image with Python and Boto3. We are then going to create 3 Ubuntu Containers with a bind mount (storage area/directory) containing GitHub repositories that we download to our local host. Last we will log into each container to verify that we have access to each of our repo directories.

What you’ll need to get started…

  • AWS account
  • Docker Hub Account
  • GitHub Account
  • Basic Understanding of Linux
Step 1: Build a Docker file for Boto3/Python

A Docker file is just a text file with the commands of instructions to build an image line by line. An image in Docker is an immutable read-only document that is the instructions on how the container will be built.

When building a Docker file the order is very important as well as there are required and optional fields you can include depending on how you want your file to look.

FROM(required): Creates a base image.
ENV(optional): A way to set environment variables.
RUN(required): Used to run specified commands. You can use this command more than once but it’s more efficient to run in a single line.
EXPOSE(optional): Exposes ports to a virtual network.
WORKDIR(optional): changes directory
COPY(optional): copy source code from local machine to containers
CMD(*optional): Specifies what commands to run within the container. Runs every time you start or restart the container.

*depends on if the commands are already included somewhere else for example in the FROM line.

Here are some examples of what these commands look like. They do not necessarily go together they’re just examples for the purpose of formatting.

#Each line is a layer in the docker image and it goes top down

FROM :(FROM ubuntu:jammy)

ENV NGINX_VERSION 1.11.10-1~jessie

RUN apt−get −y install vim
RUN apt−get −y update

or

RUN apt−get −y install vim \
&& apt−get −y update

EXPOSE 80 443

WORKDIR /usr/share/nginx/html

COPY index.html index.html

CMD ["nginx", "python", "-g"]

The first thing we are going to do is open up our IDE which I will be using Cloud9. I have already set up my Cloud9 like I have in a previous tutorial to be connected with GitHub.

The first thing we have to do is install Docker. Docker may be installed already and you can check that by typing the following command.

docker version

Since we have Docker installed we are going to create a folder for our Dockerfile first with the following command.

mkdir Docker_Folder

I am going to cd into the Docker_Folder directory and create a file with the touch command called Dockerfile. This is the default name when creating a Dockerfile.

Before we move further we need to create a free account on DockerHub. Take note of your username and password because we will use that later in the tutorial.

Now to begin building out the Dockerfile. We are going to use vim to edit our file.

vim Dockerfile

Remember to begin editing you must use “i” for insert mode to begin editing the file.

FROM ubuntu:latest

WORKDIR /githubfiles

COPY . /githubfiles

RUN apt-get update \
&& apt-get install python3 -y \
&& apt-get install python3-boto3 -y

EXPOSE 80

CMD ["echo", "This is Ubuntu with Python and Boto3!", "sleep 100000"]

To get out of insert mode hit the “escape” button on your keyboard and then the command to save is “:wq” and then hit enter.

Now to build this Dockerfile to make it an image we will use the command…

#Example: docker build -t 

docker build -t chaneljemmott/ubuntu-with-python:1.0 .

#DONT FORGET THE PERIOD ON THE END (.)

Note the line that says Step 6/6. The Docker build command is actually going line by line building your file and then in the end it will give you a message that it was successfully built. If an error occurs it will inform you on which step and what the issue is.

To check that you have the image you created on your local host run the following command.

docker image ls
List of Docker Images

We have our image with our custom tag and a couple of other images from Docker Hub.

The next thing we are going to do is just download 3 repos from GitHub so that we are able to access those repos when we create our containers but first I’m going to create 3 different directories to place each of the different repos in with the following command.

mkdir Github_repo_1 Github_repo_2 Github_repo_3

Now we are going to head over to GitHub and I will use 3 of my own personal repos.

We can use the git clone command with the repo URL to download the repo to our local environment in Cloud9.

git clone https://github.com/your_url
Download repo into directory

As shown in the image above I have placed one repo from GitHub into the Github_repo_1 directory. I will do the same for the other two repos created.

Step 2: Create 3 Ubuntu Containers

Before creating our containers we are just going to push our image to Docker Hub. To connect to Docker Hub we will use the following command.

docker login

You will be prompted to enter your Docker Hub username and password. Note for security purposes as you type in your password the letters will not show up on the screen. Also note if you are using an environment that is temporary when finished you can use the “docker logout” command.

Now we can push our image to DockerHub with the following command.

#docker image push 
docker image push chaneljemmott/ubuntu-with-python:1.0

Now we have our image listed on our profile.

To create our containers we will use the image we just created and we will use the following command.

Note: Make sure that you are in the correct current working directory when entering the command. I am in the Github_repo_1 directory running this command.
#example docker run -d -p 8080:80 -v $(pwd):/path/in/container  sleep infinity
docker run -d -p 8080:80 -v $(pwd):/githubfiles chaneljemmott/ubuntu-with-python:1.0 sleep infinity
docker ps command to see running container

We need to verify that we can access the repo we mounted to our containers so we will enter them with the following command.

#example docker exec -it  bash
docker exec -it 5a3 bash

The image above shows that we are in the root directory of our container indicated by root@5a372473dd14. We also have our folder that was created from our Dockerfile called githubfiles. To get out of the container just type “exit” in the command line.

We can follow the same steps to create our other 2 containers. It’s important to note that running containers can not have the same ports so that will be the only change to the command as well as changing into the correct working directory.

docker run -d -p 8081:80 -v $(pwd):/githubfiles chaneljemmott/ubuntu-with-python:1.0 sleep infinity

Running the docker ps command we can see that we now have 3 running containers. We can also verify that the other two containers have the different directories mounted to them.

Now that we’ve verified that our containers have the mounts we can stop them with the following command.

# Stop running containers
#example: docker stop
docker stop 450

If you’ve made it this far thank you so much for sticking with me and if you’d like to follow more of my journey please connect with me on LinkedIn!

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week


How to Build a Boto3 Development Environment Using Docker was originally published in FAUN Publication on Medium, where people are continuing the conversation by highlighting and responding to this story.

Share the post

How to Build a Boto3 Development Environment Using Docker

×

Subscribe to Top Digital Transformation Strategies For Business Development: How To Effectively Grow Your Business In The Digital Age

Get updates delivered right to your inbox!

Thank you for your subscription

×