Docker Helloword 镜像练习
首先介绍下命令:docker run 镜像名
我们就用 docker run hello-world为例子
[root@bogon ~]# 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/
从上面可以很清楚的看到,没有找到镜像hello-world:latest (默认不指定版本号,也就是tag值,就是latest) 。那么当本地没有找到这个hello-world镜像的时候,这是docker后台进程就会去访问Registry(注册仓库,也就是阿里云那边的仓库),然后从那里进行下载这个镜像。并且运行
docker run到底做了什么
首先会去本机寻找要run的镜像,若本机不存在该镜像,就去DockerHub(现在替换成了阿里云)查询该镜像,如果dockerHub/阿里云 也找不到这个镜像,那就返回失败信息。否则就下载该镜像到本地,然后以该镜像为模板运行生成容器实例放在docker中。
为什么Docker比VM快很多?
dokcer是毫秒级别的,vm启动需要1分钟多,原因在于docker去除掉了很多vm上的组件
docker命令
我们可以使用 docker --help
来查看docker命令的使用
[root@bogon ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon
(overrides DOCKER_HOST env var and default context set
with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
容器生命周期管理
容器操作
容器rootfs命令
镜像仓库
本地镜像管理
info|version
这里抽取处几个命令讲解:
- docker images 列出当前主机可以运行的镜像
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16 0dac5b41d811 4 weeks ago 126MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
我们可以使用 docker images -qa 来查询说有的镜像id
[root@bogon ~]# docker images -qa
0dac5b41d811
fce289e99eb9
我们还可以使用docker images --digests 来显示镜像的摘要信息
[root@bogon ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZ
E
nginx 1.16 sha256:0d0af9bc6ca2db780b532a522a885bef7fcaddd52d11817fc4cb6a3ead3eacc0 0dac5b41d811 4 weeks ago 126
MB
hello-world latest sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f fce289e99eb9 9 months ago 1.8
4kB
如果我们想要让Image id不使用缩写,也就是完全展示image的id,我们可以使用docker images --no-trunc
显示完整的镜像
[root@bogon ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16 sha256:0dac5b41d811ca6e1bfe68d31bec5fb1f5c37485b741674619a1a2a3dec5cc0e 4 weeks ago 126MB
- docker search 镜像名
可以去docker.hub上自行搜索。搜索的结果和这个命令返回一致
[root@bogon ~]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomca… 2538 [OK]
...
我们可以使用 docker search -s 30 镜像名
可以获取排start数前30的镜像
- docker pull 镜像名: [版本]
这里使用docker进行下载tomcat
docker pull tomcat:版本号
-
docker rmi -f 镜像名: [版本] 可以强制删除指定版本的镜像。
-
docker rmi -f 镜像名:[版本] 镜像名2:[版本2] … 可以一次性删除多个镜像
-
docker rmi -f $(docker images -qa)
将$(docker images -qa)
返回的所有 images Id 全部删除
docker run
模式 docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS说明:
- -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
- -d: 后台运行容器,并返回容器ID;
- -i: 以交互模式运行容器,通常与 -t 同时使用;
- -P: 随机端口映射,容器内部端口随机映射到主机的高端口
- -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
- -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
- –name=“nginx-lb”: 为容器指定一个名称;
- –dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
- –dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
- -h “mars”: 指定容器的hostname;
- -e username=“ritchie”: 设置环境变量;
- –env-file=[]: 从指定文件读入环境变量;
- –cpuset=“0-2” or --cpuset=“0,1,2”: 绑定容器到指定CPU运行;
- **-m 😗*设置容器使用内存最大值;
- –net=“bridge”: 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
- –link=[]: 添加链接到另一个容器;
- –expose=[]: 开放一个端口或一组端口;
- –volume , -v: 绑定一个卷
实例
使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。
docker run --name mynginx -d nginx:latest
使用镜像nginx:latest以后台模式启动一个容器,并将容器的80端口映射到主机随机端口。
docker run -P -d nginx:latest
使用镜像 nginx:latest,以后台模式启动一个容器,将容器的 80 端口映射到主机的 80 端口,主机的目录 /data 映射到容器的 /data。
docker run -p 80:80 -v /data:/data -d nginx:latest
绑定容器的 8080 端口,并将其映射到本地主机 127.0.0.1 的 80 端口上。
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
runoob@runoob:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/#
ctrl+p+q 返回宿主机,这个作用是当你使用
docker run -it centos
进入到centos docker镜像的时候
列出容器:docker ps [option]
查看运行中的容器有哪些
OPTIONS说明:
- -a :显示所有的容器,包括未运行的。
- -f :根据条件过滤显示的内容。
- –format :指定返回值的模板文件。
- -l :显示最近创建的容器。
- -n :列出最近创建的n个容器。
- –no-trunc :不截断输出。
- -q :静默模式,只显示容器编号。
- -s :显示总的文件大小。
示例:
-
列出最近创建的5个容器信息。
docker ps -n 5
-
列出所有创建的容器ID。
runoob@runoob:~$ docker ps -a -q
09b93464c2f7
b8573233d675
b1a0703e41e7
f46fb1dec520
a63b4a5597de
6a4aa42e947b
de7bb36e7968
43a432b73776
664a8ab1a585
ba52eb632bbd
关闭docker容器的两种办法
-
docker kill 容器id
强制中断容器,不管是否在执行
2.docker stop 容器id
温柔的中断容器,可以正常的让容器进行关闭
删除已停止的容器
docker ps rm 容器Id/容器名
在本机上存在的所有容器id: docker ps -qa
一次性删除多个容器 : docker rm -f $(docker ps -qa)