Home Explore Blog CI



docker

6th chunk of `content/manuals/engine/network/tutorials/overlay.md`
4d459979007caf7eb877a9540c43c6b576884d3e7b2d31500000000100000d8a
    > Notice the returned **NETWORK ID** -- you will see it again when you connect to it from `host2`.

3.  On `host1`, start an interactive (`-it`) container (`alpine1`) that connects to `test-net`:

    ```console
    $ docker run -it --name alpine1 --network test-net alpine
    / #
    ```

4.  On `host2`, list the available networks -- notice that `test-net` does not yet exist:

    ```console
    $ docker network ls
    NETWORK ID          NAME                DRIVER              SCOPE
    ec299350b504        bridge              bridge              local
    66e77d0d0e9a        docker_gwbridge     bridge              local
    9f6ae26ccb82        host                host                local
    omvdxqrda80z        ingress             overlay             swarm
    b65c952a4b2b        none                null                local
    ```

5.  On `host2`, start a detached (`-d`) and interactive (`-it`) container (`alpine2`) that connects to `test-net`:

    ```console
    $ docker run -dit --name alpine2 --network test-net alpine
    fb635f5ece59563e7b8b99556f816d24e6949a5f6a5b1fbd92ca244db17a4342
    ```

    > [!NOTE]
    >
    > Automatic DNS container discovery only works with unique container names.

6. On `host2`, verify that `test-net` was created (and has the same NETWORK ID as `test-net` on `host1`):

    ```console
    $ docker network ls
    NETWORK ID          NAME                DRIVER              SCOPE
    ...
    uqsof8phj3ak        test-net            overlay             swarm
    ```

7.  On `host1`, ping `alpine2` within the interactive terminal of `alpine1`:

    ```console
    / # ping -c 2 alpine2
    PING alpine2 (10.0.0.5): 56 data bytes
    64 bytes from 10.0.0.5: seq=0 ttl=64 time=0.600 ms
    64 bytes from 10.0.0.5: seq=1 ttl=64 time=0.555 ms

    --- alpine2 ping statistics ---
    2 packets transmitted, 2 packets received, 0% packet loss
    round-trip min/avg/max = 0.555/0.577/0.600 ms
    ```

    The two containers communicate with the overlay network connecting the two
    hosts. If you run another alpine container on `host2` that is _not detached_,
    you can ping `alpine1` from `host2` (and here we add the
    [remove option](/reference/cli/docker/container/run/#rm) for automatic container cleanup):

    ```sh
    $ docker run -it --rm --name alpine3 --network test-net alpine
    / # ping -c 2 alpine1
    / # exit
    ```

8.  On `host1`, close the `alpine1` session (which also stops the container):

    ```console
    / # exit
    ```

9.  Clean up your containers and networks:

    You must stop and remove the containers on each host independently because
    Docker daemons operate independently and these are standalone containers.
    You only have to remove the network on `host1` because when you stop
    `alpine2` on `host2`, `test-net` disappears.

    a.  On `host2`, stop `alpine2`, check that `test-net` was removed, then remove `alpine2`:

    ```console
    $ docker container stop alpine2
    $ docker network ls
    $ docker container rm alpine2
    ```

    a.  On `host1`, remove `alpine1` and `test-net`:

    ```console
    $ docker container rm alpine1
    $ docker network rm test-net
    ```

## Other networking tutorials

- [Host networking tutorial](/manuals/engine/network/tutorials/host.md)
- [Standalone networking tutorial](/manuals/engine/network/tutorials/standalone.md)
- [Macvlan networking tutorial](/manuals/engine/network/tutorials/macvlan.md)

Title: Connecting Containers Across Hosts with an Overlay Network and Cleanup
Summary
This section details connecting containers on different hosts (`host1` and `host2`) via the overlay network `test-net`. It shows how to start a detached container (`alpine2`) on `host2` connected to `test-net`, verifies the network's creation and shared NETWORK ID on both hosts, and demonstrates inter-container communication using ping from `alpine1` (on `host1`) to `alpine2` (on `host2`). It also shows running a temporary alpine container on `host2` that is not detached to ping alpine1. Finally, it outlines the cleanup process, including stopping and removing containers on both hosts independently and removing the network.