Docker的使用

Docker基本命令

  1. docker version: docker版本
  2. docker info: docker信息
  3. docker [command] –help: 获取命令帮助

1. Docker镜像命令

docker images

$ docker images
REPOSITORY    #仓库
TAG            #标签
IMAGE ID    #镜像
CREATED        #创建时间
SIZE        #大小

1. -a --all        #列出所有镜像
2. -q --quiet        #只显示镜像ID

docker search

$ docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9869                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3605                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   721                                     [OK]

1. --filter=[NAME,STARS...]=... #过滤搜索

docker pull name[:tag(默认位latest)]

$ docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Already exists                                                                                                8254623a9871: Already exists                                                                                                938e3e06dac4: Already exists                                                                                                ea28ebf28884: Already exists                                                                                                f3cef38785c2: Already exists                                                                                                894f9792565a: Already exists                                                                                                1d8a57523420: Already exists                                                                                                5f09bf1d31c1: Pull complete                                                                                                 1b6ff254abe7: Pull complete                                                                                                 74310a0bf42d: Pull complete                                                                                                 d398726627fd: Pull complete                                                                                                 Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi -f 容器ID[:tag] [容器ID…/$(docker) images -aq]

$ docker rmi mysql:5.7
Untagged: mysql:5.7
Untagged: mysql@sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Deleted: sha256:718a6da099d82183c064a964523c0deca80619cb033aadd15854771fe592a480
Deleted: sha256:058d93ef2bfb943ba6a19d8b679c702be96e34337901da9e1a07ad62b772bf3d
Deleted: sha256:7bca77783fcf15499a0386127dd7d5c679328a21b6566c8be861ba424ac13e49
Deleted: sha256:183d05512fa88dfa8c17abb9b6f09a79922d9e9ee001a33ef34d1bc094bf8f9f
Deleted: sha256:165805124136fdee738ed19021a522bb53de75c2ca9b6ca87076f51c27385fd7

Docker Commit 提交镜像

PS C:\Users\Administrator> docker commit -a="calong" -m="Commit Test" b3a59f64071f tomcal:1.0
sha256:64fdb91ddae709b57ef7dd4f9c8b29ec88f1e0e08039a8d49d07fc595654a8bb
PS C:\Users\Administrator> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                          NAMES
b3a59f64071f        tomcat              "/bin/bash"         2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp, 8080/tcp   upbeat_meitner
PS C:\Users\Administrator> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcal              1.0                 64fdb91ddae7        11 seconds ago      652MB
nginx               latest              4bb46517cac3        9 days ago          133MB
node                latest              784e696f5060        10 days ago         943MB
centos              latest              0d120b6ccaa8        12 days ago         215MB
tomcat              latest              2ae23eb477aa        2 weeks ago         647MB
redis               latest              1319b1eaa0b7        2 weeks ago         104MB
mysql               latest              0d64f46acfd1        2 weeks ago         544MB

2. Docker容器命令

docker run [可选参数] image
–name=“Name” 指定容器名称
-d 以后台的方式运行
-p [主机端口:][容器端口] 指定端口/随机端口

#启动并运行
$ docker run -it centos /bin/bash
[root@4f73b5364975 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#退出容器
[root@4f73b5364975 /]# exit
exit

docker ps 列出当前正在运行的容器
-a #列出所有
-n=? #列出最近创建的容器
-q #只显示容器的编号

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e1fb88fd4773        centos              "/bin/bash"         9 seconds ago       Up 9 seconds                            jolly_stonebraker

docker rm 删除容器

$ docker rm -f $(docker ps -aq)
e1fb88fd4773
15f9b0644bca
9774e741f39d
4f73b5364975

3. 其它常用命令

docker start 容器ID 启动容器
docker restart 容器ID 重启容器
docker stop 容器ID 停止当前正在运行的容器
docker kill 容器ID 强制停止当前容器

docker run [-d] [-v] 镜像名称 启动容器
-d: 后台启动
-v: 挂载外部目录

docker logs [可选参数]
-t #时间戳
-f #输出日志
–tail 10 #显示10条日志

docker inspect 容器ID 查看镜像的元数据

docker exec [=it] 容器ID 进入正在运行的容器并开启新的终端
docker attach 容器ID 进入正在运行的容器并打开正在运行的终端

$ docker run -it centos /bin/bash
[root@962ca12ae7e8 /]#
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
962ca12ae7e8        centos              "/bin/bash"         17 seconds ago      Up 19 seconds                           nervous_keller
$ docker exec -it 962ca12ae7e8 /bin/bash
[root@962ca12ae7e8 /]#

docker cp 容器ID:文件路径 目标目录 从容器中复制文件

4. Docker Machine 命令

  • 查看docker虚拟机的IP地址:docker-machine ip [default]

Docker图形化管理界面Portainer

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!