mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
translated
This commit is contained in:
parent
b751a9e589
commit
e9cf8f9b07
@ -1,108 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
Basic Networking Commands with Docker Containers
|
||||
================================================================================
|
||||
Hi everybody, today we'll learn some basic networking with Docker Containers. Docker is an Open Source project that provides an open platform to pack, ship and run any application as a lightweight container. It has no boundaries of Language support, Frameworks or packaging system and can be run anywhere, anytime from a small home computers to high-end servers. It makes them great building blocks for deploying and scaling web apps, databases, and back-end services without depending on a particular stack or provider. Docker is meant for networking as it is gradually used in data-centers, ISPs and much more networked servers.
|
||||
|
||||
So, here are some basic networking commands that you can use on managing Docker Containers.
|
||||
|
||||
### 1. Finding the Docker Interface ###
|
||||
|
||||
Docker by default creates a bridge interface named docker0 which basically connects with the outside world. The docker containers running is directly connected with the bridge interface docker0. By default, docker assigns ip address 172.17.42.1/16 to the bridge interface docker0 which acts as a subnet for all the ip addresses of the running containers. It is pretty easy to get the Docker Interface's IP Address. To find out our docker0 bridge interface and the docker containers connected with the bridge we can simply run ip a command inside a terminal or a shell where Docker is installed.
|
||||
|
||||
# ip a
|
||||
|
||||
![Docker Interface](http://blog.linoxide.com/wp-content/uploads/2015/05/docker-interface.png)
|
||||
|
||||
### 2. Getting the Docker Container's IP Address ###
|
||||
|
||||
As we read above, docker creates a bridge interface named docker0 in the host machine. As we create a new docker container, it is automatically assigned with a new dynamic IP Address by default which is followed within the subnet range of the interface. So, to check the IP Address of the running Docker container, we'll need to get into a running container and check out the ip address as shown below. First, we'll run a new container and get into the container. If you have got a container running already, you may skip the first command below.
|
||||
|
||||
# docker run -it ubuntu
|
||||
|
||||
Now, we'll be able to run ip a in order to get the ip address of the running container.
|
||||
|
||||
# ip a
|
||||
|
||||
![Docker Container IP](http://blog.linoxide.com/wp-content/uploads/2015/05/docker-container-ip.png)
|
||||
|
||||
### 3. Mapping the Expose port ###
|
||||
|
||||
To map the exposed port configured in the Dockerfile for the container to the high port, we'll simply need to follow the docker command with -P flag. This will open the random port of the Docker Container to the port defined by the Dockerfile. So, here's an example of using the flag -P to open/expose the defined port.
|
||||
|
||||
# docker run -itd -P httpd
|
||||
|
||||
![Mapping Expose Port](http://blog.linoxide.com/wp-content/uploads/2015/05/mapping-expose-port.png)
|
||||
|
||||
The above command will map container's port to port 80 as its defined in the Dockerfile of httpd. We can check the exposed port by viewing the running container using the following command.
|
||||
|
||||
# docker ps
|
||||
|
||||
And can use curl to check by running the below command.
|
||||
|
||||
# curl http://localhost:49153
|
||||
|
||||
![Curl Exposed Port](http://blog.linoxide.com/wp-content/uploads/2015/05/curl-exposed-port-e1431034586219.png)
|
||||
|
||||
### 4. Mapping to the Specific Port ###
|
||||
|
||||
We can also map the expose port or docker container port to the port we specify or define. In order to map the container to a specific port of our choice, we'll need to define the port of our desire with the flag -p . Here's an example on how we can do that.
|
||||
|
||||
# docker run -itd -p 8080:80 httpd
|
||||
|
||||
The above command will map the port 8080 to port 80. We can check it by running curl to that port.
|
||||
|
||||
# curl http://localhost:8080
|
||||
|
||||
![Mapping Specific Port](Curl Exposed Port)
|
||||
|
||||
### 5. Creating own Bridge ###
|
||||
|
||||
To assign a custom IP Address to the containers, we'll need to create a new bridge interface named bro in this tutorial. To assign a desired IP Address, we'll need to run the following command in the host machine running docker.
|
||||
|
||||
# stop docker.io
|
||||
# ip link add br0 type bridge
|
||||
# ip addr add 172.30.1.1/20 dev br0
|
||||
# ip link set br0 up
|
||||
# docker -d -b br0
|
||||
|
||||
![Creating Bridge Interface](http://blog.linoxide.com/wp-content/uploads/2015/05/creating-bridge-interface.png)
|
||||
|
||||
After creating the docker bridge, we'll want to let the Docker Daemon know about it.
|
||||
|
||||
# echo 'DOCKER_OPTS="-b=br0"' >> /etc/default/docker
|
||||
# service docker.io start
|
||||
|
||||
![Adding Interface to Docker](http://blog.linoxide.com/wp-content/uploads/2015/05/adding-interface-to-docker.png)
|
||||
|
||||
Here, the bridged interface will range the docker containers with the ip address followed by bridge ip subnet .
|
||||
|
||||
### 6. Linking one Container to another ###
|
||||
|
||||
We can connect one container with the another container with Docker. We can run different apps on different containers and connect or link with each other. Links allow containers to connect with each other and securely transfer information about one container to another container. To do so, we'll use the --link flag. First, we'll use flag --name so that it will be easy to denote training/postgres image.
|
||||
|
||||
# docker run -d --name db training/postgres
|
||||
|
||||
![Running db Container](http://blog.linoxide.com/wp-content/uploads/2015/05/running-db-container.png)
|
||||
|
||||
After done, we'll gonna link training/webapp with the container db that we create earlier forming a new container named web.
|
||||
|
||||
# docker run -d -P --name web --link db:db training/webapp python app.py
|
||||
|
||||
![linking two containers](http://blog.linoxide.com/wp-content/uploads/2015/05/linking-two-containers.png)
|
||||
|
||||
### Conclusion ###
|
||||
|
||||
Networking with Docker is fun and amazing cause there are many stuffs we can do with Docker Containers. These were some easy and basic networking commands that we can play with Docker. Networking with docker is really advanced. We can do many stuffs with it. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-how-to/networking-commands-docker-containers/
|
||||
|
||||
作者:[Arun Pyasi][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://linoxide.com/author/arunp/
|
@ -0,0 +1,106 @@
|
||||
关于Docker容器的基础网络命令
|
||||
================================================================================
|
||||
各位好,今天我们将学习一些Docker容器的基础命令。Docker是一个提供了开放平台来打包、发布并以一个轻量级容器运行任意程序的开放平台。它没有语言支持、框架或者打包系统的限制,可在任何时间、任何地方在小到家用电脑大到高端服务器上运行。这使得在部署和扩展网络应用、数据库和终端服务时不依赖于特定的栈或者提供商。Docker注定是用于网络的如它正应用于数据中心、ISP和越来越多的网络服务。
|
||||
|
||||
因此,这里有一些你在管理Docker容器的时候会用到的一些命令。
|
||||
|
||||
### 1. 找到Docker接口 ###
|
||||
|
||||
Docker默认会创建一个名为docker0的网桥接口来连接外部的世界。docker容器运行时直接连接到网桥接口docker0。默认上,docker会分配172.17.42.1/16给docker0,它是所有运行容器ip地址的子网。得到Docker接口的ip地址非常简单。要找出docker0网桥接口和连接到网桥上的docker容器,我们可以在终端或者安装了docker的shell中运行ip命令。
|
||||
|
||||
# ip a
|
||||
|
||||
![Docker Interface](http://blog.linoxide.com/wp-content/uploads/2015/05/docker-interface.png)
|
||||
|
||||
### 2. 得到Docker容器的ip地址 ###
|
||||
|
||||
如我们上面读到的,docker在主机中创建了一个叫docker0的网桥接口。如我们创建一个心的docker容器一样,它自动被默认分配了一个在子网范围内的ip地址。因此,要检测运行中的Docker容器的ip地址,我们需要进入一个正在运行的容器并用下面的命令检查ip地址。首先,我们运行一个新的容器并进入。如果你已经有一个正在运行的容器,你可以跳过这个步骤。
|
||||
|
||||
# docker run -it ubuntu
|
||||
|
||||
现在,我们可以运行ip a来得到容器的ip地址了。
|
||||
|
||||
# ip a
|
||||
|
||||
![Docker Container IP](http://blog.linoxide.com/wp-content/uploads/2015/05/docker-container-ip.png)
|
||||
|
||||
### 3. 映射暴露的端口 ###
|
||||
|
||||
要映射配置在Dockerfile的暴露端口,我们只需用下面带上-P标志的命令。这会打开docker容器的随机端口并映射到Dockerfile中定义的端口。下面是使用-P来打开/映射定义的端口的例子。
|
||||
|
||||
# docker run -itd -P httpd
|
||||
|
||||
![Mapping Expose Port](http://blog.linoxide.com/wp-content/uploads/2015/05/mapping-expose-port.png)
|
||||
|
||||
上面的命令会映射Dockerfile中定义的httpd 80端口到容器的端口上。我们用下面的命令来查看正在运行的容器暴露的端口。
|
||||
|
||||
# docker ps
|
||||
|
||||
并且可以用下面的curl命令来检查。
|
||||
|
||||
# curl http://localhost:49153
|
||||
|
||||
![Curl Exposed Port](http://blog.linoxide.com/wp-content/uploads/2015/05/curl-exposed-port-e1431034586219.png)
|
||||
|
||||
### 4. 映射到特定的端口上 ###
|
||||
|
||||
我们也可以映射暴露端口或者docker容器端口到我们指定的端口上。要实现这个,我们用-p标志来定义我们的需要。这里是我们的一个例子。
|
||||
|
||||
# docker run -itd -p 8080:80 httpd
|
||||
|
||||
上面的命令会映射8080端口到80上。我们可以运行curl来检查这点。
|
||||
|
||||
# curl http://localhost:8080
|
||||
|
||||
![Mapping Specific Port](Curl Exposed Port)
|
||||
|
||||
### 5. 创建自己的网桥 ###
|
||||
|
||||
要给容器创建一个自定义的IP地址,在本篇中我们会创建一个名为bro的新网桥。要分配需要的ip地址,我们需要在运行docker的主机中运行下面的命令。
|
||||
|
||||
# stop docker.io
|
||||
# ip link add br0 type bridge
|
||||
# ip addr add 172.30.1.1/20 dev br0
|
||||
# ip link set br0 up
|
||||
# docker -d -b br0
|
||||
|
||||
![Creating Bridge Interface](http://blog.linoxide.com/wp-content/uploads/2015/05/creating-bridge-interface.png)
|
||||
|
||||
创建完docker网桥之后,我们要让docker的守护进程知道它。
|
||||
|
||||
# echo 'DOCKER_OPTS="-b=br0"' >> /etc/default/docker
|
||||
# service docker.io start
|
||||
|
||||
![Adding Interface to Docker](http://blog.linoxide.com/wp-content/uploads/2015/05/adding-interface-to-docker.png)
|
||||
|
||||
到这里,桥接后的接口将会分配给容器新的在桥接子网内的ip地址。
|
||||
|
||||
### 6. 链接到另外一个容器上 ###
|
||||
|
||||
我们可以用Dokcer连接一个容器到另外一个上。我们可以在不容的容器上运行不同的程序,并且相互连接或链接。链接允许容器间相互连接并安全地从一个容器上传输信息给另一个容器。要做到这个,我们可以使用--link标志。首先,我们使用--name标志来表示training/postgres镜像。
|
||||
|
||||
# docker run -d --name db training/postgres
|
||||
|
||||
![Running db Container](http://blog.linoxide.com/wp-content/uploads/2015/05/running-db-container.png)
|
||||
|
||||
完成之后,我们将容器db与training/webapp链接来形成新的叫web的容器。
|
||||
|
||||
# docker run -d -P --name web --link db:db training/webapp python app.py
|
||||
|
||||
![linking two containers](http://blog.linoxide.com/wp-content/uploads/2015/05/linking-two-containers.png)
|
||||
|
||||
### 总结 ###
|
||||
|
||||
Docker网络很神奇也好玩,因为有我们可以对docker容器做很多事情。这里有些简单和基础的我们可以把玩docker网络命令。docker的网络是非常高级的。我们可以用它做很多事情。如果你有任何的问题、建议、反馈请在下面的评论栏写下来以便于我们我们可以提升或者更新文章的内容。谢谢! 玩得开心!:-)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-how-to/networking-commands-docker-containers/
|
||||
|
||||
作者:[Arun Pyasi][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://linoxide.com/author/arunp/
|
Loading…
Reference in New Issue
Block a user