您好,登錄后才能下訂單哦!
練習環境:
操作系統:CentOS Linux release 7.5.1804 (Core)
虛擬工具:VMware
應用容器引擎: Docker version 1.13.1
一.鏡像
容器其實是在鏡像的最上面加了一層讀寫層,在運行容器里做的任何文件改動,都會寫到這個讀寫層。如果容器刪除了,最上面的讀寫也就刪除了,改動也就丟
失了。
1.配置成國內鏡像
方法一:docker官方
#vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com/"]
}
方法二:阿里
[root@docker /]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
2.鏡像常用命令操作
[root@docker /]# systemctl restart docker
#下載指定版本鏡像
[root@docker /]# docker pull nginx:1.12
#顯示所有鏡像
[root@docker /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v2 6e9545b1e2a2 17 minutes ago 1.22 MB
<none> <none> 26795fdb6ee8 18 minutes ago 1.22 MB
docker.io/tomcat latest 4e7840b49fad 6 days ago 529 MB
docker.io/nginx latest a1523e859360 6 days ago 127 MB
docker.io/python 3.5 0320ef7199ca 6 days ago 909 MB
docker.io/mysql latest c8ad2be69a22 6 days ago 465 MB
docker.io/php latest e66ae809d99a 6 days ago 405 MB
docker.io/httpd latest c5a012f9cf45 7 days ago 165 MB
docker.io/mongo latest bcef5fd2979d 11 days ago 386 MB
docker.io/ubuntu latest 72300a873c2c 11 days ago 64.2 MB
docker.io/centos latest 470671670cac 6 weeks ago 237 MB
docker.io/busybox latest 6d5fcfe5ff17 2 months ago 1.22 MB
#刪除所有無名稱的鏡像
[root@docker overlay2]# docker rmi $(docker images -f "dangling=true" -q)
#查看鏡像歷使
[root@docker overlay2]# docker image history centos
IMAGE CREATED CREATED BY SIZE COMMENT
470671670cac 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
<missing> 7 weeks ago /bin/sh -c #(nop) ADD file:aa54047c80ba300... 237 MB
#顯示鏡像詳細信息
[root@docker /]# docker image inspect nginx
......
[root@docker /]# docker image pull nginx:1.11
......
#刪除指定鏡像
[root@docker /]# docker image rm nginx:1.12
Untagged: nginx:1.12
Untagged: docker.io/nginx@sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Deleted: sha256:19f98a1ae65cc06a1f35e86ff4a7894c7f8fab2313a35503166233d470ff3752
Deleted: sha256:d626a8ad97a1f9c1f2c4db3814751ada64f60aed927764a3f994fcd88363b659
#對引用鏡像做個標記
[root@docker /]# docker tag nginx:1.11 nginx:v1
[root@docker /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx 1.11 5766334bdaa0 2 years ago 183 MB
nginx v1 5766334bdaa0 2 years ago 183 MB
#保存鏡像文件
[root@docker /]# docker image save nginx:1.11 > nginx1.11.tar
[root@docker /]# du -sh nginx1.11.tar
182M nginx1.11.tar
#加載鏡像文件
[root@docker /]# docker load <nginx1.11.tar
Loaded image: nginx:1.11
#導出容器文件
[root@docker /]# docker export 3c28790c0a41 > sky9899.tar
[root@docker /]# du -sh sky9899.tar
1.4M sky9899.tar
#導入容器文件
[root@docker /]# docker image import sky9899.tar sky9899:self
sha256:1302c27299d6326c256baeca85504fabb30b92abd480c234284fc567458fcac7
[root@docker /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sky9899 self 1302c27299d6 4 seconds ago 1.22 MB
3.容器常用操作命令
#運行容器,如果不存在,默認會下載
[root@docker sky9890]# docker container run -itd --name bs busybox
Unable to find image 'busybox:latest' locally
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
bdbbaa22dec6: Pull complete
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for docker.io/busybox:latest
3c28790c0a4187063324aafed93caf6d5589d17c9b458001c7b6232205a97907
#查看在使用的容器
[root@docker sky9890]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c28790c0a41 busybox "sh" 12 seconds ago Up 10 seconds bs
#執行bs容器
[root@docker sky9890]# docker container attach bs
/ # ls
bin dev etc home proc root run sys tmp usr var
/ # ls
bin dev etc home proc root run sys tmp usr var
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sh
8 root 0:00 ps -ef
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1296 (1.2 KiB) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ #exit #退出后,容器即停止了
[root@docker sky9890]# docker container start bs
bs
#執行容器bs
[root@docker sky9890]# docker container attach bs
/ # exit #退出后,容器容在運行
[root@docker sky9890]# docker container run -itd -e a=123 --name bs1 busybox
b14e43e4cbc3924af1f8425edc2cce43bf8682e1f7b3436f06bfa02cb5db0071
[root@docker sky9890]# docker exec -it bs1 sh
/ # ls
bin dev etc home proc root run sys tmp usr var
/ # echo $a
123
/ # exit
#指定容器端口
[root@docker sky9890]# docker container run -itd -p 8080:80 --name nginx01 nginx
2b6992269e6b9716c101d6cd8f11462b9bb05facbd7bae2b0b656baed2ff027d
[root@docker sky9890]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b6992269e6b nginx "nginx -g 'daemon ..." 43 seconds ago Up 41 seconds 0.0.0.0:8080->80/tcp nginx01
b14e43e4cbc3 busybox "sh" 4 minutes ago Up 4 minutes
#獲取nginx01容器日志
[root@docker sky9890]# docker logs nginx01
192.168.153.1 - - [04/Mar/2020:07:25:14 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
like Gecko" "-"
2020/03/04 07:25:15 [error] 6#6: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client:
192.168.153.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.153.141:8080"
192.168.153.1 - - [04/Mar/2020:07:25:15 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT
6.0; Trident/4.0)" "-"
[root@docker sky9890]# ls /var/lib/docker/containers/
#限制CPU和內存資源
[root@docker sky9890]# docker container run -itd --cpus 1 --memory 512m --name nginx02 nginx
b66722038a14775cb47b9d4f954c27373433cf89a6e0a544630903d90aba1a08
#容器資源使用統計
[root@docker sky9890]# docker container stats nginx02
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
nginx02 0.00% 1.375 MiB / 512 MiB 0.27% 648 B / 648 B 0 B / 0 B 2
#列出容器
[root@docker sky9890]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b66722038a14 nginx "nginx -g 'daemon ..." 8 minutes ago Up 8 minutes 80/tcp nginx02
2b6992269e6b nginx "nginx -g 'daemon ..." 23 minutes ago Up 23 minutes 0.0.0.0:8080->80/tcp nginx01
b14e43e4cbc3 busybox "sh" 27 minutes ago Up 27 minutes bs1
#顯示容器詳細信息
[root@docker sky9890]# docker container inspect b66722038a14
[root@docker sky9890]# docker exec -it nginx01 bash
root@2b6992269e6b:/#
[root@docker sky9890]# docker commit bs1 busybox:v2
sha256:6e9545b1e2a2254243ef2599bce5337aa5f67d57cef1d4aa7aa1622e3f1a4921
[root@docker sky9890]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox v2 6e9545b1e2a2 8 seconds ago 1.22 MB
docker.io/mongo latest bcef5fd2979d 11 days ago 386 MB
docker.io/ubuntu latest 72300a873c2c 11 days ago 64.2 MB
docker.io/centos latest 470671670cac 6 weeks ago 237 MB
docker.io/busybox latest 6d5fcfe5ff17 2 months ago 1.22 MB
#拷貝文件到bs1容器中
[root@docker /]# docker container cp sky9899.zip bs1:/root
[root@docker /]# docker container exec bs1 ls /root
sky9899.zip
#重啟容器
[root@docker /]# docker restart bs1
bs1
[root@docker /]# docker container exec bs1 ls /root
sky9899.zip
#查看容器日志
[root@docker /]# docker logs nginx01
[root@docker /]# docker port nginx01
80/tcp -> 0.0.0.0:8080
[root@docker /]# docker stats nginx02
#啟動容器
[root@docker /]# docker start b66722038a14
#停止容器
[root@docker /]# docker stop b66722038a14
#刪除容器
[root@docker /]# docker rm b66722038a14
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。