关于docker:docker常用命令

5次阅读

共计 3371 个字符,预计需要花费 9 分钟才能阅读完成。

Docker 根本命令

1. 查看 docker 信息(version、info)

# 查看 docker 版本  
$docker version  
  
# 显示 docker 零碎的信息  
$docker info  

2. 对 image 的操作(search、pull、images、rmi、history)

# 检索 image  
$docker search image_name  
  
# 下载 image  
$docker pull image_name  
  
# 列出镜像列表; 
# -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
$docker images  
  
# 删除一个或者多个镜像; 
# -f, --force=false Force; --no-prune=false Do not delete untagged parents  
$docker rmi image_name  
  
# 显示一个镜像的历史; 
# --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
$docker history image_name  

3. 启动容器(run)

docker 容器能够了解为在沙盒中运行的过程。这个沙盒蕴含了该过程运行所必须的资源,包含文件系统、零碎类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你须要在沙盒中运行一个过程来启动某一个容器。这个过程是该容器的惟一过程,所以当该过程完结的时候,容器也会齐全的进行。

# 在容器中运行 "echo" 命令,输入 "hello word"  
$docker run image_name echo "hello word"  
  
# 交互式进入容器中  
$docker run -i -t image_name /bin/bash  
  
  
# 在容器中装置新的程序  
$docker run image_name apt-get install -y app_name  

4. 查看容器(ps)

# 列出以后所有正在运行的 container  
$docker ps  
# 列出所有的 container  
$docker ps -a  
# 列出最近一次启动的 container  
$docker ps -l  

5. 保留对容器的批改(commit)

当你对某一个容器做了批改之后(通过在容器中运行某一个命令),能够把对容器的批改保留下来,这样下次能够从保留后的最新状态运行该容器。

# 保留对容器的批改; -a, --author=""Author; -m, --message="" Commit message  
$docker commit ID new_image_name  

Note:image 相当于类,container 相当于实例,不过能够动静给实例装置新软件,而后把这个 container 用 commit 命令固化成一个 image。

6. 对容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)

# 删除所有容器  
$docker rm `docker ps -a -q`  
  
# 删除单个容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container  
$docker rm Name/ID  
  
# 进行、启动、杀死一个容器  
$docker stop Name/ID  
$docker start Name/ID  
$docker kill Name/ID  
  
# 从一个容器中取日志; 
$docker logs Name/ID 
# --since : 指定了输入日志开始日期,即只输入指定日期之后的日志 -f: -- 查看实时日志; -t: -- 查看日志产生的日期  --tail=10 : 查看最初的 10 条日志,可本人设定。# 列出一个容器外面被扭转的文件或者目录,list 列表会显示出三种事件,A 减少的,D 删除的,C 被扭转的  
$docker diff Name/ID  
  
# 显示一个运行的容器外面的过程信息  
$docker top Name/ID  
  
# 从容器外面拷贝文件 / 目录到本地一个门路  
$docker cp Name:/container_path to_path  
$docker cp ID:/container_path to_path  
  
# 重启一个正在运行的容器; 
# -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10  
$docker restart Name/ID  
  
# 附加到一个运行的容器下面; 
# --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process  
$docker attach ID  

Note:attach 命令容许你查看或者影响一个运行的容器。你能够在同一时间 attach 同一个容器。你也能够从一个容器中脱离进去,是从 CTRL-C。

7. 保留和加载镜像(save、load)

当须要把一台机器上的镜像迁徙到另一台机器的时候,须要保留镜像与加载镜像。

# 保留镜像到一个 tar 包; -o, --output="" Write to an file  
$docker save image_name -o file_path  
# 加载一个 tar 包格局的镜像; -i, --input="" Read from a tar archive file  
$docker load -i file_path  
  
# 机器 a  
$docker save image_name > /home/save.tar  
# 应用 scp 将 save.tar 拷到机器 b 上,而后:$docker load < /home/save.tar  

示例:

docker save 00ead811e8ae -o ./portainer.tar

docker load -i fastdfs.tar

8. 登录 registry server(login)

# 登陆 registry server; -e, --email=""Email; -p, --password="" Password; -u, --username="" Username  
$docker login  

9. 公布 image(push)

# 公布 docker 镜像  
$docker push new_image_name  

10. 依据 Dockerfile 构建出一个容器

# build  
# --no-cache=false Do not use cache when building the image  
# -q, --quiet=false Suppress the verbose output generated by the containers  
# --rm=true Remove intermediate containers after a successful build  
# -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success  
$docker build -t image_name Dockerfile_path  

11. 批改镜像名称

docker tag server:latest myname/server:latest 
or
docker tag d583c3ac45fd myname/server:latest

罕用的命令

获取容器 / 镜像的元数据。

Docker inspect ID

进入 Docker 容器外部

docker exec -it ID sh
当想退出容器时,能够通过 exit 命令或 Ctrl+d 来退出终端,而所创立的容器立即终止。

从一个容器中取日志;

$docker logs Name/ID

–since , 指定了输入日志开始日期,即只输入指定日期之后的日志
-f, – 查看实时日志;
-t, – 查看日志产生的日期
–tail=10 , 查看最初的 10 条日志,可本人设定。

依据容器打包镜像

打包镜像的命令格局:

docker commit -m “ 备注 ” -a “ 作者名 ” 容器 ID 容器名称

示例:

docker commit -m "111" -a "李杰" 141f15df05ff test
正文完
 0