Building Dockerfiles, Containers, and Bind Mounts

Destany Parria
Nerd For Tech
Published in
5 min readMay 17, 2023

Hi everyone! Thank you for joining me as I dive into some of the fundamentals of Docker! This is a great application to build your skills on as it can help developers to simplify their development environment setup and ensure consistent execution of their code across different machines. Not to mention, it creates efficiency due to the elimination of redundancy after your containers are created. Let’s go over some key terms and steps that we will work through for the demo!

Docker — An essential part of the continuous deployment process, allowing you to package programs (apps, binaries, libraries) into containers.

Docker Hub — A registry for docker images that can be used to create your docker containers.

Docker Images — A set of layers that provide the instructions required to create a container. To create an image, you can use a Dockerfile or you can create/run a container via the CLI that will generate a image.

Docker Containers — An isolated environment to run any code. It allows you to package and repurpose applications that you would want to use repeatedly. Libraries, code, and files are required for a Docker container.

Steps to complete the task:

  • Build docker file for Boto3 / Python
  • Use the Python official image on Dockerhub
  • Download any three repos to local host
  • Create three Ubuntu containers
  • Each container should have a bind mount to one of the repo directories
  • Log into each container and verify access

Prerequisites:

  • A DockerHub account
  • Docker Desktop
  • Docker installed on your IDE
  • A GitHub account
  • Basic knowledge of Linux

Step 1: Build the Dockerfile for Boto3/Python

Dockerfiles are text files that allow us to define an applications environment and it is typically used to create images. First, we will need to create the directory in the CLI by running mkdir <directoryname> and we will go into that directory by running cd <directoryname>.

Next, create an empty file by running touch Dockerfile and type vim Dockerfilein the CLI to open and input information into the Dockerfile that we will use to build our image. Type the ls -l command and the file should be listed within the directory.

Step 2: Use Python official images on DockerHub to build our image

When pulling an image from DockerHub, you will know that the image is official if you see the “DOCKER OFFICIAL IMAGE” message on it that is shown below.

I will be using this specific version of Python so that I can also have that version of Bullseye in case if I will need certain Debian services. If there is not a specific version that you would like to run, you can just use “python:3” to get the latest version of python that will include boto3.

The “FROM” section creates a layer from the python:3.12.0a7-bullseye image. The “3.12.0a7-bullseye” part is our tag that represents the specific version of Python that we will be using. The “RUN” section installs the application with boto3 but without the cache so that we can save space. The “CMD” section specifies the command to run within the container after it has been built.

Step 3: Download any three repos to local host

There is more than one way to do this, but I will use the git clone <repository_https URL> command to pull my 3 repos.

Step 4: Create three Ubuntu containers with a bind mount

Keep in mind that we will use a bind mount on each of our containers to connect to one of the repository directories. Although there are different types of mounts, bind mounts are a great option for storing permanent data that will remain on the Docker Volume even if the container is deleted. This is also used in cases where you would want your containers to communicate with one another.

Let’s now run the following commands to create our docker containers with a bind mount to the repo directories.

docker run -d -it --name <container name> -v "$(pwd)/docker_project:/myapp" ubuntu

As we can see, our docker_project_c ubuntu container was successfully created. We can also confirm that was created by running the docker container ps command to confirm that our container is listed.

Build the other two containers by running a similar command that we used to create this one.

Step 5: Log into each container to confirm access

Use the docker exec -it <container_name> <shell> command to log into each container. You can then run the ls command in the bash shell to show that you have access to the file information.

Please excuse the typo when I initially typed the stay golden container name.

Congratulations! You successfully completed all of the tasks for this Docker project!

Follow me on LinkedIn at https://www.linkedin.com/in/destanyparria/ to follow my journey in tech!

--

--

Destany Parria
Nerd For Tech

DevOps Engineer that enjoys working with cloud tools and technologies. Follow me on LinkedIn: https://www.linkedin.com/in/destanyparria/