Home Explore Blog CI



docker

4th chunk of `content/manuals/engine/network/tutorials/standalone.md`
7ccaf88b371beb714d557941761cb7de91c24d0025f81d110000000100000fb2
    If you wish, attach to `alpine2` and repeat steps 4, 5, and 6 there,
    substituting `alpine1` for `alpine2`.

8.  Stop and remove both containers.

    ```console
    $ docker container stop alpine1 alpine2
    $ docker container rm alpine1 alpine2
    ```

Remember, the default `bridge` network is not recommended for production. To
learn about user-defined bridge networks, continue to the
[next tutorial](#use-user-defined-bridge-networks).

## Use user-defined bridge networks

In this example, we again start two `alpine` containers, but attach them to a
user-defined network called `alpine-net` which we have already created. These
containers are not connected to the default `bridge` network at all. We then
start a third `alpine` container which is connected to the `bridge` network but
not connected to `alpine-net`, and a fourth `alpine` container which is
connected to both networks.

1.  Create the `alpine-net` network. You do not need the `--driver bridge` flag
    since it's the default, but this example shows how to specify it.

    ```console
    $ docker network create --driver bridge alpine-net
    ```

2.  List Docker's networks:

    ```console
    $ docker network ls

    NETWORK ID          NAME                DRIVER              SCOPE
    e9261a8c9a19        alpine-net          bridge              local
    17e324f45964        bridge              bridge              local
    6ed54d316334        host                host                local
    7092879f2cc8        none                null                local
    ```

    Inspect the `alpine-net` network. This shows you its IP address and the fact
    that no containers are connected to it:

    ```console
    $ docker network inspect alpine-net

    [
        {
            "Name": "alpine-net",
            "Id": "e9261a8c9a19eabf2bf1488bf5f208b99b1608f330cff585c273d39481c9b0ec",
            "Created": "2017-09-25T21:38:12.620046142Z",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "172.18.0.0/16",
                        "Gateway": "172.18.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Containers": {},
            "Options": {},
            "Labels": {}
        }
    ]
    ```

    Notice that this network's gateway is `172.18.0.1`, as opposed to the
    default bridge network, whose gateway is `172.17.0.1`. The exact IP address
    may be different on your system.

3.  Create your four containers. Notice the `--network` flags. You can only
    connect to one network during the `docker run` command, so you need to use
    `docker network connect` afterward to connect `alpine4` to the `bridge`
    network as well.

    ```console
    $ docker run -dit --name alpine1 --network alpine-net alpine ash

    $ docker run -dit --name alpine2 --network alpine-net alpine ash

    $ docker run -dit --name alpine3 alpine ash

    $ docker run -dit --name alpine4 --network alpine-net alpine ash

    $ docker network connect bridge alpine4
    ```

    Verify that all containers are running:

    ```console
    $ docker container ls

    CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
    156849ccd902        alpine              "ash"               41 seconds ago       Up 41 seconds                           alpine4
    fa1340b8d83e        alpine              "ash"               51 seconds ago       Up 51 seconds                           alpine3
    a535d969081e        alpine              "ash"               About a minute ago   Up About a minute                       alpine2
    0a02c449a6e9        alpine              "ash"               About a minute ago   Up About a minute                       alpine1

Title: Moving to User-Defined Bridge Networks: Creating and Connecting Containers
Summary
This section transitions to using user-defined bridge networks, highlighting their advantages over the default bridge network. It starts by creating a new network named 'alpine-net' using the 'docker network create' command. The section then shows how to list and inspect the newly created network to view its configuration, including the IP address range. Next, four Alpine containers are created: 'alpine1' and 'alpine2' are connected to 'alpine-net', 'alpine3' is connected to the default 'bridge' network, and 'alpine4' is connected to both. The 'docker network connect' command is used to connect 'alpine4' to the bridge network after the initial 'docker run' command. Finally, the section verifies that all four containers are running using 'docker container ls'.