1.docker port 可以查看容器的端口映射關(guān)系
格式:
docker port CONTAINER [PRIVATE_PORT[/PROTO]]?
例子:
[root@centos7 ~]# docker port nginx-c1
443/tcp -> 0.0.0.0:8443
53/udp -> 0.0.0.0:8053
80/tcp -> 0.0.0.0:8080
[root@centos7 ~]# docker port nginx-c1 53/udp
0.0.0.0:8053
2.指定端口映射
docker run -p 可以將容器的預(yù)定義的指定端口映射到宿主機的相應(yīng)端口
注意: 多個容器映射到宿主機的端口不能沖突厨埋,但容器內(nèi)使用的端口可以相同
方式1: 容器80端口映射宿主機本地隨機端口
docker run -p 80 --name nginx-test-port1 nginx?
方式2: 容器80端口映射到宿主機本地端口81
docker run -p 81:80 --name nginx-test-port2 nginx?
方式3: 宿主機本地IP:宿主機本地端口:容器端口
docker run -p 10.0.0.100:82:80 --name nginx-test-port3 docker.io/nginx
方式4: 宿主機本地IP:宿主機本地隨機端口:容器端口,默認從32768開始
docker run -p 10.0.0.100::80 --name nginx-test-port4 docker.io/nginx
方式5: 宿主機本機ip:宿主機本地端口:容器端口/協(xié)議捐顷,默認為tcp協(xié)議
docker run ?-p 10.0.0.100:83:80/udp --name nginx-test-port5 docker.io/nginx
方式6: 一次性映射多個端口+協(xié)議
docker run ?-p 8080:80/tcp -p 8443:443/tcp -p 53:53/udp --name nginx-test-port6 nginx
查看容器的日志
docker logs 可以查看容器中運行的進程在控制臺輸出的日志信息
格式
docker logs [OPTIONS] CONTAINER
范例: 查看容器日志
[root@ubuntu1804 ~]#? docker run alpine /bin/sh -c 'i=1;while true;do echo
hello$i;let i++;sleep 2;done'
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
188c0c94c7c5: Pull complete
Digest: sha256:c0e9560cda118f9ec63ddefb4a173a2b2a0347082d7dff7dc14272e7841a5b5a
Status: Downloaded newer image for alpine:latest
hello1
hello2
hello3
hello4
hello5
^C[root@ubuntu1804 ~]#
[root@ubuntu1804 ~]#docker run -d alpine /bin/sh -c 'i=1;while true;do echo
hello$i;let i++;sleep 2;done'
512622b006c05673630eb04f081f8475400b1cda786b0a8a5d1c1c2fd6dc56a7
[root@ubuntu1804 ~]#docker logs 5126
hello1
hello2
hello3
hello4
hello5
hello6
[root@ubuntu1804 ~]#docker logs --tail 3 5126
hello8
hello9
hello10
#顯示時間
[root@ubuntu1804 ~]#docker logs --tail 0 -t 5126
2020-02-25T13:30:07.321390731Z hello17
#持續(xù)跟蹤
[root@ubuntu1804 ~]#docker logs -f 5126
hello1
hello2
hello3
hello4
實戰(zhàn)案例: 基于CentOS 基礎(chǔ)鏡像手動制作編譯版本 nginx 鏡像
在CentOS 基礎(chǔ)鏡像的容器之上手動編譯安裝nginx荡陷,然后再將此容器提交為鏡像
下載鏡像并初始化系統(tǒng)
[root@ubuntu1804 ~]#docker pull centos:centos7.7.1908
[root@ubuntu1804 ~]#docker images
REPOSITORY ????TAG ????????IMAGE ID ?????CREATED ?????
SIZE
centos ??????centos7.7.1908 ??08d05d1d5859 ????2 months ago ??
204MB
[root@ubuntu1804 ~]#docker run -it centos:centos7.7.1908 /bin/bash
#生成yum源配置
[root@86a48908bb97 /]# yum -y install wget
[root@64944257fa88 /]# rm -rf /etc/yum.repos.d/*
[root@64944257fa88 /]# wget -P /etc/yum.repos.d/
http://mirrors.aliyun.com/repo/Centos-7.repo
http://mirrors.aliyun.com/repo/epel-7.repo
?編譯安裝 nginx
[root@64944257fa88 /]# useradd -r -s /sbin/nologin nginx
#安裝基礎(chǔ)包
[root@64944257fa88 /]# yum -y install gcc gcc-c++ automake pcre pcre-devel zlib
zlib-devel openssl openssl-devel
[root@64944257fa88 /]# cd /usr/local/src
[root@64944257fa88 src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@64944257fa88 src]# tar xf nginx-1.16.1.tar.gz
[root@64944257fa88 src]# cd nginx-1.16.1
[root@64944257fa88 nginx-1.16.1]# ./configure --prefix=/apps/nginx
[root@64944257fa88 nginx-1.16.1]# make && make install
[root@64944257fa88 nginx-1.16.1]# rm -rf nginx*
[root@64944257fa88 nginx-1.16.1]# rm -rf /var/cache/yum/*
關(guān)閉 nginx 后臺運行
[root@64944257fa88 nginx-1.16.1]# cd /apps/nginx/ ?
[root@64944257fa88 nginx]# ls
conf html logs sbin
[root@64944257fa88 nginx]# vi conf/nginx.conf
user nginx;
daemon off;
[root@64944257fa88 nginx]# ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@64944257fa88 nginx]# ll /usr/sbin/nginx
lrwxrwxrwx 1 root root 22 Jan 28 05:29 /usr/sbin/nginx -> /apps/nginx/sbin/nginx
準備相關(guān)數(shù)據(jù)自定義web界面
[root@64944257fa88 nginx]# echo "Nginx Test Page in Docker" >
/apps/nginx/html/index.html
提交為鏡像
#不要退出容器,在另一個終端窗口執(zhí)行以下命令
[root@ubuntu1804 ~]#docker images
REPOSITORY ????TAG ????????IMAGE ID ?????CREATED ?????
SIZE
centos ??????centos7.7.1908 ??08d05d1d5859 ????2 months ago ??
204MB
[root@ubuntu1804 ~]#docker ps
CONTAINER ID ???IMAGE ?????????COMMAND ??????CREATED ???
??STATUS ??????PORTS ???????NAMES
64944257fa88 ???centos:centos7.7.1908 ?"/bin/bash" ????18 seconds ago
?Up 17 seconds ?????????????stupefied_albattani
[root@ubuntu1804 ~]#docker commit -m "nginx1.6.1" 64944257fa88 -c "CMD nginx"
centos7-nginx:1.6.1
sha256:d86d957bb00f35fe09ae38230e1e2d12916f4406e997146c68e34dae7526c079
[root@ubuntu1804 ~]#docker images
REPOSITORY ????TAG ????????IMAGE ID ?????CREATED ?????
SIZE
centos7-nginx ???1.6.1 ???????d86d957bb00f ????2 minutes ago? ?486MB
centos ??????centos7.7.1908 ??08d05d1d5859 ????2 months ago? ?204MB
從自己的鏡像啟動容器
[root@ubuntu1804 ~]#docker run -d -p 80:80 centos7-nginx:1.6.1 nginx
ae90b1abf374138a21f7ed104d14c88f1af23c0b2027c3fe099722fd7fbad3a4
[root@ubuntu1804 ~]#docker ps
CONTAINER ID ???IMAGE ????????COMMAND ??????CREATED ????
?STATUS ??????PORTS ???????NAMES
ae90b1abf374 ???centos7-nginx:1.6.1 ?"nginx" ??????About a minute ago
?Up About a minute ?0.0.0.0:80->80/tcp ?naughty_thompson
備注: 最后面的nginx是運行的命令迅涮,即鏡像里面要運行一個nginx命令废赞,所以前面軟鏈接
到/usr/sbin/nginx,目的為了讓系統(tǒng)不需要指定路徑就可以執(zhí)行此命令
訪問測試
[root@ubuntu1804 ~]#curl 127.0.0.1
Nginx Test Page in Docker
?查看Nginx訪問日志和進程
[root@ubuntu1804 ~]#docker exec -it ae90b1abf374 bash
[root@ae90b1abf374 /]# cat /apps/nginx/logs/access.log
172.17.0.1 - - [28/Jan/2020:05:40:51 +0000] "GET / HTTP/1.1" 200 26 "-"
"curl/7.58.0"
[root@ae90b1abf374 /]# ps aux
USER ???PID %CPU %MEM ?VSZ ?RSS TTY ??STAT START ?TIME COMMAND
root ?????1 ?0.0 ?0.2 ?20572 ?2468 ? ???Ss ?05:40 ?0:00 nginx: master
process nginx
nginx ????6 ?0.0 ?0.3 ?21024 ?3104 ? ???S ??05:40 ?0:00 nginx: worker
process
root ?????7 ?0.3 ?0.2 ?11840 ?2928 pts/0 ?Ss ?05:45 ?0:00 bash
root ????21 ?0.0 ?0.3 ?51764 ?3344 pts/0 ?R+ ?05:46 ?0:00 ps aux