docker最近很火的容器化部署平臺挠蛉。我們可以把docker中的鏡像和容器分別理解成類和對象(類的實例 )。本文的測試環(huán)境針對mac谴古,linux上可能會有些許不同悄窃。
檢查Docker Engine蹂窖,Docker Compose和Docker Machine的版本
MacBook:~ max$ docker --version
Docker version 18.03.1-ce, build 9ee9f40
MacBook:~ max$ docker-compose --version
docker-compose version 1.21.1, build 5a3f1a3
MacBook:~ max$ docker-machine --version
docker-machine version 0.14.0, build 89b8332
測試docker是否已經(jīng)部署好轧抗,可以使用docker run hello-world命令瞬测,如果輸出如下內(nèi)容,說明docker已經(jīng)部署完畢
MacBook:~ max$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
上述命令實際已經(jīng)運(yùn)行了一個鏡像為hello-world容器月趟,從上面我們看出docker運(yùn)行hello-world鏡像的過程灯蝴,
1.docker client先與docker daemon(也就是docker server)建立連接
2.docker daemon從 docker hub pull鏡像下來
3.docker daemon根據(jù)鏡像創(chuàng)建了一個容器孝宗,并且產(chǎn)生了output那句"Hello from Docker!"
4.docker daemon把output通過docker client輸出的終端上,所以我們看到了因妇,那句輸出"Hello from Docker!"
至此,這個容器運(yùn)行完畢婚被,也就退出了。
docker ps命令查看正在運(yùn)行的容器址芯,所以我們?nèi)绻榭慈康娜萜鳎枰玫絛ocker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d0a57302d9b hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago distracted_mclean
用docker ps -a 命令發(fā)現(xiàn)北专,目前本地只有一個hello-world的鏡像,接下來拓颓,我們試著在后臺運(yùn)行一個nigix服務(wù)看看
docker run -d -p 80:80 --name webserver nginx
默認(rèn)會從docker hub下載的鏡像都是latest鏡像,下載完畢后會在后端啟用一個端口為80的web服務(wù)录粱。
-d :分離模式: 在后臺運(yùn)行
-p (小寫) 參數(shù)來指定端口映射腻格,也就是docker daemon中的nginx鏡像運(yùn)行的端口指定為80,并且映射到本地電腦的80端口
--name webserver:表示把這個nginx服務(wù)命令為webserver
打開瀏覽器青抛,查看本地啟用的web服務(wù),如下
關(guān)閉剛才在后臺運(yùn)行web容器服務(wù)
docker stop webserver
如果想運(yùn)行一個容器
docker start 容器名
刪除容器蜜另,需要注意的是刪除容器時,需要確保容器已經(jīng)停止運(yùn)行
docker rm 容器id
接下來举瑰,pull一個centos鏡像下來,試著在 centos中進(jìn)行相關(guān)的操作此迅,如果centos鏡像沒有的話,需要先把centos鏡像下載下來
拉取tag為latest的centos鏡像時,tag可以省略成如下命令
docker pull centos
拉取全部的centos鏡像耸序,命令為
docker pull centos -a
提示:
如果出現(xiàn)Status: Image is up to date for centos:latest 說明已經(jīng)存在了鏡像centos:latest
我們查看所有的鏡像
docker images
刪除鏡像
如果你執(zhí)行了docker pull centos -a操作,在查看所有鏡像會發(fā)現(xiàn)好多centos都被pull下來了罢坝,刪除鏡像用
如果鏡像id相同,需要用docker rmi REPOSITORY:TAG 進(jìn)行操作嘁酿,否則會報錯
docker rmi 鏡像id
運(yùn)行一個容器
運(yùn)行docker 中的REPOSITORY為centos悴了,TAG為latest鏡像運(yùn)行一個hello world备燃。運(yùn)行完畢后渊抄,我們查看容器docker ps -a脐往,發(fā)現(xiàn)剛才運(yùn)行的容器已經(jīng)退出
docker run centos:latest /bin/echo 'hello world'
當(dāng)然tag為latest時薪铜,我們可以簡寫成
docker run centos /bin/echo 'hello world'
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d0a57302d9b hello-world "/hello" 19 minutes ago Exited (0) 19 minutes ago distracted_mclean
b349c90cd93d nginx "nginx -g 'daemon of…" 44 minutes ago Exited (0) 43 minutes ago friendly_mahavira
99ecd2d1ec3b centos:latest "/bin/echo 'Hello wo…" About an hour ago Exited (0) About 1 minutes ago sandly_adesa
獲取容器的pid
docker inspect --format "{{.State.pid}}" webserver
進(jìn)入容器
nsenter --target 32439 --mount --uts --ipc --net --pid
默認(rèn)運(yùn)行一個容器時,系統(tǒng)會指定一個容器的名字名字隔箍,如果需要以特定名字啟動一個容器,可以運(yùn)行
docker run --name mycentos centos /bin/echo 'hello-w'
運(yùn)行一個指定名字的容器滨达,并且進(jìn)入對應(yīng)的容器中
-it實際上時-i和-t的參數(shù)的縮寫,-i是讓容器的終端打開捡遍,以便輸入東西。-t是開一個偽終端綁定到我們當(dāng)前的終端上
docker run --name mydocker -it centos /bin/bash
這樣画株,我們就進(jìn)入了centos容器里面了,新打開一個終端谓传,輸入docker ps可以看到正在運(yùn)行的名為mydocker的centos容器
MacBook:~ max$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
284a76eeb573 centos "/bin/bash" About an hour ago Up About an hour mydocker
MacBook:~ max$
下面的操作在centos容器內(nèi)部操作了哦
表示查看當(dāng)前的目錄
ls /
[root@284a76eeb573 /]# ls /
bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
查看當(dāng)前的進(jìn)程
ps aux
[root@284a76eeb573 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 11828 2908 pts/0 Ss 06:01 0:00 /bin/bash
root 14 0.0 0.1 51716 3364 pts/0 R+ 06:24 0:00 ps aux