Docker Config Reference (docker network)
We go over using the docker network command
docker network ls
This command lists all available Docker networks on the host system, displaying details such as network ID, name, driver type, and scope. It provides an overview of the networking environment, including default networks like bridge, host, and none, which is essential for initial assessment and management of container connectivity.
docker network create mynet
This command creates a new user-defined bridge network named "mynet" using the default bridge driver. It establishes an isolated network segment for containers, enabling better organization and security compared to the default bridge network by supporting automatic DNS resolution between containers.
docker network rm mynet
This command removes the specified network "mynet" from the Docker host, provided no containers are currently attached to it. It is used for cleanup operations to free resources and maintain a tidy networking configuration.
docker network inspect bridge
This command inspects the default "bridge" network, outputting detailed JSON-formatted information including subnet, gateway, attached containers, and configuration options. It aids in diagnosing network-related issues or understanding default behaviors.
docker network connect mynet mycontainer
This command connects an existing container "mycontainer" to the network "mynet", allowing it to communicate with other containers on that network. It supports multi-network attachments for flexible routing setups.
docker network disconnect mynet mycontainer
This command disconnects the container "mycontainer" from the network "mynet", isolating it from other containers on that network while preserving its other connections. It is useful for dynamic reconfiguration without restarting containers.
docker network prune
This command removes all unused networks that are not referenced by any containers, helping to reclaim storage and simplify the network list. It prompts for confirmation unless the --force flag is used.
docker network create --driver bridge customnet
This command creates a network "customnet" explicitly specifying the bridge driver, which is the default but can be overridden for clarity or in scripts. It ensures compatibility with local host networking requirements.
docker network create --subnet 192.168.0.0/24 subnetnet
This command creates a network "subnetnet" with a custom subnet CIDR, allowing control over IP address allocation to avoid conflicts with host or external networks. It customizes the address pool for specific deployment needs.
docker network ls --quiet
Using the --quiet flag, this command lists only the network IDs without additional details, providing a concise output suitable for scripting or piping into other commands for automation.
docker network ls --format "{{.Name}}"
This command lists network names using a custom Go template format, enabling selective output for integration into tools or dashboards where only specific attributes are required.
docker network inspect --format '{{json .IPAM.Config}}' bridge
This inspects the "bridge" network and formats the output to display only the IPAM (IP Address Management) configuration in JSON, useful for extracting subnet or gateway details programmatically.
docker network create --internal internalnet
This command creates an "internalnet" network that disables external connectivity, ensuring containers attached to it can only communicate internally for enhanced security in isolated environments.
docker network create --attachable attachnet
This command creates an "attachnet" network that allows manual attachment of containers in Docker Swarm mode, extending flexibility for service orchestration across clusters.
docker network ls --filter driver=bridge
This command lists networks filtered by the bridge driver, narrowing results to specific types for targeted management or auditing in multi-driver setups.
docker network prune --force
With the --force flag, this command prunes unused networks without prompting, automating cleanup in non-interactive scripts or CI/CD pipelines.
docker network create --gateway 192.168.1.1 --subnet 192.168.1.0/24 gatednet
This creates "gatednet" with a custom subnet and gateway IP, providing precise control over routing for scenarios requiring specific network topologies.
docker network create --ip-range 192.168.1.128/25 rangednet
This command creates "rangednet" with a restricted IP allocation range within the subnet, optimizing address usage for large-scale deployments.
docker network create --label com.example.type=custom labelednet
This adds a custom label to the "labelednet" network during creation, facilitating organization, filtering, or metadata association for management tools.
docker network inspect -v bridge
Using the -v (verbose) flag, this command provides extended details about the "bridge" network, including container endpoints, for in-depth troubleshooting.
docker network ls --no-trunc
This command lists networks without truncating long IDs or names, ensuring full visibility for precise identification in dense outputs.
docker network connect --ip 192.168.1.10 mynet mycontainer
This connects "mycontainer" to "mynet" with a static IP assignment, overriding dynamic allocation for predictable addressing in configurations like load balancers.
docker network connect --alias db mynet mydbcontainer
This assigns an alias "db" to "mydbcontainer" on "mynet", enabling name-based resolution for simplified service discovery within the network.
docker network disconnect --force mynet mycontainer
With --force, this disconnects "mycontainer" from "mynet" even if the container is running, useful for urgent reconfigurations without downtime.
docker network create --ipv6 ipv6net
This creates "ipv6net" with IPv6 support enabled, allocating a ULA prefix for dual-stack networking in modern environments.
docker network create --opt encrypted encryptednet
This command creates "encryptednet" with MACsec encryption enabled for bridge traffic, enhancing security for sensitive data transmission between containers.
docker network inspect --format '{{.Id}}' mynet
This extracts and displays only the network ID of "mynet" using a format template, ideal for scripting where unique identifiers are needed.
docker network ls --format 'table {{.ID}}\t{{.Name}}\t{{.Driver}}'
This lists networks in a tabular format with selected columns (ID, Name, Driver), improving readability for human review or reports.
docker network prune --filter until=24h
This prunes networks unused for the last 24 hours, applying a time-based filter for selective cleanup in long-running systems.
docker network create --driver overlay --scope swarm swarmnet
This creates "swarmnet" using the overlay driver in swarm scope, supporting multi-host networking for distributed container orchestration in Docker Swarm clusters.