DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spinnaker Meets Minikube: Part 1
  • Getting Started With Windows Containers
  • Kubernetes Installation in RedHat/CentOS
  • How To Use the Node Docker Official Image

Trending

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Ensuring Configuration Consistency Across Global Data Centers
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Unlocking the Benefits of a Private API in AWS API Gateway
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Understanding Docker Networking

Understanding Docker Networking

Sharing is a necessity when it comes to using Docker, both between containers themselves and with third-party applications.

By 
Manjula Jayawardana user avatar
Manjula Jayawardana
·
Jul. 11, 19 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

Image title


When we talk about Docker, we say that containers are isolated. How do we communicate with our containers, or other applications like MySQL database? It is not useful if we can't access it.

Docker has a network concept. It has several network drivers to work with. Depending on how we want our container to behave, we can select our network. This helps us to connect container with a container or container with host.

Image title


Network Commands Summary

  •  docker network ls  - list available networks

  •  docker network create  - create a network

  •  docker network rm  - remove a network

  •  docker network inspect  - inspect a network

  •  docker network connect  - connect container to a network

  •  docker network disconnect  - disconnect container from a network

Docker Network Drivers

 bridge is the default network. When the Docker daemon service starts, it configures a virtual bridge named docker0 . When we don't specify the network, this is the one Docker uses. Docker creates a private network inside the host which allows containers to communicate with each other. host tells Docker to use host computers network directly.

Network Commands

Just like other Docker commands, the Docker network command have the same pattern.

List Available Network Commands

docker network help

 

Inspecting a Network

Use the inspect command to inspect a Docker:

docker network inspect bridge

Create a Network

We can create our own network using the create command.

docker network create mynetwork

Docker prints the ID of the created network. Use the inspect command to see properties. You will see that it has used bridge as the driver since we didn't specify a driver to be used. We can specify a driver using the -d  option.

docker network inspect mynetwork
docker network create -d bridge mynetwork2

Remove a Network

We can use rm command to remove a network.

docker network rm mynetwork

Connect to A Network

When we create a Docker container, by default it is connected to bridge network. We can use the --net   option to connect with another network when we run the container.

docker container run -it --net=mynetwork nginx

Connect with The Host

Now we need to use our containers from the host. There is no meaning of isolating a container if we can't access it when needed (although there are isolated containers in some cases).

We can get the exposed port of an image by inspecting it. Issue the inspect command and see the line for "ExposedPorts."

docker image inspect nginx 
"ExposedPorts": {"80/tcp": {}}

This means Nginx exposes port 80 to access from the host or other containers.

We can use -p   or --publish  option to bind this port to the host's port when running an image.

 hostport:containerport 
 docker container run -it -p 81:80 nginx

This means we are binding the port 81 with Docker container's exposed port 80.

If we want to connect with a network,

docker container run -it --net=mynetwork -p 81:80 nginx

When we publish the port, we can access it using localhost like localhost:81

Image title

Or we can use host network directly.

docker container run -it --net=host nginx

Get Container Ports

We can get the containers port using port command

 docker port cotainer_name/id

Let's run Nginx again and publish to 81.

docker container run --name my-nginx -p 81:80 nginx

Now when we inspect the container (under NetworkSettings), we can see that it has attached to host's port 81.

docker container inspect my-nginx 
 "Ports": {"80/tcp": [{"HostIp": "0.0.0.0","HostPort": "81"}]},

Conclusion

Docker networking is a very useful thing to learn. When we are building microservices and deploy using Docker containers, we need to connect them into the same network. It will be much easier to work with these kinds of scenarios when we have a basic understanding.

Docker (software) Network Host (Unix) Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Spinnaker Meets Minikube: Part 1
  • Getting Started With Windows Containers
  • Kubernetes Installation in RedHat/CentOS
  • How To Use the Node Docker Official Image

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!