docker runjava
docker run [OPTIONS] IMAGE [COMMEND] [ARG...]python
OPTIONS:linux
--name=[容器新名字] :爲容器指定一個名稱docker
-d:後臺運行容器,並返回容器ID,也即啓動守護式容器centos
-i:以交互模型運行容器,一般與-t同時使用bash
-t:爲容器從新分配一個僞輸入終端,一般與-i同時使用app
-P:隨機端口映射post
-p:指定端口映射,有如下四種格式:spa
ip:host_port:container_port.net
ip:container_port
host_port:container_port
container_port
新建啓動運行一個centos鏡像的實例(即容器),而且進入該容器進行交互:
neil@linux-famw:~> docker run -it centos [root@6c8275123500 /]# [root@6c8275123500 /]# [root@6c8275123500 /]# ll total 28 -rw-r--r-- 1 root root 12076 Dec 5 01:37 anaconda-post.log lrwxrwxrwx 1 root root 7 Dec 5 01:36 bin -> usr/bin drwxr-xr-x 5 root root 360 Feb 17 14:06 dev drwxr-xr-x 1 root root 1978 Feb 17 14:06 etc drwxr-xr-x 1 root root 0 Apr 11 2018 home lrwxrwxrwx 1 root root 7 Dec 5 01:36 lib -> usr/lib lrwxrwxrwx 1 root root 9 Dec 5 01:36 lib64 -> usr/lib64 drwxr-xr-x 1 root root 0 Apr 11 2018 media drwxr-xr-x 1 root root 0 Apr 11 2018 mnt drwxr-xr-x 1 root root 0 Apr 11 2018 opt dr-xr-xr-x 269 root root 0 Feb 17 14:06 proc dr-xr-x--- 1 root root 120 Dec 5 01:37 root drwxr-xr-x 1 root root 124 Dec 5 01:37 run lrwxrwxrwx 1 root root 8 Dec 5 01:36 sbin -> usr/sbin drwxr-xr-x 1 root root 0 Apr 11 2018 srv dr-xr-xr-x 13 root root 0 Feb 17 14:06 sys drwxrwxrwt 1 root root 140 Dec 5 01:37 tmp drwxr-xr-x 1 root root 106 Dec 5 01:36 usr drwxr-xr-x 1 root root 160 Dec 5 01:36 var [root@6c8275123500 /]# [root@6c8275123500 /]# [root@6c8275123500 /]#
從上面能夠看到咱們進入了這個容器centos實例,root用戶,6c8275123500是容器id,這裏將這個id做爲了容器內的主機名。
咱們在外部經過docker ps查看容器進程。
neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c8275123500 centos "/bin/bash" 3 minutes ago Up 3 minutes elated_brown neil@linux-famw:~>
另外一邊,若是咱們在容器內 但願退出容器 並中止該容器,則輸入exit。
[root@6c8275123500 /]# [root@6c8275123500 /]# [root@6c8275123500 /]# exit exit neil@linux-famw:~> neil@linux-famw:~>
docker ps [OPTIONS]
-a 列出當前全部正在運行的容器 和 歷史上運行過的容器
-l 顯示最近建立的容器
-n 顯示最近n個建立的容器。包括正在運行的和已經中止的。
-q 靜默模式,僅顯示容器號。多用於與其餘命令組合使用,以方便批量處理容器。
--no-trunc 不截斷輸出。
(本文出自oschina博主happyBKs的博文:https://my.oschina.net/happyBKs/blog/3011199)
示例以下:單個字母的參數能夠連寫,好比-l -q能夠寫成-lq
neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c8275123500 centos "/bin/bash" 5 minutes ago Exited (0) 39 seconds ago elated_brown neil@linux-famw:~> neil@linux-famw:~> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c8275123500 centos "/bin/bash" 5 minutes ago Exited (0) 46 seconds ago elated_brown neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker ps -n 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c8275123500 centos "/bin/bash" 6 minutes ago Exited (0) About a minute ago elated_brown neil@linux-famw:~> neil@linux-famw:~> docker ps -lq 6c8275123500 neil@linux-famw:~>
咱們回過來看看容器內部的狀況,則個centos沒有裝jdk,預裝了python2。這與個人本地宿主機不一樣。
neil@linux-famw:~> docker run -it --name=centos-01 centos [root@94769f89ea30 /]# [root@94769f89ea30 /]# java -version bash: java: command not found [root@94769f89ea30 /]# python Python 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() [root@94769f89ea30 /]#
查看容器列表,能夠看到容器id、對應的鏡像名稱、容器名稱、建立容器的時間、容器狀態等。
neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 11 seconds ago Up 9 seconds centos-01 neil@linux-famw:~>
Ctrl + P, Q 從容器內以不關閉容器的方式退回宿主機。這與在容器內輸入exit不一樣,exit是不只推出容器並且關閉結束容器。
[root@94769f89ea30 /]# [root@94769f89ea30 /]# neil@linux-famw:~> neil@linux-famw:~>
這時候能夠看到這個容器還活着呢。
neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 10 minutes ago Up 10 minutes centos-01 neil@linux-famw:~>
docker start [容器id]
若是咱們想把一個容器啓動,好比將剛纔已經關閉的那個容器啓動:
neil@linux-famw:~> docker ps -n 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 12 minutes ago Up 12 minutes centos-01 6c8275123500 centos "/bin/bash" 30 minutes ago Exited (0) 25 minutes ago elated_brown neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker start 6c8275123500 6c8275123500 neil@linux-famw:~> neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 12 minutes ago Up 12 minutes centos-01 6c8275123500 centos "/bin/bash" 30 minutes ago Up 9 seconds elated_brown neil@linux-famw:~>
docker restart
重啓一個容器也相似:
neil@linux-famw:~> docker restart 94769f89ea30 94769f89ea30 neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 18 minutes ago Up 3 seconds centos-01 6c8275123500 centos "/bin/bash" 35 minutes ago Up 5 minutes elated_brown neil@linux-famw:~>
溫柔型:docker stop [容器id或者容器名]
強制型:docker kill [容器id或者容器名]
neil@linux-famw:~> docker stop 94769f89ea30 94769f89ea30 neil@linux-famw:~> neil@linux-famw:~> docker kill 6c8275123500 6c8275123500 neil@linux-famw:~> neil@linux-famw:~> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES neil@linux-famw:~>
前者慢,後者快;前者相似正常關機,後者相似拔電源。
neil@linux-famw:~> docker ps -n 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 25 minutes ago Exited (137) About a minute ago centos-01 6c8275123500 centos "/bin/bash" 43 minutes ago Exited (137) About a minute ago elated_brown neil@linux-famw:~> neil@linux-famw:~> docker rm 6c8275123500 6c8275123500 neil@linux-famw:~> docker ps -n 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 94769f89ea30 centos "/bin/bash" 26 minutes ago Exited (137) 2 minutes ago centos-01 neil@linux-famw:~>
neil@linux-famw:~> docker rm -f 94769f89ea30 94769f89ea30 neil@linux-famw:~>
neil@linux-famw:~> docker rm -f $(docker ps -a -q)
neil@linux-famw:~> docker ps -a -q | xargs docker rm -f
xargs爲linux命令可變參數