首页 > 其他分享 >docker常用命令

docker常用命令

时间:2022-11-26 15:46:41浏览次数:58  
标签:容器 container 常用命令 镜像 docker root localhost

docker的常用命令

帮助命令

docker version #显示docker的版本信息
docker info#显示docker的系统的详细信息
docker  命令   ---help  #帮助命令

帮助文档的地址:https://docs.docker.com/reference/

镜像命令

docker images 查看镜像

[root@localhost ~]# docker images
REPOSITORY    TAG            IMAGE ID       CREATED        SIZE
alpine        xiaowangc1.0   b5226f57471e   3 weeks ago    249MB
alpine        latest         e66264b98777   8 weeks ago    5.53MB
hello-world   latest         feb5d9fea6a5   9 months ago   13.3kB
#解释
REPOSITORY   仓库名称  
TAG   仓库标签
IMAGE ID   仓库ID
CREATED    镜像的创建的时间
SIZE      镜像的大小

#可选项
-a, --all              #列出所有镜像 
 -q, --quiet           #只显示ID


docker search搜索命令

[root@localhost ~]# docker search mysql
NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql        MySQL is a widely used, open-source relation…   12887     [OK]
mariadb     MariaDB Server is a high performing open sou…   4942      [OK]

# --filter 筛选条件
   --filter =STARS=3000 #筛选出stars值在3000以上的
   

dokcer pull 下载镜像

#下载 镜像  docker pull 镜像名称 [:tag]
[root@localhost ~]# docker pull mysql
Using default tag: latest    #如果不写tag,默认为latset
latest: Pulling from library/mysql
72a69066d2fe: Pull complete  #分层下载
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  #真实地址
[root@localhost ~]# docker pull mysql:5.7  #指定mysql 5.7版本下载

5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

image.png
docker rmi 删除镜像

[root@localhost ~]# docker rmi -f  镜像ID    目标指定镜像
[root@localhost ~]# docker rmi -f  镜像 ID   容器ID     删除多个镜像
[root@localhost ~]# docker rmi -f  $(docker images -aq)    删除所有

容器命令

下载一个linux镜像进行学习

docker pull  centos

新建容器并启动

docker run [可选参数]  images
#参数说明
--name="name" 容器的名字    区分容器
-d     在后台运行
-it       交互方式运行
-P(大写)  指定容器的端口   -p:8080
     -p  ip:主机端口:容器端口
     -p 主机端口:容器端口(常用的)
     -p  容器端口
    容器端口
 -p   随机指定端口
#测试    #启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash   
[root@3ab4dbba43a0 /]# ls  #查看内部的centos    很多命令不完善
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@3ab4dbba43a0 /]# exit     #退出容器

列出所有运行的容器

#docker ps命令
-a     列出历史运行过的容器
-n= ?  显示最近创建的容器
-q    只显示容器编号


