Docker常用命令简介

Posted 白-胖-子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker常用命令简介相关的知识,希望对你有一定的参考价值。

Docker常用命令分类

分类Docker 子命令
Docker信息info,version
镜像仓库命令login/logout,pull,push,search
本地镜像管理images,rmi,tag,build,save,load,import
容器操作命令ps,exec,attach,stats,top,update,wait,export,port
容器运行管理run,rm,start/stop/restart,kill,pause/unpause,creat
文件系统相关commit,cp,diff
日志信息历史操作events,logs,history

docker命令

Docker 镜像操作 常用命令简介

docker search 镜像搜索

  • 使用命令连接到官方Docker Hub中在线搜索镜像
docker search [OPTIONS] TERM
  • 不指定版本搜索,搜到的都是lasted
root@u20a:~# docker search centos
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                            The official build of CentOS.                   6638      [OK]       
ansible/centos7-ansible           Ansible on Centos7                              134                  [OK]
consol/centos-xfce-vnc            Centos container with "headless" VNC session…   129                  [OK]
jdeathe/centos-ssh                OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   118                  [OK]
centos/systemd                    systemd enabled base container.                 100                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   89                   
imagine10255/centos6-lnmp-php56   centos6-lnmp-php56                              58                   [OK]
tutum/centos                      Simple CentOS docker image with SSH access      48                   
centos/postgresql-96-centos7      PostgreSQL is an advanced Object-Relational …   45                   
kinogmt/centos-ssh                CentOS with SSH                                 29                   [OK]
guyton/centos6                    From official centos6 container with full up…   10                   [OK]
nathonfowlie/centos-jre           Latest CentOS image with the JRE pre-install…   8                    [OK]
centos/tools                      Docker image that has systems administration…   7                    [OK]
drecom/centos-ruby                centos ruby                                     6                    [OK]
darksheer/centos                  Base Centos Image -- Updated hourly             3                    [OK]
mamohr/centos-java                Oracle Java 8 Docker image based on Centos 7    3                    [OK]
indigo/centos-maven               Vanilla CentOS 7 with Oracle Java Developmen…   2                    [OK]
dokken/centos-7                   CentOS 7 image for kitchen-dokken               2                    
miko2u/centos6                    CentOS6 日本語環境                                   2                    [OK]
amd64/centos                      The official build of CentOS.                   2                    
mcnaughton/centos-base            centos base image                               1                    [OK]
blacklabelops/centos              CentOS Base Image! Built and Updates Daily!     1                    [OK]
starlabio/centos-native-build     Our CentOS image for native builds              0                    [OK]
smartentry/centos                 centos with smartentry                          0                    [OK]
king019/centos                    centos                                          0                    
  • 仅搜索官方版镜像
root@u20a:~# docker search -f is-official=true ubuntu
NAME                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu               Ubuntu is a Debian-based Linux operating sys…   12493     [OK]       
websphere-liberty    WebSphere Liberty multi-architecture images …   276       [OK]       
ubuntu-upstart       Upstart is an event-based replacement for th…   111       [OK]       
neurodebian          NeuroDebian provides neuroscience research s…   84        [OK]       
open-liberty         Open Liberty multi-architecture images based…   47        [OK]       
ubuntu-debootstrap   debootstrap --variant=minbase --components=m…   44        [OK]       

docker pull 下载镜像

  • 把搜索到的镜像从镜像仓库拉到本地
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • 如果是私有仓库要写名地址和端口号
docker pull 仓库服务器:端口/项目名称/镜像名称:tag(版本)号

docker images 查看本地镜像

  • 查看本地镜像
root@u20a:~# docker images
REPOSITORY    TAG        IMAGE ID       CREATED        SIZE
nginx         latest     4cdc5dd7eaad   8 days ago     133MB
hello-world   latest     d1165f221234   4 months ago   13.3kB
centos        latest     300e315adb2f   7 months ago   209MB
centos        7.9.2009   8652b9f0cb4c   8 months ago   204MB

docker save 导出镜像

  • 导出镜像,将本地镜像导出到文件
    docker save 镜像名 -o 本地文件.tar.gz

docker load 导入镜像

  • 导入镜像,将外部离线镜像包导入到本地
    docker load -i 本地文件.tar.gz

docker rmi 删除镜像

  • 删除镜像,将本地镜像从docker中删除掉
    docker rmi -f 镜像名

docker镜像查看 导出、删除、导入示例

