Docker-简单入门与实践
根据Docker Tutorial Doc进行跟随学习
推荐网站:
Docker — 从入门到实践
Docker Documents
Docker For Beginners
Awesome Docker
Windows安装过程详见 https://docs.docker.com/desktop/setup/install/windows-install/
Mac安装过程详见 https://docs.docker.com/desktop/setup/install/mac-install/
Linux(Ubuntu) 安装过程详见 https://docs.docker.com/engine/install/ubuntu/
安装验证
docker run hello-world
镜像查看
docker images
docker pull busybox
busybox为images,container在images基础上创建
docker run busybox echo "Hello from busybox"
docker run 通过镜像busybox加载container,并在container中执行echo ...命令,然后该container会退出
原文辅助理解:
When you call
run, the Docker client finds the image (busybox in this case), loads up the container and then runs a command in that container.Imagine booting up a virtual machine, running a command and then killing it.
docker ps
该命令用于查看当前正在运行的containers
docker ps -a
该命令 -a 用于指示查看所有containers(包括运行过的,存在过的,-a == --all)
docker run -it busybox sh
该命令 -it 用于container,使操作者可以在container上进行多道命令的执行
原文辅助理解:
Running the
runcommand with the-itflags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want. Take some time to run your favorite commands.
docker rm ID
通过ID删除创建过的stray container,container在创建使用并退出后会成为stray(流浪) container,这些container会占用disk space(硬盘空间),通过 docker ps -a 查看stray container(status = exited)的CONTAINER ID并替换掉ID便可以删除
docker rm $(docker ps -a -q -f status=exited)
如果存在多个且不需要的stray container,可以通过此命令清除所有的stray container
docker container prune
在较新版本的docker中,此命令可以实现上文同样的效果
docker rmi [OPTIONS] IMAGE [IMAGE...]
此命令用于删除不需要的image
此板块搬运对应 Docker Tutorial Doc 的Terminology小节
- Images - The blueprints of our application which form the basis of containers. In the demo above, we used the
docker pullcommand to download the busybox image.- Containers - Created from Docker images and run the actual application. We create a container using
docker runwhich we did using the busybox image that we downloaded. A list of running containers can be seen using thedocker pscommand.- Docker Daemon - The background service running on the host that manages building, running and distributing Docker containers. The daemon is the process that runs in the operating system which clients talk to.
- Docker Client - The command line tool that allows the user to interact with the daemon. More generally, there can be other forms of clients too - such as Kitematic which provide a GUI to the users.
- Docker Hub - A registry of Docker images. You can think of the registry as a directory of all available Docker images. If required, one can host their own Docker registries and can use them for pulling images.
在Docker上运行静态网页服务器
docker run --rm -it prakhar1989/static-site
解释:
--rm 在container退出时自动删除
--it 为container建立一个可交互的命令行终端
在一般情况下,若docker run的执行对象不存在,docker client会首先尝试在registry(相当于docker的remote library) fetch 一份然后执行docker run指令
此命令执行后,当shell出现 Nginx is running... 则视为成功
已知Nginx 需要端口数据才能访问本地代理服务器,我们则需要下面命令使container运行在后台而不影响可交互终端
docker run -d -P --name static-site prakhar1989/static-site
解释:
-d (--detached) detached mode,使container运行在后台,隔离运行
-P (--Publish-all) 将container的所有exposed接口映射到本地随机接口
--name 为container取名(方便操纵container)
原文辅助理解:
In the above command,
-dwill detach our terminal,-Pwill publish all exposed ports to random ports and finally--namecorresponds to a name we want to give.
docker port static-site
port用于显示static-site(container)的exposed port在本地的映射
docker run -p 8888:80 IMAGE
你也可以如上指定映射端口
docker stop static-site
通过以上命令停止后台运行的container
个人对container的补充
docker start CONTAINER
该命令用于启动指定的container,而不是从image上再次创建一个新的container
docker tag image_build_id image_name
该命令表示将image_build_id(一个不好识别的镜像名字ID)取别名为image_name
docker diff CONTAINER
该命令用于查看CONTAINER与原IMAGE的差异,结果中C表示修改,A表示增加
docker start [Container Name | Container ID]
启动一个Container
docker exec -it [Container Name | Container ID] /bin/bash
用于在终端启动一个已经在运行的container
虚悬镜像:
docker image ls -f dangling=true
显式列出虚悬镜像,或通过docker image ls不进行筛选也可以查看到
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 00285df0df87 5 days ago 342 MB
得到仓库名和标签均为<none>的镜像
引用解释: 这个镜像原本是有镜像名和标签的,原来为 mongo:3.2,随着官方镜像维护,发布了新版本后,重新 docker pull mongo:3.2 时,mongo:3.2 这个镜像名被转移到了新下载的镜像身上,而旧的镜像上的这个名称则被取消,从而成为了 <none>。除了 docker pull 可能导致这种情况,docker build 也同样可以导致这种现象。由于新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为 <none> 的镜像。
通过
docker image prune
删除虚悬镜像
Dockerfile Note
前提概述:
docker commit 这条指令做了什么?
container在运行时,所有的改动都会存储在该container的存储层里,这些存储层位于container的最上层,其下层为image的存储层;docker commit的作用就是把container的存储层转化为image的存储层,换一种说法就是,在原有的image的存储层上加入container的新增存储层,并构成新的镜像.
慎用docker commit,在非个人简单的开发实验环境上以外,尽量减少使用commit命令;
通过diff命令可以查看container与原有image的差异,可以发现一个简单的命令都会改变很多文件,这些文件的改变在commit后均不可见,该commit产生的镜像称之为"黑箱镜像",除了创建者以外都不可能知道该黑箱是如何产生的,这种黑箱镜像的维护工作是非常困难的.
引用:
使用 docker commit 意味着所有对镜像的操作都是黑箱操作,生成的镜像也被称为 黑箱镜像,换句话说,就是除了制作镜像的人知道执行过什么命令、怎么生成的镜像,别人根本无从得知。而且,即使是这个制作镜像的人,过一段时间后也无法记清具体的操作。这种黑箱镜像的维护工作是非常痛苦的。
而且,回顾之前提及的镜像所使用的分层存储的概念,除当前层外,之前的每一层都是不会发生改变的,换句话说,任何修改的结果仅仅是在当前层进行标记、添加、修改,而不会改动上一层。如果使用 docker commit 制作镜像,以及后期修改的话,每一次修改都会让镜像更加臃肿一次,所删除的上一层的东西并不会丢失,会一直如影随形的跟着这个镜像,即使根本无法访问到。这会让镜像更加臃肿。
一个简单的Dockerfile及其指令解释(该文件无实际意义)
FROM debian:latest #FROM (IMAGE:VERSION) 从基础镜像上开始修改
RUN touch test \ #RUN 从bash/sh上运行指令,通过 \ 换行及 && 达成链式指令执行,每一个RUN都会重新建立一个容器
&& echo "Hello World" > test \
&& cat test
#每执行一条指令都是新增一层存储层,所以尽量将指令在一条指令内写完
RUN apt update \
&& apt install g++ cmake sudo
COPY package.json /usr/src/app #将当前package.json复制到/usr/src/app下
其他比较重要的指令为 CMD,ENV,WORKDIR,EXPOSE,USER等,查阅文档即可
docker build -t imageName .
以当前上下文目录构建(build)名为imageName的docker image,docker客户端会自动寻找Dockerfile,也可以进行指定,该上下文目录内所有文件都会被上传以供Dockerfile对image进行操作,包括COPY,ADD等;所有需要的文件都需要添加到此目录下.与WORKDIR区分,WORKDIR是对容器内当前工作目录进行环境切换.

浙公网安备 33010602011771号