Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use docker Network

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use docker network". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to use docker network".

Data volume

Previously we created a container and created a file in the container. Then resubmit the container as an image, so that we will include this file when we use the image to create a new container, but this solution is not recommended. Docker provides both data volume and data volume container for data storage.

Data volume

A data volume is very similar to a linux mount directory. In fact, a data volume is a special directory that can be used by a container. There are many ways to create and hang data volumes on the Internet, but they are all the same. Here we will use the-v method to create and hang data volumes.

Sudo docker run-ti-- name volume1-v / myDir ubuntu:16.04 bash

For example, using the above statement, we can create a container for volume1 and create a data volume for myDir to hang on the container.

We can see that there is a myDir directory in the container, which is the data volume we mounted.

Another way is to map a directory on the host to the data source of the container. Sudo docker run-ti-- name volume2-v / home/test/:/myShare ubuntu:16.04 bash like this, the / home/test directory on the host is mapped to the container's myShare folder. PS: by default, data volumes are hung with read and write permissions. You can use: ro to specify read-only sudo docker run-ti-- name volume2-v / home/test/:/myShare:ro ubuntu:16.04 bash

You can use the docker volume command to manage data volumes.

Data volume container

The data volume container is actually a normal container, but this container is specifically used as a data volume for other containers to mount.

First create a data volume container (as with the above command to create a container with data volumes) sudo docker run-ti-d-v / dataVolume-- name v0 ubuntu:16.04 above create a container v0, containing a data volume, directory is / dataVolume

Then create a container and mount the container sudo docker run-ti-- volumes-from v0-- name v1 ubuntu:16.04 bash so that you can know the data volume mount directory / dataVolume of v0 in the new container just now.

PS: the data volume container does not have to be running when it is mounted

Docker network

By default, the ubuntu container does not have basic network aids installed with ping vim apache2 openssh tools through the following code

Apt-get updateapt-get install vimapt-get install net-toolsapt install iputils-pingapt install apache2apt install apache2-utilsapt install openssh-serverapt install openssh-client

Then modify vim / etc/ssh/sshd_config to change the content of "PermitRootLogin" to yes and save it. Then use the command passwd to set the root password

Then repackage the container into an image sudo docker commit-m "commit message" {containerName} {imageName}: {tag}

When we create a container from this image and open various services, we encounter a problem: how to access the apache server of the container content from the public network? So the most important thing is port mapping, which needs to be established with the-p parameter when creating a container using run.

-p hostPort:containerPort mapping specified port on all IP addresses to container internal-p ip:hostPort:containerPort mapping specified port on specified IP address to container internal-p ip::containerPort mapping any port on specified IP address to container internal

For example, our server address is xxx, and the image (above) is net:v1.0 sudo docker run-ti-- name web-p 80:80 net:v1.0 bash.

Then open the apache service in the container.

Finally, accessing xxx:80 will be mapped to port 80 of the web container, which actually accesses the container's apache server.

PS: Docker uses TCP protocol for network communication between containers by default. If you need UDP, you can specify sudo docker run-ti-name web-p 80:80/udp net:v1.0 bash in the following format

Container interconnection

Container interconnection allows containers to interact with each other without port mapping. The container interconnection creates a secure tunnel between the source container and the receiving container, and the receiving container can see the information of the source container. For example, two containers, one container is db, and one container is app

First, create a source container:

Sudo docker run-ti-- name db net:v1.0 bash then runs another container, connecting the first container with the-- link parameter:

Sudo docker run-ti-- name app-- link db:data net:v1.0 bash here-- link db:data means to link the container named db to the alias data, and then you can communicate with the first container with the name sender in the second container, such as ping data: ping data

In fact, the system adds this alias to the container's / etc/hosts:

Docker component local area network

I don't need pass for the time being.

Thank you for your reading, the above is the content of "how to use docker network", after the study of this article, I believe you have a deeper understanding of how to use docker network, the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report