一、docker的安裝
1.依賴包安裝
yum install -y yum-utils device-mapper-persistent-data lvm2
2.添加yum源
yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
#配置爲安裝最新版Docker CE
3.安裝docker
yum install docker-ce
*官方爲了簡化安裝流程提供了便捷的安裝腳本
curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun
4.啓動docker
systemctl enable docker
systemctl start docker
5.建立docker用戶和組
6.測試docker是否安裝完成
[docker@dockertest ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
7.配置鏡像加速器並重啓服務
cat /etc/docker/daemon.json
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
systemctl daemon-reload
systemctl restart docker
二、使用鏡像
1.獲取鏡像
[root@dockertest ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
22dc81ace0ea: Pull complete
1a8b3c87dba3: Pull complete
91390a1c435a: Pull complete
07844b14977e: Pull complete
b78396653dae: Pull complete
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:16.04
2.查看獲取的鏡像
[root@dockertest ~]# docker image ls
#使用--help可以查看參數選項,比如自己定義顯示的列
[root@dockertest ~]# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID REPOSITORY TAG
f975c5035748 ubuntu 16.04
f2a91732366c hello-world latest
#查看空間的佔用
[root@dockertest ~]# docker system df
3.刪除鏡像
#刪除本地鏡像可以使用鏡像短ID、長ID、鏡像名或鏡像摘要來刪除
[root@dockertest ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 f975c5035748 7 days ago 112MB
centos latest 2d194b392dd1 8 days ago 195MB
mysql latest 5d4d51c57ea8 2 weeks ago 374MB
nginx latest e548f1a579cf 3 weeks ago 109MB
hello-world latest f2a91732366c 3 months ago 1.85kB
[root@dockertest ~]# docker image rm 5d4
[root@dockertest ~]# docker image rm nginx
[root@dockertest ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 f975c5035748 7 days ago 112MB
centos latest 2d194b392dd1 8 days ago 195MB
hello-world latest f2a91732366c 3 months ago 1.85kB
#還可以使用docker image ls命令來配合
docker image rm $(docker image ls -q ubuntu)
docker image rm $(docker image ls -q -f before=centos)
三、容器的操作
1.容器的啓動
①新建並啓動
[root@dockertest ~]# docker run ubuntu:16.04 /bin/echo "hello world"
hello world
②啓動一個bash終端,允許用戶進行交互。
[root@dockertest ~]# docker run -ti ubuntu:16.04 /bin/bash
root@edf8af896cc0:/# pwd
/
#選項 '-t' 開啓一個僞終端,'-i' 讓容器的標準輸入保持打開。
③後臺運行
[root@dockertest ~]# docker run -d ubuntu:16.04 /bin/sh -c "while true; do echo hello world; sleep 10; done"
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37
#此時容器會在後臺運行並不會把輸出的結果打印到宿主機上面,可以使用docker logs查看輸出信息。
[root@dockertest ~]# docker logs cab41
hello world
hello world
hello world
hello world
2.容器的終止
[root@dockertest ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cab4112e91fb ubuntu:16.04 "/bin/sh -c 'while t…" 5 minutes ago Up 5 minutes
[root@dockertest ~]# docker container stop cab41
cab41
#查看終止的容器
[root@dockertest ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cab4112e91fb ubuntu:16.04 "/bin/sh -c 'while t…" 7 minutes ago Exited (137) About a minute ago angry_wescoff
#可以使用docker container start來啓動處於終止狀態的容器
[root@dockertest ~]# docker container start cab41
cab41
[root@dockertest ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cab4112e91fb ubuntu:16.04 "/bin/sh -c 'while t…" 10 minutes ago Up 5 seconds angry_wescoff
[root@dockertest ~]# docker attach 4417
root@4417b64c1c7f:/# pwd
/
root@4417b64c1c7f:/# exit
exit
3.進入容器
①attack(輸入exit會導致容器的終止)
[root@dockertest ~]# docker run -itd ubuntu
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
[root@dockertest ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4417b64c1c7f ubuntu "/bin/bash" 8 seconds ago Up 7 seconds hungry_easley
②exec(輸入exit不會導致容器的終止)
[root@dockertest ~]# docker run -itd ubuntu
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
[root@dockertest ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5276352cf8d1 ubuntu "/bin/bash" 8 seconds ago Up 8 seconds blissful_edison
[root@dockertest ~]# docker exec -it 5276 bash
root@5276352cf8d1:/# whoami
root
root@5276352cf8d1:/# exit
exit
#也可以使用組合鍵退出,仍然保持容器運行,我們可以隨時回來到這個bash中來,組合鍵是 Ctrl-p Ctrl-q,先同時按下Ctrl和p,再按Ctrl和q。就可以退出到我們的宿主機了。
4.容器的導出和導入
①導出
[root@dockertest ~]# docker export 4417 > test.tar
[root@dockertest ~]# ls
test.tar
②導入
[root@dockertest ~]# cat test.tar | docker import - test/ubuntu:v1.1
sha256:fb0e8d9dcaba2d018ec1fe26394f3b4989db55ace695d1c685c5e753fbe8ed9e
[root@dockertest ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.1 fb0e8d9dcaba 3 seconds ago 85.8MB
#也可以通過指定URL或者某個目錄來導入
[root@dockertest ~]# docker import /root/test.tar test/ubuntu:v1.2
sha256:e33803dd759481e27cd807536f28fe8ab0cfddd9c3bae2a15b95ea8b7645172b
[root@dockertest ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.2 e33803dd7594 3 seconds ago 85.8MB
test/ubuntu v1.1 fb0e8d9dcaba 3 minutes ago 85.8MB
5.刪除容器
①刪除一個處於終止狀態的容器
[root@dockertest ~]# docker container rm edf8af
edf8af
#如果要刪除一個運行中的容器,可以添加 -f 參數來刪除。
②清除所有處於終止狀態的容器
[root@dockertest ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
99aee81f7684d59c72d2c1bbc933fd052f2158e833fb08988803108de613de24
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37
#學習文檔地址:https://github.com/yeasy/docker_practice/blob/master/SUMMARY.md