{"id":48457,"date":"2025-02-08T08:42:47","date_gmt":"2025-02-08T08:42:47","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48457"},"modified":"2025-07-12T05:38:40","modified_gmt":"2025-07-12T05:38:40","slug":"docker-network","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/docker-network\/","title":{"rendered":"Docker commands Guide &#8211; docker network with examples"},"content":{"rendered":"\n<p>Here\u2019s a <strong>complete tutorial<\/strong> on <code>docker network<\/code>, covering <strong>what it does<\/strong>, <strong>examples<\/strong>, and <strong>use cases<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is <code>docker network<\/code>?<\/strong><\/h2>\n\n\n\n<p><code>docker network<\/code> is a command used to <strong>manage Docker networks<\/strong>, enabling containers to <strong>communicate with each other<\/strong> and control how they connect internally and externally. Docker provides several types of networks (bridge, overlay, host, and macvlan), each suited for different use cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Features:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Create, inspect, connect, and disconnect networks<\/strong>.<\/li>\n\n\n\n<li>Control how containers communicate within and across multiple hosts.<\/li>\n\n\n\n<li>Supports <strong>custom and isolated networks<\/strong> for security and scalability.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Docker Network Types<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Bridge Network (default)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The most common type for standalone containers.<\/li>\n\n\n\n<li>Containers on the same bridge network can communicate with each other.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Host Network<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Removes network isolation; the container uses the <strong>host\u2019s network stack<\/strong> directly.<\/li>\n\n\n\n<li>Suitable for high-performance network communication.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Overlay Network<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Used for <strong>multi-host networking<\/strong> in Docker Swarm.<\/li>\n\n\n\n<li>Allows containers on different hosts to communicate securely.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Macvlan Network<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Assigns a <strong>MAC address<\/strong> to containers, making them appear as physical devices on the network.<\/li>\n\n\n\n<li>Useful for integrating Docker with existing physical networks.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span> <span class=\"hljs-selector-tag\">network<\/span> <span class=\"hljs-selector-attr\">&#91;COMMAND]<\/span> <span class=\"hljs-selector-attr\">&#91;OPTIONS]<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Common <code>docker network<\/code> Commands:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>create<\/code><\/strong>: Create a new network.<\/li>\n\n\n\n<li><strong><code>ls<\/code><\/strong>: List all networks.<\/li>\n\n\n\n<li><strong><code>inspect<\/code><\/strong>: Show detailed information about a network.<\/li>\n\n\n\n<li><strong><code>connect<\/code><\/strong>: Connect a container to a network.<\/li>\n\n\n\n<li><strong><code>disconnect<\/code><\/strong>: Disconnect a container from a network.<\/li>\n\n\n\n<li><strong><code>rm<\/code><\/strong>: Remove a network.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of <code>docker network<\/code> Commands<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. List All Docker Networks<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network ls\n<\/code><\/span><\/pre>\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">NETWORK ID     NAME      DRIVER    SCOPE\n<span class=\"hljs-number\">6e8<\/span>d77eabc12   bridge    bridge    local\n<span class=\"hljs-number\">5<\/span>aeb2c1234ab   host      host      local\n<span class=\"hljs-number\">9<\/span>a8d12345def   none      <span class=\"hljs-literal\">null<\/span>      local\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Create a New Bridge Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network create my_custom_network\n<\/code><\/span><\/pre>\n\n\n<p>This creates a custom bridge network called <code>my_custom_network<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Create a Network with a Specific Subnet<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network create \\\n  --subnet=192.168.1.0\/24 \\\n  custom_subnet_network\n<\/code><\/span><\/pre>\n\n\n<p>This creates a network with the subnet <code>192.168.1.0\/24<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Inspect a Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network inspect my_custom_network\n<\/code><\/span><\/pre>\n\n\n<p>This provides detailed information about the network, including connected containers and configuration.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Connect a Container to a Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network connect my_custom_network my_container\n<\/code><\/span><\/pre>\n\n\n<p>This connects <code>my_container<\/code> to <code>my_custom_network<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Disconnect a Container from a Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network disconnect my_custom_network my_container\n<\/code><\/span><\/pre>\n\n\n<p>This disconnects <code>my_container<\/code> from <code>my_custom_network<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Create an Overlay Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network create \\\n  --driver overlay \\\n  my_overlay_network\n<\/code><\/span><\/pre>\n\n\n<p>This creates an overlay network for multi-host communication (requires Docker Swarm).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Create a Macvlan Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">docker network create \\\n  -d macvlan \\\n  --subnet=<span class=\"hljs-number\">192.168<\/span><span class=\"hljs-number\">.1<\/span><span class=\"hljs-number\">.0<\/span>\/<span class=\"hljs-number\">24<\/span> \\\n  --gateway=<span class=\"hljs-number\">192.168<\/span><span class=\"hljs-number\">.1<\/span><span class=\"hljs-number\">.1<\/span> \\\n  -o <span class=\"hljs-keyword\">parent<\/span>=eth0 \\\n  my_macvlan_network\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This creates a macvlan network where containers get their own IP addresses in the <code>192.168.1.0\/24<\/code> subnet.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Remove a Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network rm my_custom_network\n<\/code><\/span><\/pre>\n\n\n<p>This removes the <code>my_custom_network<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. Remove All Unused Networks<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network prune\n<\/code><\/span><\/pre>\n\n\n<p>This removes all unused networks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11. Use a Custom Network When Running a Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker run --rm --network=my_custom_network nginx\n<\/code><\/span><\/pre>\n\n\n<p>This runs an <code>nginx<\/code> container attached to <code>my_custom_network<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12. List Connected Containers in a Network<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker network inspect my_custom_network | jq <span class=\"hljs-string\">'.&#91;0].Containers'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This lists all containers connected to <code>my_custom_network<\/code> (requires <code>jq<\/code> for JSON parsing).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use Cases for <code>docker network<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Container Communication in Isolated Environments<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use custom bridge networks to <strong>group related containers<\/strong> for secure communication.<\/li>\n\n\n\n<li>Example: Create a network for a <strong>web app and database<\/strong>, ensuring they can communicate internally.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. High-Performance Networking with Host Mode<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <strong>host network<\/strong> for <strong>low-latency<\/strong> and high-performance applications like monitoring tools or network proxies.<\/li>\n\n\n\n<li>Example: Run a <strong>Prometheus<\/strong> container in host mode to reduce network overhead.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Multi-Host Networking (Docker Swarm)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use overlay networks for <strong>distributed applications<\/strong> across multiple Docker hosts.<\/li>\n\n\n\n<li>Example: Deploy a <strong>microservices application<\/strong> across a Docker Swarm cluster.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Integrating Docker with Physical Networks (Macvlan)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use macvlan networks to <strong>assign unique IP addresses<\/strong> to containers on your physical network.<\/li>\n\n\n\n<li>Example: Deploy containers as part of your corporate network infrastructure.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Secure and Private Networks<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create isolated networks for <strong>sensitive applications<\/strong> to prevent external access.<\/li>\n\n\n\n<li>Example: Run an <strong>internal API service<\/strong> on a private network only accessible to specific containers.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Simplified CI\/CD Networking<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create temporary networks in CI pipelines for testing container communication.<\/li>\n\n\n\n<li>Example: Set up an <strong>isolated network<\/strong> for integration tests in a CI\/CD job.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>List of Common <code>docker network<\/code> Commands<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Command<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>docker network ls<\/code><\/td><td>List all networks<\/td><\/tr><tr><td><code>docker network create my_network<\/code><\/td><td>Create a new bridge network<\/td><\/tr><tr><td><code>docker network create --driver overlay my_overlay<\/code><\/td><td>Create an overlay network for multi-host setups<\/td><\/tr><tr><td><code>docker network inspect my_network<\/code><\/td><td>Inspect a network for detailed information<\/td><\/tr><tr><td><code>docker network connect my_network my_container<\/code><\/td><td>Connect a container to a network<\/td><\/tr><tr><td><code>docker network disconnect my_network my_container<\/code><\/td><td>Disconnect a container from a network<\/td><\/tr><tr><td><code>docker network rm my_network<\/code><\/td><td>Remove a network<\/td><\/tr><tr><td><code>docker network prune<\/code><\/td><td>Remove all unused networks<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Using Docker Networks:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use custom networks<\/strong> for container communication to avoid exposing services to the default bridge network.<\/li>\n\n\n\n<li><strong>Isolate sensitive services<\/strong> on private networks for security.<\/li>\n\n\n\n<li><strong>Monitor and prune unused networks<\/strong> to avoid network clutter.<\/li>\n\n\n\n<li><strong>Use overlay networks<\/strong> for multi-host Docker Swarm deployments.<\/li>\n\n\n\n<li><strong>Plan subnets<\/strong> carefully to avoid conflicts with your physical network.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors and Solutions<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8220;Network not found&#8221;<\/strong><br>\u2192 Ensure the network exists by checking with <code>docker network ls<\/code>.<\/li>\n\n\n\n<li><strong>&#8220;Address already in use&#8221;<\/strong><br>\u2192 Choose a subnet that does not conflict with other networks on your host.<\/li>\n\n\n\n<li><strong>&#8220;Cannot connect to container&#8221;<\/strong><br>\u2192 Verify that the container is running and the network is properly configured.<\/li>\n\n\n\n<li><strong>&#8220;Permission denied&#8221;<\/strong><br>\u2192 Run the command with <code>sudo<\/code> or check user permissions.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining <code>docker network<\/code> with Other Commands<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Run a Web App with a Linked Database<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network create web_net\ndocker run -d --network=web_net --name=web_app my_web_app\ndocker run -d --network=web_net --name=db my_database\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Inspect and Check for IP Conflicts<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network inspect my_network\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Automate Network Cleanup<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker network prune -f\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a complete tutorial on docker network, covering what it does, examples, and use cases. What is docker network? docker network is a command used to manage Docker networks, enabling&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-48457","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48457"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48457\/revisions"}],"predecessor-version":[{"id":48459,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48457\/revisions\/48459"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}