[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED             STATUS                         PORTS     NAMES
3ab4dbba43a0   centos        "/bin/bash"   3 minutes ago       Exited (0) 59 seconds ago                intelligent_euclid
9484d4fa30c5   hello-world   "/hello"      About an hour ago   Exited (0) About an hour ago             strange_cori

退出容器

exit   直接退出
ctrl +p +q #容器不停止退出

删除容器

docker  rm  容器id    #删除指定容器,删除正在运行的容器需要带-f
docker rm -f $(docker ps -aq)   删除所有容器
docker ps -a|xargs docker rm   #删除所有的 容器

启动和停止容器的操作

docker start   容器id  #启动容器
docker restart  容器id #重启容器
docker stop  容器id     #停止容器
docker  kill   容器id    #强制停止容器

常用的其他命令

后台启动容器

#docker run  d  镜像名称

[root@localhost ~]# docker run -d centos

#问题   ps    发现 centos 停止了

#容器使用后台运行,必须要有一个前台执行程序,docker发现没有可用程序
就会进行停止。

查看日志命令

docker logs -f  -t -tail   容器没有日志
#编写一个脚本
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo yutoujun;sleep 1;done

#[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND
43bddbf6340d   centos
#显示日志  
-tf
--tail  number  指定条数
[root@localhost ~]# docker logs -tf --tail 10 43bddbf6340d

查看进程信息

#命令:docker top  容器id
[root@localhost ~]# docker top  43bddbf6340d
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4439                4421                0                   18:07               ?                   00:00:00            /bin/sh -c while true;do echo yutoujun;sleep 1;done
root                4761                4439                0                   18:12               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
 docker inspect  容器id
[root@localhost ~]# docker inspect 43bddbf6340d
[
    {
        "Id": "43bddbf6340dc19fe749d94cd0737c9443da83b140dce6e436340015bda30114",
        "Created": "2022-07-19T10:08:00.411361034Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo yutoujun;sleep 1;done"
        ],
     ...........................

当前正在运行的容器

#容器通常都是以后台的方式运行,需要进入容器,修改一些配置
docker exec -it 容器id bashshell
#测试
[root@localhost ~]# docker exec -it 43bddbf6340d bash
[root@43bddbf6340d /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 10:07 ?        00:00:00 /bin/sh -c while true;do echo yutoujun;sleep 1;done
root       2471      0  1 10:49 pts/0    00:00:00 bash
root       2491      1  0 10:49 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root       2492   2471  0 10:49 pts/0    00:00:00 ps -ef

#方式2
docker attach 容器id 
#测试
[root@localhost ~]docker attach  43bddbf6340d
正在执行代码.....

#docker exec #开启一个新的终端,可以在里边进行操作
#docker attach    #进入正在执行的终端,不会开启新的进程

从容器拷贝文件到主机上

docker cp 容器id:容器内路径  主机路径
#查看当前主机目录
[root@localhost home]# ls
yutoujun.txt
[root@localhost home]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
bf93c08ffbfd   centos    "/bin/bash"   22 seconds ago   Up 21 seconds             laughing_mestorf
#进入容器内部
[root@localhost home]# docker attach bf93c08ffbfd
[root@bf93c08ffbfd /]# cd /home
[root@bf93c08ffbfd home]# ls
#新建一个文件
[root@bf93c08ffbfd home]# touch test.txt
[root@bf93c08ffbfd home]# exit
exit
#退出进行文件拷贝
[root@localhost home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost home]# docker cp bf93c08ffbfd:/home/test.txt  /home
[root@localhost home]# ls
test.txt  yutoujun.txt
#拷贝是手动过程,后边会使用卷的技术进行自动同步

小结

docker.png

attach    Attach to a running container  #当前shell下attach连接指定运行镜像
build     Build an image from a Dockerfile  #通过Dockerfile定制镜像
commit    Create a new image from a container's changes  #提交当前容器为新的镜像
cp    Copy files/folders from a container to a HOSTDIR or to STDOUT  #从容器中拷贝指定文件或者目录到宿主机中
create    Create a new container  #创建一个新的容器,同run 但不启动容器
diff    Inspect changes on a container's filesystem  #查看docker容器变化
events    Get real time events from the server#从docker服务获取容器实时事件
exec    Run a command in a running container#在已存在的容器上运行命令
export    Export a container's filesystem as a tar archive  #导出容器的内容流作为一个tar归档文件(对应import)
history    Show the history of an image  #展示一个镜像形成历史
images    List images  #列出系统当前镜像
import    Import the contents from a tarball to create a filesystem image  #从tar包中的内容创建一个新的文件系统映像(对应export)
info    Display system-wide information  #显示系统相关信息
inspect    Return low-level information on a container or image  #查看容器详细信息
kill    Kill a running container  #kill指定docker容器
load    Load an image from a tar archive or STDIN  #从一个tar包中加载一个镜像(对应save)
login    Register or log in to a Docker registry#注册或者登陆一个docker源服务器
logout    Log out from a Docker registry  #从当前Docker registry退出
logs    Fetch the logs of a container  #输出当前容器日志信息
pause    Pause all processes within a container#暂停容器
port    List port mappings or a specific mapping for the CONTAINER  #查看映射端口对应的容器内部源端口
ps    List containers  #列出容器列表
pull    Pull an image or a repository from a registry  #从docker镜像源服务器拉取指定镜像或者库镜像
push    Push an image or a repository to a registry  #推送指定镜像或者库镜像至docker源服务器
rename    Rename a container  #重命名容器
restart    Restart a running container  #重启运行的容器
rm    Remove one or more containers  #移除一个或者多个容器
rmi    Remove one or more images  #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
run    Run a command in a new container  #创建一个新的容器并运行一个命令
save    Save an image(s) to a tar archive#保存一个镜像为一个tar包(对应load)
search    Search the Docker Hub for images  #在docker
hub中搜索镜像
start    Start one or more stopped containers#启动容器
stats    Display a live stream of container(s) resource usage statistics  #统计容器使用资源
stop    Stop a running container  #停止容器
tag         Tag an image into a repository  #给源中镜像打标签
top       Display the running processes of a container #查看容器中运行的进程信息
unpause    Unpause all processes within a container  #取消暂停容器
version    Show the Docker version information#查看容器版本号
wait         Block until a container stops, then print its exit code  #截取容器停止时的退出状态值

标签:容器,container,常用命令,镜像,docker,root,localhost
From: https://www.cnblogs.com/yutoujun/p/16927527.html

相关文章

  • docker基础练习
    练习nginx[root@localhosthome]#dockersearchnginx[root@localhosthome]#dockerpullnginx[root@localhosthome]#dockerimagesREPOSITORYTAG......
  • docker镜像详解
    Docker镜像详解什么是镜像镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个......
  • Dockerfile配合IDEA实现一键部署
    1.1Dokcker开启远程访问1.1.0修改docker服务的配置文件vim/lib/systemd/system/docker.service1在ExecStart那行,加上-Htcp://0.0.0.0:2375代表任何ip都可以访问重新加......
  • IDEA官方 Docker 插件一键部署应用到远程服务器
    环境:jdk1.8及以上。Maven3.2+ideadockerdocker开启远程连接访问首先我们要开启docker的远程连接访问。保证不是docker所在的服务器,也能够远程访问docker。Linux版的docker:1......
  • docker保存镜像
    在已有镜像系统上导出镜像保存镜像dockersave-oneo4j-3.5.35.tarneo4j:3.5.35-community导入镜像文件dockerload<neo4j-3.5.35.tar dockerimages   ......
  • 微服务之Docker
    大型项目组件较多,运行环境也较为复杂,部署时会碰到一些问题:依赖关系复杂,容易出现兼容性问题开发、测试、生产环境有差异 Docker如何解决大型项目依赖关系复杂,不同组件......
  • 使用Docker恢复Mysql8备份的Data数据 - 诚哥博客
    前段时间公司数据库服务器崩溃启动不起来,经过我初步排查是/home目录挂载不上导致启动不起来,虽然通过注释/etc/fstab中的挂载信息,启动了,但是发现mysql数据有缺失,还好有例......
  • [CG] 用 Docker 配置 Ubuntu OpenGL 环境
    成功在MacOS的Docker中运行OpenGL程序并显示这里记录一下:我用的是https://hub.docker.com/r/thewtex/opengl这个镜像非常好,大部分工作都做好了,这里用的是基于CPU......
  • centos 7.9环境下安装Docker
    一、安装CentOS7.9 1.从阿里镜像下载:https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/?spm=a2c6h.25603864.0.0.786ff5ad8J2jGC   2.虚拟机挂载ISO文件......
  • Docker数据持久化
    Docker挂载方式有3种,分别为volume,bindmount,tmpfs.volume, 挂载在宿主机指定路径bind,由开发者指定宿主机路径tmpfs,  挂载在宿主机内存   一、volume方......