> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/docker/networking.md).

# Networking

#### **What is Docker Networking?**

Docker networking allows multiple containers to communicate with each other or with the outside world (e.g., the internet). Think of it as a virtual network where containers can "talk" to one another.

***

#### **Types of Docker Networks**

Docker provides different network drivers depending on your needs:

**1️⃣ Bridge (Default Network)**

* Containers share a private network on the same Docker host.
* They can talk to each other via their **container name** or **IP address**.
* Used for most standalone containers.

**Example:**

```bash
docker network create mynetwork
docker run -d --name app --network mynetwork nginx
docker run -d --name db --network mynetwork mysql
```

* Both `app` and `db` can communicate on the `mynetwork`.

***

**2️⃣ Host**

* The container shares the **host’s network stack**.
* No isolation — the container uses the host’s IP address and ports.
* Faster, but not ideal for security.

**Example:**

```bash
docker run --network host nginx
```

Use this when performance is critical (e.g., running a web server without any network overhead).

***

**3️⃣ None**

* The container has **no network access** (fully isolated).
* Useful for running tasks that don’t require networking.

**Example:**

```bash
docker run --network none busybox
```

***

**4️⃣ Overlay**

* Connects containers **across multiple Docker hosts**.
* Used in Docker Swarm or Kubernetes for distributed applications.

**Example:**

```bash
docker network create -d overlay myoverlay
```

***

#### **How to Connect Containers?**

1. **Using a Custom Network**

   * Containers in the same custom network can use **container names** to talk to each other.

   Example:

   ```bash
   bashKopierenBearbeitendocker network create mynetwork
   docker run -d --name app --network mynetwork nginx
   docker run -d --name db --network mynetwork mysql
   ```

   * Inside the `app` container, you can use:

     ```bash
     ping db
     ```

     This works because both containers are on the same network.

***

#### **How to Check Network Details?**

1. **List all networks:**

   ```bash
   docker network ls
   ```
2. **Inspect a specific network:**

   ```bash
   docker network inspect mynetwork
   ```

   * This shows details like connected containers, IP addresses, and network configurations.

***

#### **Advanced Networking Concepts**

* **Subnets and IP Ranges**: You can specify a subnet for your network during creation.

  ```bash
  docker network create --subnet=192.168.1.0/24 mynetwork
  ```
* **Aliases**: Assign alternative names for containers within the network.

  ```bash
  docker network connect --alias db_alias mynetwork db
  ```

***

#### **When to Use Different Networks?**

* **Bridge**: Default for local containers on the same host.
* **Host**: For maximum performance or when you need direct access to the host's network.
* **None**: To isolate containers completely.
* **Overlay**: For multi-host applications (e.g., in a Docker Swarm or Kubernetes setup).

***

#### **In Summary**

Docker networking allows you to connect containers easily:

* Create a **custom network** to link containers.
* Use different network types depending on your use case (local, distributed, isolated).
* **Inspect networks** to see connections and troubleshoot issues.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/docker/networking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
