Centos7 docker基础5-网络配置

一、从外部访问容器应用

当使用 -P 标记时,Docker 会随机映射一个49000~49900(这个范围还待考证)的端口到容器内部开放的网络端口

使用 -p 指定端口到容器的端口;可以多次使用-p
docker run -d -p 5000:5000 -p 8080:80 docker.io/training/webapp python app.py

[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                       PORTS                                          NAMES
cd55feca7ff0        docker.io/training/webapp   "python app.py"     18 seconds ago      Up 18 seconds                0.0.0.0:5000->5000/tcp, 0.0.0.0:8080->80/tcp   eloquent_wilson
d9834d820a56        docker.io/training/webapp   "python app.py"     9 minutes ago       Up 9 minutes                 0.0.0.0:32778->5000/tcp                        optimistic_gates
9256ad44457a        docker.io/centos            "/bin/bash"         26 minutes ago      Exited (0) 26 minutes ago                                                   dbdata2
9557a29349ed        docker.io/centos            "/bin/bash"         About an hour ago   Exited (137) 9 minutes ago                                                  dbdata

#docker port 容器ID

#可以查看此容器正在映射的端口

[root@centos7 ~]# docker port cd55feca7ff0
5000/tcp -> 0.0.0.0:5000
80/tcp -> 0.0.0.0:8080

-p 也可以指定IP:port 如
[root@centos7 ~]# docker run -d -p 127.0.0.1:5001:5000 –name web4 docker.io/training/webapp python app.py

1aa8018d3e70ef32891a1dc4071058606bf530dc651369545d8beeb0af0a9e69
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                           PORTS                                          NAMES
1aa8018d3e70        docker.io/training/webapp   "python app.py"     1 second ago        Up 1 second                      127.0.0.1:5001->5000/tcp                       web4                                               dbdata
[root@centos7 ~]# netstat -tuplan | grep 5001
tcp        0      0 127.0.0.1:5001          0.0.0.0:*               LISTEN      20263/docker-proxy- 
[root@centos7 ~]# 

不过这个好像没有什么用,5001又不开放给其他网络,只有127.0.0.1才通

二、容器互联

比如说web应用与数据库的容器直接互联 而不是通过外部去连接

#下载且开启postgres数据库容器
docker run -d –name db docker.io/postgres
运行结果:

[root@centos7 ~]# docker run -d --name db docker.io/postgres
Unable to find image 'docker.io/postgres:latest' locally
Trying to pull repository docker.io/library/postgres ... 
latest: Pulling from docker.io/library/postgres
0a4690c5d889: Pull complete 
723861590717: Pull complete 
db019468bdf4: Pull complete 
91cb81a60371: Pull complete 
........
Digest: sha256:68b49a280d2fbe9330c0031970ebb72015e1272dfa25f0ed7557514f9e5ad7b7
Status: Downloaded newer image for docker.io/postgres:latest
d7a13f84879d2d47a583c4cfd99aa03a7a5bf047ae1ef335311bc09f2919be20
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS               NAMES
d7a13f84879d        docker.io/postgres   "docker-entrypoint..."   About a minute ago   Up About a minute   5432/tcp            db

新建web容器与db容器链接

docker run -d -P --name web --link db:db docker.io/training/webapp python app.py

#–link db:db

运行结果:

[root@centos7 ~]# docker run -d -P --name web --link db:db docker.io/training/webapp python app.py
f57d225265ed0943c9dd127f8bb48f3df185993226aabf338a9ec8bb5e70bb54
[root@centos7 ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                     NAMES
f57d225265ed        docker.io/training/webapp   "python app.py"          5 seconds ago       Up 4 seconds        0.0.0.0:32779->5000/tcp   web
d7a13f84879d        docker.io/postgres          "docker-entrypoint..."   4 minutes ago       Up 4 minutes        5432/tcp                  db
[root@centos7 ~]# 

#如何查看容器之间是否存在link???
答:用docker inspect 可以看 不知道还有没有更便捷的方式查看?

[root@centos7 ~]# docker inspect f57d225265ed | cat -n -
    59              "Links": [
    60                  "/db:/web/db"
    61              ],

查看做过–link的web中 环境变量有何改变

[root@centos7 ~]# docker exec -ti f57d225265ed /bin/bash
root@f57d225265ed:/opt/webapp# env
DB_ENV_PGDATA=/var/lib/postgresql/data
HOSTNAME=f57d225265ed
DB_NAME=/web/db
TERM=xterm
#######################
DB_PORT_5432_TCP_ADDR=172.17.0.2
DB_PORT=tcp://172.17.0.2:5432
DB_ENV_LANG=en_US.utf8
DB_ENV_GOSU_VERSION=1.11
DB_PORT_5432_TCP=tcp://172.17.0.2:5432
#######################
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DB_ENV_PG_MAJOR=11
PWD=/opt/webapp
DB_PORT_5432_TCP_PORT=5432
SHLVL=1
HOME=/root
DB_PORT_5432_TCP_PROTO=tcp
LESSOPEN=| /usr/bin/lesspipe %s
DB_ENV_PG_VERSION=11.4-1.pgdg90+1
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env

查看容器中的/etc/hosts

root@f57d225265ed:/opt/webapp# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      db d7a13f84879d   #这个数据库的name与容器ID
172.17.0.3      f57d225265ed
root@f57d225265ed:/opt/webapp# 

web容器可以ping通db

root@f57d225265ed:/opt/webapp# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.180 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.094 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.069 ms

教程参考于:http://www.bdkyr.com

# docker
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×