Docker常用命令

共 10384字,需浏览 21分钟

 ·

2021-04-12 00:59


       整理了一些 Docker 常用的基础命令。


搜索镜像

        使用 search 命令可以搜索存放在 docker hub 上的镜像。

       命令格式如下:

% docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output

       例如:

% docker search mysqlNAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDmysql                             MySQL is a widely used, open-source relation…   10712     [OK]       mariadb                           MariaDB Server is a high performing open sou…   4029      [OK]       mysql/mysql-server                Optimized MySQL Server Docker images. Create785                  [OK]percona                           Percona Server is a fork of the MySQL relati…   529       [OK]       centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87                   mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   80                   centurylink/mysql                 Image containing mysql. Optimized to be link59                   [OK]bitnami/mysql                     Bitnami MySQL Docker Image                      50                   [OK]


下载镜像

       使用 docker pull 可以从 docker registry( docker 注册中心)中下载镜像。

        命令格式如下:

% docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options: -a, --all-tags Download all tagged images in the repository --disable-content-trust Skip image verification (default true) --platform string Set platform if server is multi-platform capable -q, --quiet Suppress verbose output

        例如:

% docker pull nginx      Using default tag: latestlatest: Pulling from library/nginxa076a628af6f: Already exists 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aaStatus: Downloaded newer image for nginx:latestdocker.io/library/nginx:latest


列出镜像

        使用 docker images 可以查看已经下载到本地的镜像。

        命令如下:

% docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show image IDs

       例如:

docker images -aREPOSITORY   TAG       IMAGE ID       CREATED        SIZEmysql        latest    c8562eaf9d81   2 months ago   546MBredis        latest    621ceef7494a   2 months ago   104MBnginx        latest    f6d0b4767a6c   2 months ago   133MB


删除本地的镜像

       使用 docker rmi 命令即可删除本地镜像。

% docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents

       例如:

% docker rmi nginxUntagged: nginx:latestUntagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aaDeleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5% docker images --allREPOSITORY   TAG       IMAGE ID       CREATED        SIZEmysql        latest    c8562eaf9d81   2 months ago   546MBredis        latest    621ceef7494a   2 months ago   104MB


创建并运行容器

       使用 docker run 命令可以创建并运行一个容器。

% docker run --name nginx-test -p 8088:80 -d nginxe9e3294f533923438a1bae551b2013d90916488f9e0cc0953282a593d06bf03b

--name:给容器命名

-p进行端口映射 8088 为映射后的物理机的端口号,80 为 docker 容器的端口

-d是后台容器


       用浏览器访问本地的 8088 端口,可以看到 nginx 的默认页。


查看容器

         可以通过 docker ps 查看运行中的容器。

       命令如下:

% docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display container IDs -s, --size Display total file sizes

       例如:

% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                  PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes            0.0.0.0:8088->80/tcp   nginx-test


停止容器

       可以使用 docker stop 来停止运行中的容器。

        命令如下:

% docker stop --help        
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options: -t, --time int Seconds to wait for stop before killing it (default 10)

       命令如下:

% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS          PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   8 minutes ago   Up 35 seconds   0.0.0.0:8088->80/tcp   nginx-test% docker stop e9e3294f5339e9e3294f5339% docker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES


强制停止docker容器

       使用 docker kill 来停止 docker 的容器。

        命令如下:

% docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options: -s, --signal string Signal to send to the container (default "KILL")

        例如:

% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 3 seconds   0.0.0.0:8088->80/tcp   nginx-test% docker kill e9e3294f5339e9e3294f5339% docker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES


启动停止的容器

       可以使用 docker start 来启动被 stop 或 kill 的容器。

% docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options: -a, --attach Attach STDOUT/STDERR and forward signals --detach-keys string Override the key sequence for detaching a container  -i, --interactive          Attach container's STDIN

       例如:

% docker start e9e3294f5339e9e3294f5339% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   14 minutes ago   Up 5 seconds   0.0.0.0:8088->80/tcp   nginx-test


重启容器

        可以使用 docker restart 来完成 docker stop 和 docker start 两个命令的工作

        命令如下:

% docker restart --help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers
Options: -t, --time int Seconds to wait for stop before killing the container (default 10)

        例如:

% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   16 minutes ago   Up 2 minutes   0.0.0.0:8088->80/tcp   nginx-test% docker restart e9e3294f5339e9e3294f5339% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   17 minutes ago   Up 3 seconds   0.0.0.0:8088->80/tcp   nginx-test

        是否进行了重启,可以查看status字段。


进入容器

        可以使用 docker exec 进入容器进行管理。

        命令如下:

 % docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   38 minutes ago   Up 14 minutes   0.0.0.0:8088->80/tcp   nginx-test% docker exec -it e9e3294f5339 /bin/bashroot@e9e3294f5339:/# uname -r4.19.121-linuxkit


删除容器

        可以使用 docker rm 来删除容器。

        命令如下:

% docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options: -f, --force Force the removal of a running container (uses SIGKILL) -l, --link Remove the specified link -v, --volumes Remove anonymous volumes associated with the container

        例如:

% docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMESe9e3294f5339   nginx     "/docker-entrypoint.…"   56 minutes ago   Up 33 minutes   0.0.0.0:8088->80/tcp   nginx-test% docker stop e9e3294f5339e9e3294f5339% docker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES% docker ps -allCONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS                  PORTS     NAMES5669d0d7c177   mysql     "docker-entrypoint.s…"   7 days ago   Exited (0) 5 days ago             mysql-new


总结

       上面的命令,基本上就把常用的对于镜像和容器的命令进行了整理。希望对你有帮助。

浏览 32
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报