安装命令:
yum -y install docker
启动 Docker 后台服务:
<span style="color:#000000">service docker start</span>
运行hello-world命令:
[root@localhost ~]# docker run ansible/centos7-ansible:latest /bin/echo 'hello'
hello
[root@localhost ~]#
各个参数解析:
-
docker: Docker 的二进制执行文件。
-
run:与前面的 docker 组合来运行一个容器。
-
ansible/centos7-ansible:latest指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
-
/bin/echo "Hello world": 在启动的容器里执行的命令
以上命令完整的意思可以解释为:Docker 以ansible/centos7-ansible:latest镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。
交互式启动方式:
[root@localhost ~]# docker run -i -t ansible/centos7-ansible:latest /bin/bash
[root@367b51f5b233 ansible]#
[root@367b51f5b233 ansible]#
各个参数解析:
-
-t:在新容器内指定一个伪终端或终端。
-
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
我们可以通过运行exit命令或者使用CTRL+D来退出容器。
后台启动模式:
[root@localhost ~]# docker run -d ansible/centos7-ansible:latest /bin/bash -c "while true; do echo hello world; sleep 1; done"
20abcde0437d6d27d3536cd4e3de3054be71f6d3d18380218fe5fe6167a70911
20abcde0437d6d27d3536cd4e3de3054be71f6d3d18380218fe5fe6167a70911
<span style="color:#333333"><span style="color:#333333">[root@localhost ~]# </span>
</span>
查看后台命令:
docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20abcde0437d ansible/centos7-ansible:latest "/bin/bash -c 'whi..." 2 minutes ago Up 2 minutes amazing_sammet
8c17fc4c6d6f training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp determined_nightingale
[root@localhost ~]#
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20abcde0437d ansible/centos7-ansible:latest "/bin/bash -c 'whi..." 2 minutes ago Up 2 minutes amazing_sammet
8c17fc4c6d6f training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp determined_nightingale
[root@localhost ~]#
查看日志:docker logs id/name
[root@localhost ~]# docker logs 20abcde0437d
hello world
hello world
hello world
hello world
hello world
hello world
hello world
停止命令:docker stop
[root@localhost ~]# docker stop 20abcde0437d
20abcde0437d
[root@localhost ~]#
查看镜像:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest f2a91732366c 3 months ago 1.85 kB
docker.io/ansible/centos7-ansible latest 688353a31fde 15 months ago 447 MB
docker.io/training/webapp latest 6fae60ef3446 2 years ago 349 MB
[root@localhost ~]#
各个选项说明:
-
REPOSITORY:表示镜像的仓库源
-
TAG:镜像的标签
-
IMAGE ID:镜像ID
-
CREATED:镜像创建时间
-
SIZE:镜像大小
查找镜像命令:docker search name
[root@localhost ~]# docker search http
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/httpd The Apache HTTP Server Project 1569 [OK]
docker.io docker.io/haproxy HAProxy - The Reliable, High Performance T... 913 [OK]
docker.io docker.io/jruby JRuby (http://www.jruby.org) is an impleme... 62 [OK]
docker.io docker.io/hypriot/rpi-busybox-httpd Raspberry Pi compatible Docker Image with ... 40
[root@localhost ~]#
获取docker镜像:docker pull name
[root@localhost ~]# docker pull httpd
Using default tag: latest
Trying to pull repository docker.io/library/httpd ...
latest: Pulling from docker.io/library/httpd
f2b6b4884fc8: Pull complete
b58fe2a5c9f1: Pull complete
e797fea70c45: Pull complete
6c7b4723e810: Pull complete
02074013c987: Pull complete
a4a11b801d86: Pull complete
70d17a98bee0: Pull complete
Digest: sha256:8359424a58cf59f1ea6a1a55e3974d5d569a510ebec0004357cf200adce5f27a
Status: Downloaded newer image for docker.io/httpd:latest
[root@localhost ~]#