容器操作基本命令

[[email protected] opt]# docker ps   #显示正在运行的容器

[[email protected] opt]# docker ps -a   #显示正在运行的容器以及已经关闭的所有容器

[[email protected] opt]# docker run -it alpine   #从镜像启动一个容器

[[email protected] opt]# docker rm -f (CONTAINER ID)   #删除运行中的容器

 

 

随机映射端口

[[email protected] ~]# docker pull nginx

[[email protected] ~]# docker run -P nginx      #前台启动并随机映射

容器操作基本命令

效果展示:

容器操作基本命令

 

指定端口81映射到容器80端口

容器操作基本命令

 

#本地端口81映射到容器80端口

[[email protected] ~]# docker run -d -p 81:80 --name nginx-test-port2 nginx   r.io/nginx
9925875fa13961676b9b31ddfe04a3cdd8bdfc6d6ae2983f008d362106ac4170

#本地IP:本地端口:容器端口

[[email protected] ~]# docker run -d -p 172.18.180.174:82:80 --name nginx-test-port3 docker.io/nginx
9925875fa13961676b9b31ddfe04a3cdd8bdfc6d6ae2983f008d362106ac4170

#本地IP:本地随机端口:容器端口
[[email protected] ~]# docker run -d -p 172.18.180.174::80 --name nginx-test-port4 docker.io/nginx
e3338f4255a919f8b5cc0c1704bf01d1aba2dfafdb5e3742e1fbb7e26383560f

#本地IP:本地端口:容器端口/协议,默认为TCP协议
[[email protected] ~]# docker run -d -p 172.18.180.174:83:80/udp --name nginx-test-port5 docker.io/nginx
d8d55f4f2d0469eddf6013abcaa7f31a8a960b4ece58f99792ae0a051c2d3a9b

#一次性映射多个端口+协议
[[email protected] ~]# docker run -d -p 86:80/tcp -p 443:443/tcp -p 53:53/udp --name nginx-test-port6 docker.io/nginx
7a6f5661e5794b6af8c055d018d2765d7d27858c98233d2463dbd6a3b11067bf

#查看运行的容器

容器操作基本命令

#查看nginx容器访问日志

[[email protected] ~]# docker logs nginx-test-port3     #一次查看
[[email protected] ~]# docker logs -f nginx-test-port4   #持续查看

#查看容器已经映射的端口

容器操作基本命令

#创建并进入容器

[[email protected] ~]# docker run -t -i --name test-centos1 docker.io/centos /bin/bash  #创建容器后直接进入,执行exit退出后容器关闭
[[email protected] /]# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.2  0.2  12012  2056 pts/0    Ss   11:43   0:00 /bin/bash
root        13  0.0  0.1  46340  1704 pts/0    R+   11:43   0:00 ps -aux
[[email protected] /]# exit
exit
[[email protected] ~]#

[[email protected] ~]# docker run -it --rm --name nginx-delete-test docker.io/nginx  #容器退出后自动删除

相关文章: