2.1.命令帮助
Docker查看docker所有命令
Docker run --help查看具体命令使用说明
2.2.镜像
镜像网站
https://hub.docker.com/explore/
所有镜像都在这里
2.2.1.列出本机上的镜像
Docker images,
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
2.2.2.查找镜像
Docker search centos,
2.2.3.下载镜像
Docker pull 镜像:tag
Docker pull centos,默认tag是latest,latest是最新版本,如下图,即latest右边的centos7
如果拉取centos7,下载失败的,提示repository not found。
我是centos6.6,所以要用命令Docker pull centos:6.6
多阅读以下镜像的说明
2.2.4.运行镜像
Docker run hello-world
[root@localhost ~]# docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from hello-world
882673a3c694: Pull complete
83f0de727d85: Pull complete
Digest: sha256:4555e23a9cf5a1a216bd8b0d71b08a25e4144c2ecf6adb26df9620245ba99529
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:
- The Docker client contacted the Docker daemon.
- The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64) - The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading. - 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/engine/userguide/
先从网站下载镜像,然后输出内容,内容是镜像确定的
Docker run运行一个容器, 以centos:6.6镜像创建一个新容器,执行命令/bin/echo “Hello World”。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 11 days ago 1.848 kB
centos 6.6 abd4a196b77f 5 months ago 202.6 MB
[root@localhost ~]# docker run centos:6.6 /bin/echo “Hello World”
Hello World
2.3.镜像存储路径
/var/lib/docker
文件用户都是root, 所以切换到root用户
2.3.1.镜像
image/overlay2/imagedb/content/sha256下的文件和镜像对应
2.3.2.容器
Containers文件夹内文件名和docker ps -a的容器id对应
2.4.创建镜像
2.4.1.修改镜像
2.4.2.Dockerfile创建
dockerfile的命令都是大写的,比如:FROM、RUN等
2.5.容器
2.5.1.交互式容器
docker run -t -i centos:6.6 /bin/bash
-t 在新容器内指定一个终端,
-i 对容器内的标准输入stdin交互
[root@localhost ~]# docker run -t -i centos:6.6 /bin/bash
[root@6741ded7bac8 /]# pwd
/
[root@6741ded7bac8 /]# cat /proc/version
Linux version 2.6.32-504.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Oct 15 04:27:16 UTC 2014
[root@6741ded7bac8 /]# ls
bin etc lib lost+found mnt proc sbin srv tmp var
dev home lib64 media opt root selinux sys usr
[root@6741ded7bac8 /]# exit
Exit
[root@localhost ~]#
标志变成了[root@6741ded7bac8 /],说明进入容器了
在容器内执行cat /proc/version,ls等命令。
Exit退出容器
2.5.2.启动容器(后台模式)
docker run -d centos:6.6 /bin/sh -c “while true;do echo hello world;sleep 1;done”
-d, --detach=false Run container in background and print container ID,即后台运行
[root@localhost ~]# docker run -d centos:6.6 /bin/sh -c “while true;do echo hello world;sleep 1;done”
b76e968933da87cf9b7da5aa9019a8e9a6a03ca7da93805efe0345d996f6a0ac
打印出容器id。
Docker ps查看容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b76e968933da centos:6.6 "/bin/sh -c 'while t 9 seconds ago Up 9 seconds adoring_stallman
CONTAINER ID容器id,为b76e968933da。
NAMES容器名称,为adoring_stallman。
Docker logs 查看日志
后面用容器id的前面部分字符,或者容器名称的全部字符。
[root@localhost ~]# docker logs b7
hello world
hello world
[root@localhost ~]# docker logs adoring
Error response from daemon: no such id: adoring
[root@localhost ~]# docker logs adoring_stallman
hello world
hello world
2.5.3.停止容器
docker stop 容器id前面部分字符|容器名称全部字符
[root@localhost ~]# docker stop b7
b7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
2.5.4.重启容器
已经停止的容器,用Docker start adoring_stallman启动
正在运行的容器,用Docker restart adoring_stallman重启
2.5.5.删除容器
先停止容器,然后删除Docker rm adoring_stallman
2.6.Web容器
docker pull training/webapp,拉取python的web容器
[root@localhost ~]# docker pull training/webapp
latest: Pulling from training/webapp
e9e06b06e14c: Pull complete
a82efea989f9: Pull complete
37bea4ee0c81: Pull complete
07f8e8c5e660: Pull complete
23f0158a1fbe: Pull complete
0a4852b23749: Pull complete
7d0ff9745632: Pull complete
99b0d955e85d: Pull complete
33e109f2ff13: Pull complete
cc06fd877d54: Pull complete
b1ae241d644a: Pull complete
b37deb56df95: Pull complete
02a8815912ca: Already exists
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker run -d -P training/webapp python app.py运行容器,
-d,后台运行
-p, --publish=[] Publish a container’s port(s) to the host,将容器端口映射到主机上
[root@localhost ~]# docker run -d -P training/webapp python app.py
50f9e5186b77952f07eee2910ed7083b1458406668ab51e161c3573fff02a38f
[root@localhost ~]#