安装
apt install curl
curl -fsSL https://test.docker.com -o test-docker.sh
sudo sh test-docker.sh
//检测安装是否成功
$ sudo docker run hello-world
输出以下内容表示安装成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
简单实例
runoob@runoob:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
输出:Hello world
我们通过 docker 的两个参数 -i -t,让 docker 运行的容器实现"对话"的能力:
runoob@runoob:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
各个参数解析:
-
-t: 在新容器内指定一个伪终端或终端。
-
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。
注意第二行 root@0123ce188bd8:/#,此时我们已进入一个 ubuntu15.10 系统的容器
退出对话
exit 或者 ctrl+d
使用以下命令创建一个以进程方式运行的容器
runoob@runoob:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
在宿主主机内使用 docker logs 命令,查看容器内的标准输出:
runoob@runoob:~$ docker ps //查看容器信息
CONTAINER ID IMAGE COMMAND ...
5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t…" ...
runoob@runoob:~$ docker logs 5917eac21c36
docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
runoob@runoob
启动容器
以下命令使用 ubuntu 镜像启动一个容器,参数为以命令行模式进入该容器:
$ docker run -it ubuntu /bin/bash
使用 docker start 启动一个已停止的容器:
$ docker start b750bbbcfd88
停止容器的命令如下:
$ docker stop <容器 ID>
停止的容器可以通过 docker restart 重启:
$ docker restart <容器 ID>
下面演示了使用 docker attach 命令。
$ docker attach 1e560fca3906
注意: 如果从这个容器退出,会导致容器的停止。
exec 命令
下面演示了使用 docker exec 命令。
docker exec -it 243c32535da7 /bin/bash
注意: 如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec 的原因。
导出容器
如果要导出本地某个容器,可以使用 docker export 命令。
$ docker export 1e560fca3906 > ubuntu.tar
导出容器 1e560fca3906 快照到本地文件 ubuntu.tar。
这样将导出容器快照到本地文件。
导入容器快照
可以使用 docker import 从容器快照文件中再导入为镜像,以下实例将快照文件 ubuntu.tar 导入到镜像 test/ubuntu:v1:
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
此外,也可以通过指定 URL 或者某个目录来导入,例如:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
删除容器
删除容器使用 docker rm 命令:
$ docker rm -f 1e560fca3906
下面的命令可以清理掉所有处于终止状态的容器。
$ docker container prune