# #Day16 Docker Networking 🐳🌐 Secure containers with custom bridge

### Docker Networking

Networking allows containers to communicate with each other and with the host system. Containers run isolated from the host system and need a way to communicate with each other and with the host system.

By default, Docker provides two network drivers for you, the bridge and the overlay drivers.

`docker network ls`

### Bridge Networking

The default network mode in Docker. It creates a private network between the host and containers, allowing containers to communicate with each other and with the host system.

![image](https://user-images.githubusercontent.com/43399466/217745543-f40e5614-ac34-4b78-85a9-91b24512388d.png align="left")

If you want to secure your containers and isolate them from the default bridge network you can also create your own bridge network.

`docker network create -d bridge my_bridge`

Now, if you list the docker networks, you will see a new network.

`docker network ls`

This new network can be attached to the containers, when you run these containers.

`docker run -d --net=my_bridge --name db training/postgres`

This way, you can run multiple containers on a single host platform where one container is attached to the default network and the other is attached to the my\_bridge network.

These containers are completely isolated with their private networks and cannot talk to each other.

[![image](https://user-images.githubusercontent.com/43399466/217748680-8beefd0a-8181-4752-a098-a905ebed5d2a.png align="left")](https://user-images.githubusercontent.com/43399466/217748680-8beefd0a-8181-4752-a098-a905ebed5d2a.png)

However, you can at any point of time, attach the first container to my\_bridge network and enable communication

`docker network connect my_bridge web`

[![image](https://user-images.githubusercontent.com/43399466/217748726-7bb347d0-3736-4f89-bdff-31d240b15150.png align="left")](https://user-images.githubusercontent.com/43399466/217748726-7bb347d0-3736-4f89-bdff-31d240b15150.png)

### Host Networking

This mode allows containers to share the host system's network stack, providing direct access to the host system's network.

To attach a host network to a Docker container, you can use the --network="host" option when running a docker run command. When you use this option, the container has access to the host's network stack, and shares the host's network namespace. This means that the container will use the same IP address and network configuration as the host.

Here's an example of how to run a Docker container with the host network:

`docker run --network="host" <image_name> <command>`

Keep in mind that when you use the host network, the container is less isolated from the host system, and has access to all of the host's network resources. This can be a security risk, so use the host network with caution.

Additionally, not all Docker image and command combinations are compatible with the host network, so it's important to check the image documentation or run the image with the --network="bridge" option (the default network mode) first to see if there are any compatibility issues.

### Overlay Networking

This mode enables communication between containers across multiple Docker host machines, allowing containers to be connected to a single network even when they are running on different hosts.

### Macvlan Networking

This mode allows a container to appear on the network as a physical host rather than as a container.

let's do thinks practically

`docker run -d --name login nginx: latest`

to run a container named "login" using the "nginx:latest" image,

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701682856011/bcb34576-fd98-4ba0-8a3b-63dd1ed44ca6.png align="center")

`docker exec -it login /bin/bash`

`docker exec` command is correctly used to execute a command in a running Docker container named "login." However, to access the bash shell interactively, you should use `/bin/bash` as the command.

This will open an interactive bash shell within the "login" container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701682928789/76638d32-a896-4a22-a489-3e1061186f08.png align="center")

`apt-get install iputils-ping -y`

installing the `iputils-ping` package using `apt-get` in a Debian-based Linux distribution. The `-y` flag is used to automatically answer "yes" to prompts during the installation process.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701683546198/d1fb138d-2acb-4152-871e-4998de24dc10.png align="center")

`ping -V`  
The `ping` command with the `-V` option is used to display the version of the `ping` utility

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701683562008/9cf2cbcc-97e0-4fa0-a640-f79b685f5f4c.png align="center")

`docker ps`

`docker ps` command is used to list the currently running Docker containers on your system

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701683880462/45a7cec2-3a99-48ec-a885-ee0468ef570d.png align="center")

these are the two containers are running let see the IP address of the login container

To see IP address of the container we use the command

`docker inspect login`

`docker inspect` command provides detailed information about Docker objects, including containers.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684002096/d80b5894-e0ec-46ef-83e7-e5af763ee961.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684324926/081114ac-d6b5-4384-a849-0f3196363402.png align="center")

we can see the IP address 172.17.0.2

let's see the IP address of the logout

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684138705/32b804d4-9d34-494b-88f4-c2ed44034b4e.png align="center")

we can see IP address 172.17.0.3

here we using the default bridge network both of them should have same subnet

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684287034/033aa2c0-cc41-4df0-81b3-be383f4c5ce8.png align="center")

let login to the container

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684739519/3281a965-f5af-454b-bfec-2c59f194fb9c.png align="center")

let me do ping 172.17.0.3 which is IP address of the logout container

lets me see if one container can access the another container which are on the same host

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701684981707/b5baae04-bf73-4a74-8534-c0ffcae895fe.png align="center")

we can do that because they are using the default box bridge networking which allows one container to talk to another container

if we want to list out all networks oh the host

`docker network ls`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701685034987/aab3c2af-61d0-446f-b95d-115c2ae13d97.png align="center")

we have learned how to connect from one container to another container also we have learned how to create networking isolation from one container to another container this login container can talk to the logout container

let's create a finance container and this container has be logically isolated from the login container

For i need create bridge network to this finance container

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701685563302/ec20dc4f-a86e-4d5b-9f1c-3e8343e0ff27.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701685623182/04ef4cc8-1415-4563-9584-41c2988fa7bf.png align="center")

let me assign secure-network to the finance container let me see if this login container talks to the finance container

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686018462/0dae8c70-2a20-48fa-afb3-e25a9c111aa8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686038969/051632a4-b6da-4acc-b7a4-8dd87b2762d4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686101725/1236f669-f0d9-4a51-99a9-2cb900918a0f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686376753/b8e73aa9-c120-4c8f-99ce-6cca402622b0.png align="center")

previous we saw for login container has a bridge but here we can see a secure-network

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686747590/6112334c-d7a3-4bc9-b854-1e20562e1946.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686783096/1df8fb90-e8f7-400a-aa08-7abbaf291100.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686926113/9dfcaa2b-5afb-4caf-99e5-6b6bcefc60aa.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701686962407/0609534b-47bb-4e00-a1da-dd2d837a3b34.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701687069978/b26f2399-326c-4217-a3d2-c85e5ddd44e7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701687167326/bea9db56-477d-4406-b88f-1c34d98f78e1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701687985112/aa296e01-08cf-4e7c-b5ae-fb85d6b3135d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701688067035/784c7be0-4a71-48d7-b102-f75448fa1d25.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701688044570/72a7f00e-10e9-4f56-a445-8133ec3616d9.png align="center")
