容器(一)

新建和启动容器

OPTIONS说明:

  • — name = “容器新名称” 为容器制定一个名称
  • -d 后台运行容器并返回容器id, 即启动守护式容器(后台运行)
  • -i 以交互模式运行容器,通常与 -t 同时使用
  • -t 为容器重新分配一个伪输入终端,通常与 -i 连用,也就是启动交互式容器
  • -P 随机端口映射
  • -p 指定端口映射

举个栗子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0 b8e65a4d736d 4 months ago 680MB
redis 6.0.8 16ecd2772934 18 months ago 104MB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker run -it ubuntu:15.10 /bin/bash
root@4fb757f69adb:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:26 pts/0 00:00:00 /bin/bash
root 11 1 0 03:26 pts/0 00:00:00 ps -ef
root@4fb757f69adb:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@4fb757f69adb:/# exit
exit
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]#

docker run -it ubuntu:15.10 /bin/bash
/bin/bash 希望有交互式shell 就用/bin/bash

查询正在运行的容器

docker ps [OPTIONS]

1
2
3
4
5
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdee90a8c77d ubuntu "bash" 3 minutes ago Exited (0) 3 minutes ago ubuntu
0dcaa91afc0e ubuntu "/bin/bush" 5 minutes ago Created myubuntu
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]#

OPTIONS:

  • -a 展示所有容器,包含历史用过容器
  • -d 只展示容器id
  • -l 显示最近创建的容器
  • -n 展示最近创建的前n个容器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    List containers
    [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps -n 1
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    cdee90a8c77d ubuntu "bash" 17 minutes ago Exited (0) 17 minutes ago ubuntu
    [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps -an 1
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    cdee90a8c77d ubuntu "bash" 17 minutes ago Exited (0) 17 minutes ago ubuntu
    [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps -an 2
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    cdee90a8c77d ubuntu "bash" 17 minutes ago Exited (0) 17 minutes ago ubuntu
    0dcaa91afc0e ubuntu "/bin/bush" 19 minutes ago Created myubuntu
    [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]#

退出容器

  • exit

run进入容器,exit退出容器, 容器停止

  • ctrl + p + q

run进入容器,exit退出容器, 容器不停止

1
2
3
4
5
6
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker run -it ubuntu bash
root@562278524cda:/# [root@iZ8vbfaek3x3ogtpxnpnwfZ ~]#
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]#
[root@iZ8vbfaek3x3ogtpxnpnwfZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
562278524cda ubuntu "bash" 16 seconds ago Up 15 seconds loving_dewdney