docker命令的基本操作

一。帮助信息

  1. 分组的操作

[gh@localhost ~]$ docker --help

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

  2. 分组详细操作信息,比如查看 image 的操作信息

[gh@localhost ~]$ docker image --help

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

[gh@localhost ~]$ 

二。基本操作

  1. image 操作

docker image pull :下载镜像 (从国内仓库下载:https://c.163.com/hub)
[gh@localhost ~]$ docker image pull hub.c.163.com/library/busybox:latest
latest: Pulling from library/busybox
aab39f0bc16d: Pull complete 
Digest: sha256:662af1d642674367b721645652de96f9c147417c2efb708eee4e9b7212697762
Status: Downloaded newer image for hub.c.163.com/library/busybox:latest
[gh@localhost ~]$ 
docker image ls :查看镜像
[gh@localhost ~]$ docker image ls
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/library/busybox   latest              d20ae45477cb        16 months ago       1.13MB
hub.c.163.com/public/nginx      1.2.1               2dc68ff797db        2 years ago         172MB
[gh@localhost ~]$ 

docker image rm : 删除镜像

[gh@localhost ~]$ docker image rm hub.c.163.com/library/busybox
Untagged: hub.c.163.com/library/busybox:latest
Untagged: hub.c.163.com/library/busybox@sha256:662af1d642674367b721645652de96f9c147417c2efb708eee4e9b7212697762
Deleted: sha256:d20ae45477cbc89863fff11d01cdccf28e4ff06ce2eb2f0206ef971b14eaf6c0
Deleted: sha256:6a749002dd6a65988a6696ca4d0c4cbe87145df74e3bf6feae4025ab28f420f2
[gh@localhost ~]$ docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/public/nginx   1.2.1               2dc68ff797db        2 years ago         172MB
[gh@localhost ~]$ 

docker image tag:创建标签和仓库名称。(只是一个引用,类似软链接)

[gh@localhost ~]$ docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
busybox                      latest              d20ae45477cb        16 months ago       1.13MB
hub.c.163.com/public/nginx   1.2.1               2dc68ff797db        2 years ago         172MB
[gh@localhost ~]$ docker image tag hub.c.163.com/public/nginx:1.2.1 nginx:1.2.1
[gh@localhost ~]$ docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
busybox                      latest              d20ae45477cb        16 months ago       1.13MB
nginx                        1.2.1               2dc68ff797db        2 years ago         172MB
hub.c.163.com/public/nginx   1.2.1               2dc68ff797db        2 years ago         172MB
[gh@localhost ~]$ 

更多操作: docker image --help

  2. container 操作

---- docker container ls :查看容器

---- docker container create :创建但不运行容器

---- docker container start : 运行已经创建的容器

---- docker container run : 创建并运行容器

---- docker container stop : 停止运行容器

---- docker container pause : 暂停容器

---- docker container unpause : 恢复暂停的容器

更多操作: docker container --help

使用帮助:docker container run --help

---- docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

---- docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]

[COMMAND]:可传入容器运行的命令

[ARG...] : 传入参数

[OPTIONS]:选项

-t :使用终端

-i :交互式访问

--name:名称

--network:网络(不加默认桥接网络)

[gh@localhost ~]$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:86:28:05:ff  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

--rm : 容器一停就删除

-d :后台运行

---- 用终端交互式运行容器 busybox

[gh@localhost ~]$ docker container run --name aaaaa -it hub.c.163.com/library/busybox
/ # 
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # 

---- 在容器里面创建 /var/html 文件夹

/ # mkdir /var/html
/ # ls /var/html/
/ # 

---- 创建 index.html 文件并写入文字

 

/ # cat /var/html/index.html 
------------
ccccccccccccccccccccc
-----------------
/ # 

---- 查看httpd命令 

/ # httpd -help

-p:监听端口,默认80

-h:网页根目录

-f:前台运行

---- 运行httpd服务

/ # httpd -f -h /var/html/

---- 打开另一个终端查看容器

[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS              PORTS               NAMES
df6c8e341432        hub.c.163.com/library/busybox   "sh"                16 minutes ago      Up 16 minutes                           aaaaa
[gh@localhost ~]$ 

---- 查看名字为 aaaaa 的地址

[gh@localhost ~]$ docker container inspect aaaaa |grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[gh@localhost ~]$ 

---- 访问容器 httpd 服务

[gh@localhost ~]$ curl 172.17.0.2
------------
ccccccccccccccccccccc
-----------------
[gh@localhost ~]$ 

 ---- 退出aaaaa容器

/ # httpd -f -h /var/html/
^C
/ # exit
[gh@localhost ~]$ 

---- 查看容器

[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[gh@localhost ~]$ docker container ls -a
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS                            PORTS               NAMES
df6c8e341432        hub.c.163.com/library/busybox   "sh"                28 hours ago        Exited (130) About a minute ago                       aaaaa
[gh@localhost ~]$ 

---- 运行停止的容器并进入交互式终端

[gh@localhost ~]$ docker container start -i aaaaa
/ # 
/ # exit
[gh@localhost ~]$ 

---- 直接运行容器(默认为守护进程)

[gh@localhost ~]$ docker container start aaaaa
aaaaa
[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS              PORTS               NAMES
df6c8e341432        hub.c.163.com/library/busybox   "sh"                28 hours ago        Up 12 seconds                           aaaaa
[gh@localhost ~]$ 

---- 进入已经运行的容器

[gh@localhost ~]$ docker container attach aaaaa
/ # 
/ # exit
[gh@localhost ~]$ 

---- 删除已经停止的容器

[gh@localhost ~]$ docker container rm aaaaa
aaaaa
[gh@localhost ~]$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[gh@localhost ~]$ 

---- 容器里的程序必须运行在前台,不然容器一运行就结束了

[gh@localhost ~]$ docker container run --name web1 -d hub.c.163.com/library/busybox
302f332ee47165b59c867c1316d17fb7ffc964c9f3819884ba0e426bbd113a94
[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[gh@localhost ~]$ docker container ls -a
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS                      PORTS               NAMES
302f332ee471        hub.c.163.com/library/busybox   "sh"                11 seconds ago      Exited (0) 10 seconds ago                       web1
[gh@localhost ~]$ 
COMMAND 显示了"sh"说明容器里运行的是shell,容器一运行就结束.要加-it交互式终端运行
---- 后台运行nginx
[gh@localhost ~]$ docker container run --name web1 -d nginx:1.2.1
d711ad57db0e874433f1111e45899265ac162be3d72a0bf28e5c7a13ab0e2f00
[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
d711ad57db0e        nginx:1.2.1         "/bin/sh -c '/etc/in…"   11 seconds ago      Up 11 seconds       22/tcp, 80/tcp, 443/tcp   web1
[gh@localhost ~]$ 
COMMAND显示了容器运行在前台的命令,PORTS 为端口映射
---- 访问映射的80端口
[gh@localhost ~]$ curl 172.17.0.2
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
[gh@localhost ~]$ 
---- 停止容器并尝试交互式终端启动
[gh@localhost ~]$ docker container stop web1
web1
[gh@localhost ~]$ docker container start web1
start
[gh@localhost ~]$ docker container start -ai web1
Starting nginx: nginx.

前台运行着 nginx

 ---- 向web1容器执行命令

[gh@localhost ~]$ docker container exec web1 ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar  1  2012 /bin/sh -> dash
[gh@localhost ~]$ 

---- 绕过主进程交互式终端执行容器shell命令

[gh@localhost ~]$ docker container exec -it web1 /bin/sh
# ps -e
   PID TTY          TIME CMD
     1 ?        00:00:00 sh
    10 ?        00:00:00 nginx
    11 ?        00:00:00 sshd
    12 ?        00:00:00 nginx
    13 ?        00:00:00 nginx
    14 ?        00:00:00 nginx
    15 ?        00:00:00 nginx
   158 pts/0    00:00:00 sh
   163 pts/0    00:00:00 ps
# exit
[gh@localhost ~]$ 

能够看到第一个shell,即前台,正在运行Nginx。

---- 把Nginx停止,容器也停止

[gh@localhost ~]$ docker container exec web1 ps -e
   PID TTY          TIME CMD
     1 ?        00:00:00 sh
    10 ?        00:00:00 nginx
    11 ?        00:00:00 sshd
    12 ?        00:00:00 nginx
    13 ?        00:00:00 nginx
    14 ?        00:00:00 nginx
    15 ?        00:00:00 nginx
    16 ?        00:00:00 ps
[gh@localhost ~]$ docker container exec web1 kill -2 10 11
[gh@localhost ~]$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[gh@localhost ~]$ 

---- 查看容器日志

[gh@localhost ~]$ docker container logs web1 
Starting nginx: nginx.
[gh@localhost ~]$ 

日志不是保存在文件里,而是直接输出终端

 

posted @ 2019-01-02 12:30  Python++  阅读(315)  评论(0编辑  收藏  举报