Docker Container 命令详解(包括:容器剥离终端方法、容器内第一进程的重要性、容器状态运行转换图)

Docker Container 命令详解

1、Command Help

1.1 命令帮助

[root@Tang ~]# docker container help

Usage:	docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

1.2 子命令帮助

[root@Tang ~]# docker container run --help
[root@Tang ~]# docker container ls --help
... ...

2、Docker Container Run 启动容器

2.1 Command Help & Usage

[root@Tang ~]# docker container run --help

Usage:	docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
... ...

2.2 Command Options

# -t         # 打开一个伪终端
# -i         # 交互式访问
# --name     # 容器名字
# --network  # 指定网络
# --rm       # 容器一停,自动删除(做测试时常用此选项)
# -d         # 剥离与当前终端的关系;否则会一直占据着终端
     # 端口映射,将容器内服务的端口映射在宿主机的指定端口
	## -p <container port>
	## -p <hostport>:<container port>
	## -p <hostip>:<hostport>:<container port>

2.3 Example

2.3.1 下载镜像,创建容器,进行端口暴露

[root@Tang ~]# docker search nginx            # 搜索 Nginx 相关镜像
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        12257               [OK]                
... ...                                    
[root@Tang ~]# docker image pull nginx:latest  # 下载相关镜像
latest: Pulling from library/nginx
000eee12ec04: Pull complete 
eb22865337de: Pull complete 
bee5d581ef8b: Pull complete 
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@Tang ~]# docker image ls                 # 查看镜像列表,下载成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myap                v2                  c0986260dda2        27 hours ago        347MB
myap                v1                  2a5bcc7a5916        27 hours ago        347MB
nginx               latest              231d40e811cd        4 days ago          126MB
centos              7                   5e35e350aded        2 weeks ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
### 创建并运行一个容器,名称为 myweb1 ,镜像是 nginx:latest ###
### 暴露容器端口 80 ,宿主机端口为 8000 ,剥离当前运行终端 ### 
[root@Tang ~]# docker container run -p 8000:80 -d --name myweb1 nginx:latest
87d042f5e6d87f9ab74b6fb4535b85e3d3e72584ecadc081a1128f0e9a87e23d
[root@Tang ~]# docker container ps -a             # 查看 docker container 的运行状态
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
87d042f5e6d8        nginx:latest        "nginx -g 'daemon of…"   28 seconds ago      Up 26 seconds       0.0.0.0:8000->80/tcp   myweb1
[root@Tang ~]# docker container port myweb1       # 查询 docker 端口映射
80/tcp -> 0.0.0.0:8000

2.3.2 宿主机相关信息查看,访问容器 web 页面

### 宿主机地址查看 ###
[root@Tang ~]# ipinfo 
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.141.252  netmask 255.255.255.0  broadcast 172.16.141.255
        
### 宿主机端口映射查看 ###
[root@Tang ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 1317 packets, 91798 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  309 29887 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 208 packets, 22398 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 2 packets, 120 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT 3 packets, 180 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  218 13733 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
  308 20521 MASQUERADE  all  --  *      enp1s0  0.0.0.0/0            0.0.0.0/0           
    0     0 MASQUERADE  all  --  *      tang    0.0.0.0/0            0.0.0.0/0           
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值