root@u20a:~# docker save 
centos              centos:7.9.2009     centos:latest       hello-world         hello-world:latest  nginx               nginx:latest        
root@u20a:~# docker save centos:7.9.2009 -o /data/docker-image-back/centos_7.9.2009.tar.gz
root@u20a:~# ll /data/docker-image-back/
total 206744
drwxr-xr-x 2 root root      4096 Jul 15 04:37 ./
drwxr-xr-x 3 root root      4096 Jul 15 04:34 ../
-rw------- 1 root root 211696640 Jul 15 04:37 centos_7.9.2009.tar.gz
root@u20a:~# docker rmi -f centos:7.9.2009 && docker images
Untagged: centos:7.9.2009
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    4cdc5dd7eaad   8 days ago     133MB
hello-world   latest    d1165f221234   4 months ago   13.3kB
centos        latest    300e315adb2f   7 months ago   209MB
root@u20a:~# docker load -i /data/docker-image-back/centos_7.9.2009.tar.gz && docker images
Loaded image: centos:7.9.2009
REPOSITORY    TAG        IMAGE ID       CREATED        SIZE
nginx         latest     4cdc5dd7eaad   8 days ago     133MB
hello-world   latest     d1165f221234   4 months ago   13.3kB
centos        latest     300e315adb2f   7 months ago   209MB
centos        7.9.2009   8652b9f0cb4c   8 months ago   204MB

Docker 容器操作 常用命令简介

docker run 运行容器

  • 从镜像启动一个容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • 以交互方式单次运行nginx,映射宿主机80端口到容器80端口,运行容器直接进入/bin/bash,运行结束推出后即删除该容器
docker run -it --rm -p 80:80/tcp --name nginx_S1 nginx:latest /bin/bash

退出即删除

  • 后台运行容器,并将主机的目录 /data 映射到容器的 /data,使用tail命令作为容器的主进程,支撑容器一直运行着
docker run -d -v /data:/data centos:latest /usr/bin/tail -f '/etc/hosts'

后台运行docker容器

docker ps -a 查看容器

  • 查看本地容器
docker ps [OPTIONS]
  • 不加参数默认查看到的是正在运行中的容器
  • 常用参数就是-a,查看所有容器,包括已经停止的容器
root@u20a:~# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
a4c4322a6afe   centos    "/usr/bin/tail -f /e…"   11 minutes ago   Up 11 minutes             angry_bell
root@u20a:~# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                      PORTS     NAMES
a4c4322a6afe   centos            "/usr/bin/tail -f /e…"   14 minutes ago   Up 14 minutes                         angry_bell
c9d0c1443679   centos:7.9.2009   "bash"                   21 hours ago     Exited (255) 18 hours ago             goofy_mcnulty
4382ecca391b   hello-world       "/hello"                 23 hours ago     Exited (0) 23 hours ago               friendly_mclean

docker exec 容器交互

  • 与容器进行交互,可以传递命令或者环境变量
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  • 不进入容器,让一个运行的容器执行命令
root@u20a:~# docker exec a4c4322a6afe /bin/echo "Hello 2021"
Hello 2021
  • 也可以进入一个正在运行中的容器
root@u20a:~# docker exec -it a4c4322a6afe /bin/bash
[root@a4c4322a6afe /]# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0  23020  1324 ?        Ss   03:43   0:00 /usr/bin/coreutils --coreutils-prog-shebang=tail /usr/bin/tail -f /etc/hosts
root          73  1.1  0.1  12020  3264 pts/0    Ss   04:57   0:00 /bin/bash
root          88  0.0  0.1  44628  3428 pts/0    R+   04:57   0:00 ps aux

docker rm 删除容器

  • 删除容器,可以一次删除多个容器
  • 运行中的容器正常情况不能直接删除,如果需要可以强制删除
    docker rm [OPTIONS] CONTAINER [CONTAINER...]
root@u20a:~# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS             PORTS     NAMES
a4c4322a6afe   centos    "/usr/bin/tail -f /e…"   About an hour ago   Up About an hour             angry_bell
root@u20a:~# docker rm -fvl a4c4322a6afe
Error response from daemon: Conflict, cannot remove the default link name of the container
root@u20a:~# docker rm -fv a4c4322a6afe
a4c4322a6afe
root@u20a:~# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  • 批量毁灭级删除所有容器,跑路前奏
docker rm -f $(docker ps -a -q)

还有很多命令没有介绍到,除了镜像制作单独介绍外,其余命令均用的不多,再次并不赘述,需要的话再查帮助即可

Docker 全部命令

root@u20a:~# 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:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.8.0)
  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

以上是关于Docker常用命令简介的主要内容,如果未能解决你的问题,请参考以下文章

Docker-Compose简介及常用命令

Docker快速入门——Docker常用命令

Docker学习总结

Docker及常用软件的安装部署

markdown [Docker] Docker片段列表和命令#linux #docker #snippets

常用的docker命令进程命令镜像命令容器命令