docker 学习笔记(四)

docker 学习笔记(四)

Posted by ZW on July 18, 2021

容器的网络

Docker 允许通过外部访问容器或容器互联的方式来提供网络服务。

-p 随机制定一个端口

```shell script docker run -d -P nginx:alpine

docker logs ebee9139a9d6

docker run -d -p 80:80 nginx:alpine

docker run -d -p 127.0.0.1::80 nginx:alpine


## 容器互联
### 创建网络 
```shell script
docker network create -d bridge my-net

```shell script 运行一个容器并连接到新建的 my-net 网络

docker run -it –rm –name busybox1 –network my-net busybox sh



## 配置容器的dns

也可以在 /etc/docker/daemon.json 文件中增加以下内容
```shell
{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

shell script docker run -it --rm ubuntu:18.04 cat etc/resolv.conf

默认情况下,容器可以主动访问到外部网络的连接,但是外部网络无法访问到容器。