1. 使用镜像
1.1 在Docker Hub上查找镜像
我们查找一下之前博客里面,推送到Docker Hub里面的bage88/docker-demo
,能看到有2个仓库,第一个就是我们上次上传的镜像。点击“Details”进入到详细页面。
1.2 拉取镜像到本地机器
在我们本地虚拟机上执行如下命令,获取该镜像:
docker pull bage88/docker-demo
1.3 了解我们获取的镜像
下载完成之后,通过docker images
查看当前机器上已经有的镜像列表。
docker inspect IMAGE_ID
或者docker inspect IMAGE_NAME
查看镜像详细信息。 root@ubuntu-512mb-sfo2-01-gfw:~# docker inspect 3172d41e1212[ { "Id": "sha256:3172d41e12122d18216cd3179292e3840963e3d80e6ad70046c7270e85342f11", "RepoTags": [ "bage88/docker-demo:0.0.1" ], "RepoDigests": [ "bage88/docker-demo@sha256:26deeeb936586c34f97ece440dc2618984f887e935b5fb81856862d64e9c79a7" ], "Parent": "sha256:05a60462f8bafb215ddc5c20a364b5fb637670200a74a5bb13a1b23f64515561", "Comment": "", "Created": "2017-04-26T04:58:45.421242925Z", "Container": "50877050a5f2dc0a9c53adeba41f0760fcecca3e0bb9db3ad3f906dbdab9df32", "ContainerConfig": { "Hostname": "6b3797ab1e90", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "443/tcp": {}, "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.11.5-1~jessie" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ADD dir:e9b5928812b2520f4eaf558074546aeffcae14070c6b3aff800660ed4a6bef65 in /usr/share/nginx/html " ], "ArgsEscaped": true, "Image": "sha256:05a60462f8bafb215ddc5c20a364b5fb637670200a74a5bb13a1b23f64515561", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": [], "Labels": {} }, "DockerVersion": "17.03.1-ce", "Author": "", "Config": { "Hostname": "6b3797ab1e90", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "443/tcp": {}, "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.11.5-1~jessie" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "sha256:05a60462f8bafb215ddc5c20a364b5fb637670200a74a5bb13a1b23f64515561", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": [], "Labels": {} }, "Architecture": "amd64", "Os": "linux", "Size": 181557726, "VirtualSize": 181557726, "GraphDriver": { "Name": "aufs", "Data": null }, "RootFS": { "Type": "layers", "Layers": [ "sha256:fe4c16cbf7a4c70a5462654cf2c8f9f69778db280f235229bd98cf8784e878e4", "sha256:c4a8b7411af46497558e12eb2237a07a9cf31b3d3f9c069d3adcf60e98a092d0", "sha256:3f117c44afbb0a90d7c5d5db1375280fce40b81f8c3151a684cdd20192c30705", "sha256:9d59143b04c08194884c22fc72413a22b66cb7715506ca1be2cccbc9babd19bc" ] } }]
2. 使用容器
2.1 以bash shell
方式运行容器
# -i :打开并保持容器的标准输入# -t :分配pseudo-TTY终端并绑定到容器的标准输入上docker run -t -i bage88/docker-demo /bin/bash
进入容器中,我们可以查看到我们之前复制文件到镜像里面的内容。执行命令
root@ubuntu-512mb-sfo2-01-gfw:~# docker run -t -i bage88/docker-demo /bin/bashroot@94a1f3581ebb:/# cd /usr/share/nginx/htmlroot@94a1f3581ebb:/usr/share/nginx/html# ls -ltotal 12-rw-r--r-- 1 root root 537 Oct 11 2016 50x.htmldrwxr-xr-x 2 root root 4096 Apr 19 01:10 images-rw-r--r-- 1 root root 669 Apr 19 01:10 index.htmlroot@94a1f3581ebb:/usr/share/nginx/html#
2.2 以daemon
进程方式运行容器
我们常见的Web应用,应该是让容器一直运行,所以需要创建以daemon
方式运行容器。
# -d :daemon方式运行容器,退出终端,程序还在运行# -p :允许指定Host主机一个端口和容器内部一个端口进行隐射。docker run -d -p 8010:80 bage88/docker-demo
在浏览器访问Host主机的IP+端口,结果如图所示:
2.3 查看容器
查看当前运行的所有容器
docker ps -a
2.4 删除容器
删除容器的命令:
docker rm CONTAINER_ID