docker鏡像與容器linux
docker改變了什麼?docker
面向產品:產品交付shell
面向開發:簡化環境配置ubuntu
面向測試:多版本測試centos
面向運維:環境一致性bash
面向架構:自動化擴容(微服務)服務器
獲取鏡像網絡
可使用 docker pull命令來從倉庫獲取所須要的鏡像。架構
[root@localhost~]# docker pull centos#獲取一個centos的鏡像運維
[root@localhost~]# docker p_w_picpaths#查看docker的鏡像
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest a8493f5f50ff 12 days ago 192.5 MB
在列出信息中,能夠看到幾個字段信息,
來自於哪一個倉庫,好比centos,
鏡像的標記,好比lastest
它的 ID 號( 惟一)
建立時間
鏡像大小
dockerrmi + ID#刪除鏡像,若是這個鏡像建立了容器就不能刪除
建立而且啓動第一個容器
[root@localhost ~]#docker run centos /bin/echo "hehe"
hehe
全部容器都是使用這個參數dockerrun,centos指的是鏡像的名稱,後面跟的是命令(執行命令)
能夠用dockerrun --help查看參數
4.1 docker help
docker command
docker # docker 命令幫助
Commands:
attach Attach to a running container # 當前 shell 下 attach鏈接指定運行鏡像
build Build an p_w_picpath from a Dockerfile # 經過 Dockerfile 定製鏡像
commit Create a new p_w_picpath from a container'schanges # 提交當前容器爲新的鏡像
cp Copy files/folders from the containersfilesystem to the host path
#從容器中拷貝指定文件或者目錄到宿主機中
create Create a new container # 建立一個新的容器,同run,但不啓動容器
diff Inspect changes on a container'sfilesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務獲取容器實時事件
exec Run a command in an existingcontainer #在已存在的容器上運行命令
export Stream the contents of a container as a tararchive
# 導出容器的內容流做爲一個 tar 歸檔文件[對應import ]
history Show the history of an p_w_picpath # 展現一個鏡像造成歷史
p_w_picpaths List p_w_picpaths #列出系統當前鏡像
import Create a new filesystem p_w_picpath from thecontents of a tarball
# 從tar包中的內容建立一個新的文件系統映像[對應export]
info Display system-wide information # 顯示系統相關信息
inspect Return low-level information on a container # 查看容器詳細信息
kill Kill a running container # kill 指定 docker 容器
load Load an p_w_picpath from a tar archive # 從一個 tar 包中加載一個鏡像[對應save]
login Register or Login to the docker registryserver
# 註冊或者登錄一個 docker源服務器
logout Log out from a Docker registry server # 從當前 Docker registry 退出
logs Fetch the logs of a container # 輸出當前容器日誌信息
port Lookup the public-facing port which isNAT-ed to PRIVATE_PORT
#查看映射端口對應的容器內部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an p_w_picpath or a repository from thedocker registry server
#從docker鏡像源服務器拉取指定鏡像或者庫鏡像
push Push an p_w_picpath or a repository to thedocker registry server
#推送指定鏡像或者庫鏡像至docker源服務器
restart Restart a running container # 重啓運行的容器
rm Remove one or more containers # 移除一個或者多個容器
rmi Remove one or more p_w_picpaths
#移除一個或多個鏡像[無容器使用該鏡像纔可刪除,不然需刪除相關容器纔可繼續或 -f 強制刪除]
run Run a command in a new container
#建立一個新的容器並運行一個命令
save Save an p_w_picpath to a tar archive # 保存一個鏡像爲一個 tar 包[對應load]
search Search for an p_w_picpath on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啓動容器
stop Stop a running containers # 中止容器
tag Tag an p_w_picpath into a repository # 給源中鏡像打標籤
top Lookup the running processes of acontainer # 查看容器中運行的進程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號
wait Block until a container stops, then printits exit code
# 截取容器中止時的退出狀態值
Run 'docker COMMAND --help' for more information on a comman
查看啓動的docker
[root@localhost ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6daf1bacfa9c centos "/bin/echo hehe" 15 minutes ago Exited (0) 15 minutes ago cranky_darwin
參數介紹:
首先是一個容器的ID,而後是鏡像,後面是命令(COMMAND),建立的時間,狀態,端口,名稱(docker有名稱庫,不指定名字的時候會自動幫咱們產生名稱)
建立一個指定名字的容器
[root@localhost ~]#docker run --name mydocker -t -i centos /bin/bash
參數介紹:
--name是指定docker的名字,-t 是讓系統分配一個僞終端tty,-i表示讓這個容器的標準輸入保持打開的狀態(打開以後就能夠登進去)(-t-i要一塊兒使用),最後是?bin/bash表示執行一個命令,這個命令是/bin/bash.-h指定容器系統的主機名
[root@94ba510e3127 /]# ps -ef#能夠查看這個容器運行了什麼進程
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:43 ? 00:00:00 /bin/bash(這個進行是我啓動容器的時候讓他執行的)
root 13 1 9 10:48 ? 00:00:00 ps -ef
[root@localhost yum]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ba510e3127 centos "/bin/bash" 7minutes ago Up 7 minutes mydocker
能夠看到docker的主機名跟容器的ID是同樣的
當咱們執行dockersrun的時候的流程:首先docker會檢測本地有沒有一個叫centos的鏡像,若是沒有他就會從公共的倉庫(dockerhub)去下載,第二他就利用centos的鏡像啓動了一個容器
能夠用exit推出這個容器
[root@localhost ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ba510e3127 centos "/bin/bash" 12 minutes ago Exited (0) 7 seconds ago mydocker
6daf1bacfa9c centos "/bin/echo hehe" 32 minutes ago Exited (0) 32 minutes ago cranky_darwin
鏡像操做的幾個命令:
搜索鏡像:dockersearch
獲取鏡像:dockerpull
查看鏡像:dockerp_w_picpaths
刪除鏡像:dockerrmi
啓動容器:dockerrun --name -hhostname
中止容器:docker stopCONTAINER ID
查看容器:dockersps-a-L(加了-a會顯示全部的容器進程)
進入容器:dockerexec|dockerattach ID
刪除容器:dockerrm
中止後啓動容器:dockerstart ID
[root@localhost ~]#docker start 94ba510e3127
94ba510e3127
[root@localhost ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ba510e3127 centos "/bin/bash" 28 minutes ago Up 11 seconds mydocker
[root@localhost~]# docker attach 94ba510e3127#進入容器
[root@94ba510e3127 /]#ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 11768 1880 ? Ss 11:12 0:00 /bin/bash
root 13 0.0 0.1 47440 1668 ? R+ 11:13 0:00 ps aux
用sttach進入的話有問題:若是你再開個窗戶再進去的話他的顯示是同步的,並且exit退出後這個容器就停了(由於咱們退出了bash)
能夠用nsenter來進入(不過須要知道進程的PID)(若是沒有這個命令的話能夠用yuminstall util-linux來安裝)
查看PID
[root@localhost ~]# docker inspect --format"``.`State`.`Pid`" 94ba510e3127(ID或者容器名)
20979
[root@localhost~]# nsenter -t 20979 -u -i -n -p#進入容器
[root@94ba510e3127 ~]#
[root@localhost ~]#nsenter -t 20979 -u -i -n -p
[root@94ba510e3127 ~]#nsenter --help
用法:
nsenter [options] <program>[<argument>...]
Run a program withnamespaces of other processes.
選項:
-t, --target <pid> 要獲取名字空間的目標進程
-m, --mount[=<file>] enter mount namespace
-u, --uts[=<file>] enter UTS namespace (hostname etc)
-i, --ipc[=<file>] enter System V IPC namespace
-n, --net[=<file>] enter network namespace
-p, --pid[=<file>] enter pid namespace
-U, --user[=<file>] enter user namespace
-S, --setuid <uid> set uid in entered namespace
-G, --setgid <gid> set gid in entered namespace
--preserve-credentials do not touch uidsor gids
-r, --root[=<dir>] set the root directory
-w, --wd[=<dir>] set the working directory
-F, --no-fork 執行 <程序> 前不 fork
-Z, --follow-context set SELinux context according to --targetPID
-h, --help 顯示此幫助並退出
-V, --version 輸出版本信息並退出
能夠把查找PID和登陸容器的命令寫成腳本
[root@localhost ~]# vins.sh
#!/bin/bash
PID=$(docker inspect--format "``.`State`.`Pid`" $1)
nsenter -t $PID -u -i-n -p
這樣子就只要傳入一個容器IP就能夠登錄了
[root@localhost ~]#./ns.sh 94ba510e3127
[root@94ba510e3127 ~]#
[root@94ba510e3127~]# docker run --rm centos /bin/echo "hehe"#執行容器而且刪除容器
hehe
刪除全部的容器
dockerkill $(docker ps -a -q)
[root@94ba510e3127 ~]# docker ps -a -q#只列出PID
94ba510e3127
6daf1bacfa9c
在ubuntu容器的基本操做:
ubuntu@ubuntu-virtual-machine:~$ docker run ubuntu echo "hello word"
hello word
啓動交互式容器:
docker run -i -t IMAGE /bin/bash
-i--interactive=true |fasle 默認是fasle
-t --tty=true|fasle 默認是fasle
查看容器:
docker ps [-a] [-l]
-a 列出全部的容器,-l列出最新建立的容器
docker inspect 能夠接容器的ID也能夠是名字
會對容器詳細的檢查,返回配置的信息(包括名稱,命令,網絡配置等)
自定義容器命名:
docker run --name=container01 -I -t ubuntu /bin/bash
從新啓動中止的容器:
docker start [-i] 容器名
刪除已經中止的容器(不能刪除運行中的)
docker rm ID
守護試容器:
什麼是守護試容器:在大多數狀況下,咱們須要一個可以長期運行的容器來提供服務。
可以長期運行
沒有交互式會話
適合運行應用程序和服務
以守護形式運行容器:
1,docker run -i-t IMAGE /bin/bash
Ctrl+p Ctrl +q結束的容器,這樣子容器就會在後天運行
首先建立一個容器
buntu@ubuntu-virtual-machine:~$ docker run -i -t ubuntu /bin/bash
root@f2d00398e2c4:/# ubuntu@ubuntu-virtual-machine:~$ #Ctrl+p Ctrl +q結束的容器
而後
ubuntu@ubuntu-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2d00398e2c4 ubuntu:latest "/bin/bash" 37 seconds ago Up 31 seconds cocky_lalande
發現還在後臺運行
怎麼結束守護運行容器?
能夠附加到運行中的容器:
docker attach 容器名
ubuntu@ubuntu-virtual-machine:~$ docker attach f2d00398e2c4#從新進入容器
root@f2d00398e2c4:/#
root@f2d00398e2c4:/# exit#用exit退出
exit
ubuntu@ubuntu-virtual-machine:~$ docker ps#容器已經再也不運行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
啓動守護式容器2:
docker run -d 鏡像名 【COMMAND】[ARG…]#-d會告訴機器在啓動容器時在後臺的模式運行,只是之後臺的形式運行,在命令結束後依舊會中止
ubuntu@ubuntu-virtual-machine:~$ docker run --name dc1 -d ubuntu /bin/bash -c "while true; do echo hello word;sleep 1;done"
00cf672ea018961f73e0ec16f9dbc98750894340190575e1913cf5263576423d
ubuntu@ubuntu-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00cf672ea018 ubuntu:latest "/bin/bash -c 'while 12 seconds ago Up 10 seconds
查看容器內部運行的狀況:能夠用容器的logs日誌命令來查看容器內部運行的狀況
查看容器日誌:
docker los [-f] [-t] [--tail] 容器名
-f --follow=true|false 默認爲false#告訴logs命令一直跟蹤日誌的變化並返回結果
-t --timestamps=true|false 默認爲false#是在返回的結果上加上時間戳
--tail=「all」#選擇結尾處多少數量的日誌,不指定的話就會返回全部的日誌
查看容器內的進程:
能夠用:docker top 容器名查看運行中容器運行狀況
ubuntu@ubuntu-virtual-machine:~$ docker top dc1
UID PID PPID C STIME TTY TIME CMD
root 8342 7138 0 15:02 ? 00:00:14 /bin/bash -c while true; do echo hello word;sleep 1;done
在運行的容器中啓動新的進程:
docker exec [-d] [-i] [-t] 容器名 【COMMAND】[ARG](跟run命令類似)
ubuntu@ubuntu-virtual-machine:~$ docker exec -i -t dc1 /bin/bash
root@00cf672ea018:/# ubuntu@ubuntu-virtual-machine:~$ #使用Ctrl+p Ctrl +q
ubuntu@ubuntu-virtual-machine:~$ docker top
docker: "top" requires a minimum of 1 argument. See 'docker top --help'.
ubuntu@ubuntu-virtual-machine:~$ docker top dc1
UID PID PPID C STIME TTY TIME CMD
root 8342 7138 0 15:02 ? 00:00:18 /bin/bash -c while true; do echo hello word;sleep 1;done
root 12826 7138 0 16:11 pts/1 00:00:00 /bin/bash
root 12889 8342 0 16:12 ? 00:00:00 sleep 1
中止守護式容器:
1,docker stop 容器名#是發送一個信號給容器,等待容器中止
2,docker kill 容器名#直接中止
ubuntu@ubuntu-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d3123ba6245 ubuntu:latest "/bin/bash -c 'while 6 seconds ago Up 5 seconds dc2
00cf672ea018 ubuntu:latest "/bin/bash -c 'while 13 hours ago Up 13 hours dc1
ubuntu@ubuntu-virtual-machine:~$ docker stop dc2#中止的時候會等待,成功中止後會返回容器的名字
dc2
ubuntu@ubuntu-virtual-machine:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00cf672ea018 ubuntu:latest "/bin/bash -c 'while 13 hours ago Up 13 hours dc1
ubuntu@ubuntu-virtual-machine:~$ docker kill dc1
dc1