[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.配置镜像加速器并重启服务
[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 -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 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