Diving into Docker: Build, Create, and Bind mount.

Albania Sinai Rivera
Women in Technology
5 min readApr 5, 2023

I’m starting a new project and I’ve decided to use Docker as my development platform.

Docker is a tool that allows me to create and manage application containers in a secure and isolated development environment. With Docker, I can run my project on any machine that has Docker installed, regardless of the operating system or hardware configuration.

Scenario:

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

Prerequisites

  1. Docker installed.
  2. Text Editor or IDE.
  3. Access to Dockerhub: You will need to access Dockerhub to download the Ubuntu or Python official images.
  4. Basic knowledge of Boto3 and Python.

LET’S DO THIS!!!

1. Create a Dockerfile:

  • Once you install Docker, open VS Code and create a new directory to hold the Dockerfile and other files needed for the image.
  • Click on your new directory and create a new file. in the top menu bar, and then click on “New Folder”. Name the folder “Dockerfile” (or any name you preferred).
  • The next step is to write the contents of our Dockerfile. We need to indicate the image we are going to use. In this case, we want to start with the official Python 3.8 image from Dockerhub and install Boto3 using pip. (do not forget that you need a Dockerhub account set up to be able to see this) See below:
  • Now, let’s head over to our file to create it. We can achieve this by adding the following lines to our Dockerfile. The “FROM” statement tells Docker to start with the Python 3.8 image, and the “RUN” statement installs Boto3 using pip.

2. Download the repositories:

Alrighty, we will need to create three containers for our application as requested in our scenario, and each container will need to access 3 repositories that we need to clone. I am going to clone 3 repos from my GitHub.

After you cloned your repositories, make sure you have them in your directory.

Make sure you have successfully cloned your repos

3. Build the images for the containers:

A Docker image is a template that contains the binaries, libraries, and source code that serves as a set of instructions for how to create a container. To create a custom Docker image from our Dockerfile, we can use the “docker build -t” command followed by the name we want to give our image. This command will execute the instructions that we have defined in the Dockerfile to build the image.

Perfect!!! After has been completed, let’s see the images we have created using the command docker images

Yeeey!!!!!!!!!!!!!!!!!

ok, we are not done yet….but we are almost there.

4. Create Containers each one with a Bind Mount repository.

Ok, as I mentioned before, we are now creating the 3 containers and the bind mount. A bind mount is a feature in Docker that allows you to mount a directory or file from the host machine into a container. This means that you can share data between the host machine and the container, and any changes made to the data in the container will also be reflected on the host machine and vice versa.

To create a container, the command is easy. We will use docker run. The command looks like this:

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

You can see the containers created using the docker container ls command.

Now, you can confirm that the bind mount was created by running the docker inspect command on your container and looking for the “Mount” section. Here's an example command

docker inspect <container_name>

5. Verify we can access the repo directory:

When you run the following command, you are essentially logging into the container's command line interface and have the ability to execute commands as if you were physically sitting in front of the machine. This allows you to check if the bind mount was successful and if you have access to the repo.

docker exec -it <container_name> bash

docker exec: This is the Docker command used to execute a command inside a running container.

-it: These are options that specify an interactive session with a TTY (terminal).

bash: This is the command you want to execute inside the container, in this case, the Bash shell.

As you can see above, I have access to the repo and I can navigate to the mounted directory and check if you have access to the repo files.

Well…this was fun… I think I will publish another project, but next time It will be a little more complex.. until next time!!!!

--

--