Docker: Network Bridge with Mount Points pointing to a Common Directory
Tags: devops, aws, docker, community
Published: 2022-10-08 22:41:35


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9az8rud84eg58hgu5vm.png)

In this article, I am going to show you how to create 2 separate containers, connected to a Docker network bridge, both using a centos image and having mount points pointing to the same local directory and its files.

**_Let’s get started!_**


## Objectives:

- Ensure Docker is installed, and the Docker daemon is running.
- Create a new **local directory** called **“webfiles”**.
- Create two files named **“infofile.txt”** and **“exportfile.txt”** in that directory.
- Create a docker network called **“webnet”**.
- Start two containers using the **centos image**, and assign 
  both to the **“webnet”** network.
- Create **mount points** to the local **“webfiles”** directory 
  in both containers
- Verify both containers can see the file created in Step #3.

## Pre-requisites:

- AWS user account with admin access, not a root account.
- Cloud9 IDE, comes with Docker installed.
- An account with [Docker](https://www.docker.com/) and [Docker Hub](https://hub.docker.com/).

## Resources Used:

For this article, I used [Configure Networking](https://docs.docker.com/network/) and Command Line Reference Sections from the [Docker documentation](https://docs.docker.com/engine/reference/run/)

## Steps for implementation of this project:

1. Ensure Docker is installed, and the Docker daemon is running.
2. Create a new local directory called “webfiles”.
3. Create two files named “infofile.txt” and “exportfile.txt” in 
   that directory
4. Create a docker network called “webnet”. 
5. To verify if a Docker bridge network has been successfully 
   created
6. Start two containers using the centos image, and assign both 
   to the “webnet” network. Create mount points to the local 
   “webfiles” directory in both containers
7. Verify both containers are attached to the network bridge
8. Verify both containers can see the files created in the local 
   directory from Step #3.
9. Clean up the Docker Containers, Images, and Network Bridge

###1. Ensure Docker is installed, and the Docker daemon is running.


- Run this command to check whether Docker is installed

**`docker version`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vq5h4q5dpgp67i8nq5y2.png)

- Run this command to check whether Docker Daemon is running

**`systemctl status docker`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd5ei0lyf1oirvt2y65g.png)


###2. Create a new local directory called “webfiles”.

- This is the same directory to which we will mount both of our 
  containers
- Can do it two ways
- **One way** — Right click and select new folder on Cloud9 
  environment
- Name it as **`webfiles`**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/676kharwcilcy6vbjays.png)


- **Second way** — From the command-line interface on Cloud9 IDE,
  run this command

**`mkdir <directory name>`**

**`mkdir webfiles`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuf8nplsf6wo5vjisqdd.png)



###3. Create two files named “infofile.txt” and “exportfile.txt” in that directory.**

- Create 2 files, **infofile.txt** and **exportfile.txt**
- Can do it two ways to add these 2 files
- **One way** — Right click and select new file from the 
  directory that you just created on Cloud9 environment
- Name it as **`infofile.txt`**
- Name it as **`exportfile.txt`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hgb7n1wr3i4hxm8i6t6.png)


- **Second way** — From the command-line interface on Cloud9 IDE,
  run this command

**`touch <directory name>/<file name>`**
**`touch webfiles/infofile.txt`**

**`touch <directory name>/<file name>`**
**`touch webfiles/exportfile.txt`**



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgjaawm0r4gneay59i00.png)


- Run this command to verify **`ls -lt webfiles/`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wphmljgdlml89ml3pyyw.png)


###4. Create a docker network called “webnet”.

**`docker network create -d bridge <network name>`**

**`docker network create -d bridge webnet`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyshxa13kgcwe5adaa7h.png)
###5. To verify if a Docker bridge network has been successfully created

type the following command:

**`docker network ls`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwird6im7negk41e2kyo.png)


- This will list all the Docker networks.
- The first three are created by default.
- default bridge network is listed, along with host and none.
- The latter two **`host`** and **`none`** are not fully-fledged 
  networks, but are used to start a container connected directly   
  to the Docker daemon host’s networking stack, or to start a 
  container with no network devices.

###6. Start two containers using the centos image, and assign both to the “webnet” network. Create mount points to the local “webfiles” directory in both containers

- Repeat the same command with a different container name to 
  create 2 containers

**`docker run -dit --name <container name> --network <network name> --mount type=bind,source="$PWD"/webfiles,target=/webfiles centos:latest`**

**`docker run -dit --name web01 --network=webnet --mount type=bind,source="/home/ec2-user/environment"/webfiles,target=/webfiles centos:latest`**

**`docker run -dit --name web02 --network=webnet --mount type=bind,source="/home/ec2-user/environment"/webfiles,target=/webfiles centos:latest`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4he4mnmn4z8x9c2bdcs3.png)

- The network was created by the first command, but when used to run the second container, it acts as a reference to the already-established network.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1evyuqbgf959zv4l7dq.png)


**what this command means**

**`docker run`** ->is the base command to start the container.

**`-dit`** -> combining three flags -d, -i, and -t means to detach the container from the host and setup an interactive session.

**`--name <container name>`** -> creates container name

**`--network <network name>`** ->this will create and reference the new network bridge

**`--mount type=bind,`** -> create a mount point

**`source=<"/absolute path from host"/webfiles>,`**

**`source="/home/ec2-user/environment"/webfiles,`** ->this is the local host directory

**`target=</path in container>`**

**`target=/webfiles`** -> location of mount point within the container where you want to store the files. If it does not exist, it will be created.

**`centos:latest`** -> latest image that docker will use to build our container.

- To see the containers that we created just now

**`docker ps -a`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z156h8p6hv388hj417uy.png)


Container status is **“UP”**


###7. Verify both containers are attached to the network bridge

**`docker network inspect <network name>`**

**`docker network inspect webnet`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiub8yfia6r6pqvnkykv.png)


###8. Verify both containers can see the files created in the local directory from Step #3.

- First, find the Mount point
- The output is big, have to scroll down to find **“Mounts”** 
  section
- It will give the same Mount Type and Mount Path for both the 
  containers

**`web01 OR web02`**

**`docker inspect <container name>`**

**`docker inspect web01`**

**`docker inspect web02`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vq5sn1jj3b170ie2sgd4.png)


- Let’s verify if the files are accessible from the first 
  container or not.
- Run this command to open up a bash terminal:

**`docker exec -it <container name> bash`**

**`docker exec -it web01 bash`**

- List the directories inside the container using this command.

**`ls`**

- Now, change the directory to the target directory

**`cd /webfiles`**

- You can see the exportfile.txt and infofile.txtfiles with this 
  command

**`ls`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ryebw9orpxbzg5ptz60.png)


- Exit out of the container with the **`exit`** command
- Repeat the same steps to verify if the files are accessible
  from the second container or not.

###9. Clean up the Docker Containers, Images, and Network Bridge

- Run the following commands to **clean up your containers and 
  images**

**`# for removing containers
docker rm -f $(docker ps -a -q)`** 

**`# for removing images
docker rmi -f $(docker images -a -q)`**

- Run the following commands to remove the [Docker Network Bridge](https://docs.docker.com/engine/reference/commandline/network_rm/)

**`# for removing Docker Network Bridge
docker network rm <network name>
docker network rm webnet`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxyqquaxn0ebaxx4riya.png)


###What we have done so far

_In Cloud9 environment, we created two separate containers. Both the containers use a centos image, are assigned to the same Docker network bridge, have mount points pointing to the same local directory, and can see the files inside that directory._