端口映射与容器关联
从外部访问容器应用
ubuntu@ubuntu18:/tmp/dockertmp$ docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
-P 随机映射
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d -P training/webapp python app.py
17d43e4455b4d5d6e59177c2d62d9f799ee7eccd04a95abb41eee8783775e0f5
ubuntu@ubuntu18:/tmp/dockertmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17d43e4455b4 training/webapp "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:32768->5000/tcp nifty_agnesi
b1510d0f6822 ubuntu:18.04 "/bin/bash" 43 minutes ago Up 39 minutes dadata
32f4ce2760f6 flag:0.1 "/bin/sh -c 'while t…" 4 hours ago Up 4 hours test5
ubuntu@ubuntu18:/tmp/dockertmp$ docker logs nifty_agnesi
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
ubuntu@ubuntu18:/tmp/dockertmp$
ubuntu@ubuntu18:/tmp/dockertmp$ curl http://127.0.0.1:32768/
Hello world!
映射指定端口/地址
映射指定端口
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d -p 5000:5000 -p 3000:80 --name webapp1 training/webapp 074aa8e19fbf47fb9a25555f078e5aa3016d375c8625bafd038259152111291d
ubuntu@ubuntu18:/tmp/dockertmp$ curl -v 127.0.0.1:5000
* Rebuilt URL to: 127.0.0.1:5000/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.58.0
> Accept: */*
Hello world!
映射指定端口到指定地址端口
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d -p 127.0.0.1:4000:5000 training/webapp
30a4648e1c65cb2b8646066b059a7ce7eac68b48b75afd6c3b212be46dbc1a4d
ubuntu@ubuntu18:/tmp/dockertmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30a4648e1c65 training/webapp "python app.py" 5 seconds ago Up 4 seconds 127.0.0.1:4000->5000/tcp lucid_gagarin
b1510d0f6822 ubuntu:18.04 "/bin/bash" About an hour ago Up About an hour dadata
32f4ce2760f6 flag:0.1 "/bin/sh -c 'while t…" 4 hours ago Up 4 hours test5
ubuntu@ubuntu18:/tmp/dockertmp$ curl 127.0.0.1:4000
Hello world!
映射容器端口到某地址的随机端口
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d -p 127.0.0.1::5000 training/webapp
ab7efb7bf6e46203079bd326503c4f6b15d8680d6271899cee930b6dd1555164
ubuntu@ubuntu18:/tmp/dockertmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab7efb7bf6e4 training/webapp "python app.py" 6 seconds ago Up 5 seconds 127.0.0.1:32768->5000/tcp focused_allen
ubuntu@ubuntu18:/tmp/dockertmp$
查看容器端口映射信息
ubuntu@ubuntu18:/tmp/dockertmp$ docker port focused_allen
5000/tcp -> 127.0.0.1:32768
ubuntu@ubuntu18:/tmp/dockertmp$
容器互联
1. docker run --link的作用
docker run --link可以用来链接2个容器,使得源容器(被链接的容器)和接收容器(主动去链接的容器)之间可以互相通信,并且接收容器可以获取源容器的一些数据,如源容器的环境变量。
–link的格式:
–link :alias
其中,name和id是源容器的name和id,alias是源容器在link下的别名。
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d --name db training/postgres
dbb19a7f61ee060a5506cb71369b23bf96eba4025c1ee2fba260b700574ce272
ubuntu@ubuntu18:/tmp/dockertmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbb19a7f61ee training/postgres "su postgres -c '/us…" 8 seconds ago Up 6 seconds 5432/tcp db
ubuntu@ubuntu18:/tmp/dockertmp$ docker run -d -P --name web --link db:db training/webapp python app.py
6b2419d0180e5b954f5b0ca6d80c727be662fb6db1cb220eeffa6ee645f02f34
ubuntu@ubuntu18:/tmp/dockertmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b2419d0180e training/webapp "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0:32769->5000/tcp web
dbb19a7f61ee training/postgres "su postgres -c '/us…" 2 minutes ago Up 2 minutes 5432/tcp db
ubuntu@ubuntu18:/tmp/dockertmp$ docker exec -it web /bin/bash
root@6b2419d0180e:/opt/webapp# env
HOSTNAME=6b2419d0180e
DB_NAME=/web/db
TERM=xterm
DB_PORT_5432_TCP_ADDR=172.17.0.5
DB_PORT=tcp://172.17.0.5:5432#postgres主机
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
root@6b2419d0180e:/opt/webapp# ping db
PING db (172.17.0.5) 56(84) bytes of data.
64 bytes from db (172.17.0.5): icmp_seq=1 ttl=64 time=0.109 ms
64 bytes from db (172.17.0.5): icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from db (172.17.0.5): icmp_seq=3 ttl=64 time=0.072 ms
^C
--- db ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2041ms
rtt min/avg/max/mdev = 0.049/0.076/0.109/0.026 ms
root@6b2419d0180e:/opt/webapp#