Docker常用命令

docker的常用命令

帮助命令

docker version       #显示docker的版本显示
docker info          #显示docker的系统信息,包括镜像和容器的镜像
docker 命令 --help    #帮助命令

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

镜像命令


docker images 查看所有本地上的主机镜像

[root@Lyle ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        8 months ago        13.3kB
#解释
REPOSITORY  镜像的仓库源
TAG         镜像的标签
IMAGE ID    镜像的id
CREATED     镜像的创建时间
SIZE        镜像大小

#可选项
-a,-all      #列出所有镜像
[root@Lyle ~]# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        8 months ago        13.3kB

-q,--quiet   #只显示镜像的id
[root@Lyle ~]# docker images -q
bf756fb1ae65

docke search 搜索镜像

[root@Lyle ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9920                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3630                [OK]

#可选项,通过收藏来过滤
--filter=STARS=3000   #搜索出来的镜像就是STARS大于3000
[root@Lyle ~]# docker search mysql --filter=STARS=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9920                [OK]                
mariadb             MariaDB is a community-developed fork of MyS…   3630                [OK]


docker pull 下载镜像

#下载镜像 docker pull 镜像名[:tag]
[root@Lyle ~]# docker pull mysql
Using default tag: latest    #如果不写版本  那就是latest
latest: Pulling from library/mysql
bf5952930446: Pull complete   #分层下载,docker image的核心  联合文件系统
8254623a9871: Pull complete 
938e3e06dac4: Pull complete 
ea28ebf28884: Pull complete 
f3cef38785c2: Pull complete 
894f9792565a: Pull complete 
1d8a57523420: Pull complete 
6c676912929f: Pull complete 
ff39fdb566b4: Pull complete 
fff872988aba: Pull complete 
4d34e365ae68: Pull complete 
7886ee20621e: Pull complete 
Digest: sha256:c358e72e100ab493a0304bda35e6f239db2ec8c9bb836d8a427ac34307d074ed  #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest   #真实地址

#等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest

#指定版本下载
[root@Lyle ~]# 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 删除镜像

[root@Lyle ~]# docker rmi -f 718a6da099d8           #指定镜像id删除
[root@Lyle ~]# docker rmi -f 镜像id 镜像id 镜像id     #删除多个容器
[root@Lyle ~]# docker rmi -f $(docker images -aq)   #删除所有镜像 

容器命令


说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image
#参数说明
--name="NAME"  容器名字  tomcat01  用来区分容器
-d             后台方式运行
-it            使用交互方式运行,进入容器查看内容
-p             指定容器的端口 -p 8080:8080
	-p  主机端口:容器端口
	-p  容器端口
	
-P             随机指定端口

#测试   启动并进入容器
[root@Lyle ~]# docker run -it centos /bin/bash
[root@87218d533d2c /]# ls   #查看容器内的cnetos,基础版本
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@87218d533d2c /]# exit   #从容器退回主机命令  exit
exit
[root@Lyle ~]# ls

列出所有的运行的容器

#docker ps 命令
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=?  #显示最近创建的容器
-q #只显示容器编号
[root@Lyle /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
87218d533d2c        centos              "/bin/bash"         6 minutes ago       Exited (0) 5 minutes ago                       hopeful_villani
78c414970ddb        hello-world         "/hello"            31 hours ago        Exited (0) 31 hours ago                        clever_wescoff
41edebd26da1        hello-world         "/hello"            47 hours ago        Exited (0) 47 hours ago                        nostalgic_mendeleev
43df47b7cb75        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                          lucid_lewin
b62802784f7b        hello-world         "/hello"            3 days ago          Exited (0) 3 days ago                          serene_nash

退出容器

exit   #直接退出容器
Crtl+P+Q  #容器不停止退出

删除容器

docker rm 容器id                #删除指定容器  不能删除正在运行的容器,如果强制删除  rm-rf
docker rm -f $(docker ps -aq)  #删除所有容器
[root@Lyle /]# docker rm -f $(docker ps -aq)
3ff4f7cbb4e4
87218d533d2c
78c414970ddb
41edebd26da1
43df47b7cb75
b62802784f7b
docker ps -a -q|xargs docker rm #删除所有容器

启动和停止容器操作

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

常用其他命令


后台启动容器

#命令 docker run -d  镜像名
[root@Lyle /]# docker run -d centos
#
#问题docker ps 发现centos停止

#常见的坑,docker容器使用后台运行 就必须要有要一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs -f -t --tail 容器,没有日志
#shell脚本
[root@Lyle /]# docker run -d centos /bin/sh -c "while true;do echo lyle;sleep 1;done"

#[root@Lyle /]# docker ps
CONTAINER ID        IMAGE   
068252bb2279        centos  

#显示日志
-tf
--tail number #显示日志行数
[root@Lyle /]# docker logs -tf 068252bb2279

查看容器中进程信息ps

#命令 docker top 容器id
[root@Lyle /]# docker top 068252bb2279
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                6580                6564                0                   18:12               ?                   00:00:00            /bin/sh -c while true;do echo lyle;sleep 1;done
root                7090                6580                0                   18:18               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

#命令
docker inspect
[root@Lyle /]# docker inspect 068252bb2279

进入当前正在运行的容器

#我们通常容器都是是用后台方式运行的,需要进入容器,修改一些配置

#命令
docker exec -it 容器id bashShell
[root@Lyle /]# docker exec -it 068252bb2279 /bin/bash
[root@068252bb2279 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@068252bb2279 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 10:12 ?        00:00:00 /bin/sh -c while true;do echo lyle;sleep 1;done
root        766      0  0 10:25 pts/0    00:00:00 /bin/bash
root        789      1  0 10:25 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root        790    766  0 10:25 pts/0    00:00:00 ps -ef

#方式二  进入正在运行容器id
docker attach 容器id
#测试
[root@Lyle ~]# docker attach f0c653d77f45
#正在执行当前的代码....

#docker exec   #进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径  目的的主机路径

#查看当前主机目录下
[root@Lyle home]# ls
confluence  nginx_access.log-20200706  roo
[root@Lyle home]# touch lyle.java
[root@Lyle home]# ls
confluence  lyle.java  nginx_access.log-20200706  roo
[root@Lyle home]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bc05bc7224ee        centos              "/bin/bash"         56 seconds ago      Up 55 seconds                           great_kare

#进入docker容器内部
[root@Lyle home]# docker attach bc05bc7224ee
[root@bc05bc7224ee /]# cd /home
[root@bc05bc7224ee home]# ls
#在容器内新建一个文件
[root@bc05bc7224ee home]# touch test.java
[root@bc05bc7224ee home]# exit  
exit
[root@Lyle home]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@Lyle home]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
bc05bc7224ee        centos              "/bin/bash"              2 minutes ago       Exited (0) 19 seconds ago                        great_kare
f0c653d77f45        centos              "/bin/sh -c 'while t…"   7 hours ago         Exited (137) 2 minutes ago                       amazing_fermi
de9d0a101b2c        centos              "/bin/sh 'while true…"   7 hours ago         Exited (127) 7 hours ago                         sad_kowalevski
bc5dbab89bfa        centos              "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                           hungry_benz
963ee7bad9c4        centos              "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                           awesome_blackwell
75dfbcf48b03        centos              "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                           amazing_lewin
d28c9bd3bd95        bf756fb1ae65        "/hello"                 7 hours ago         Exited (0) 7 hours ago                           hopeful_nash
068252bb2279        centos              "/bin/sh -c 'while t…"   23 hours ago        Exited (137) 22 hours ago                        gracious_tereshkova
372678136cad        centos              "/bin/sh -C 'while t…"   23 hours ago        Exited (127) 23 hours ago                        mystifying_kepler
f49fc3109ce7        centos              "/bin/sh -C 'while t…"   23 hours ago        Exited (127) 23 hours ago                        keen_torvalds
c11e9600ddee        centos              "/bin/bash"              23 hours ago        Exited (0) 23 hours ago                          xenodochial_kepler
1f164b885071        centos              "/bin/bash"              23 hours ago        Exited (0) 23 hours ago                          happy_franklin
09a4f892f0d6        centos              "/bin/bash"              23 hours ago        Exited (0) 23 hours ago                          thirsty_hawking
45ce78303b44        centos              "/bin/bash"              23 hours ago        Exited (0) 23 hours ago                          naughty_lovelace

#将这个文件拷贝出来到主机上
[root@Lyle home]# docker cp bc05bc7224ee:/home/test.java /home
[root@Lyle home]# ls
confluence  lyle.java  nginx_access.log-20200706  roo  test.java

#拷贝是一个手动过程 未来我们使用 -v 卷的技术可以实现

小结

image-20200517091953964

img

image-20200517092953977

作业练习

Docker 安装Nginx

#1、搜索镜像 search 建议去docker搜索,可以看到帮助文档
[root@Lyle home]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13694               [OK] 
#2、下载镜像 pull
[root@Lyle home]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
#3、运行测试
[root@Lyle home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        3 weeks ago         133MB
centos              latest              0d120b6ccaa8        3 weeks ago         215MB

#运行Nginx
#-d      后台运行
#--name  给容器命名
#-p		 宿主机端口:容器内部端口
[root@Lyle home]# docker run -d --name nginx01 -p 3344:80 nginx
f53439877cdc1e93b8e1972bf36b7ab68c657797c002b3e632e387d0a3d2448d
[root@Lyle home]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
f53439877cdc        nginx               "/docker-entrypoint.…"   5 seconds ago       Up 3 seconds        0.0.0.0:3344->80/tcp   nginx01

#访问nginx
[root@Lyle home]# curl localhost:3344

#进入容器
[root@Lyle /]# docker exec -it nginx02 /bin/bash
root@16068043b76d:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@16068043b76d:/# 

作业:docker来安装tomcat

#官方的使用
docker run -it --rm tomcat:9.0

#我们之前的启动都是后台,停止了容器之后,

#下载再启动
docker pull tomcat

#启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat

#进入容器
[root@Lyle ~]# docker exec -it tomcat1 /bin/bash
root@41b62a83f3a3:/usr/local/tomcat# ls

#发现问题:1、linux命令少了 2、没有webapps 阿里云镜像的原因 默认是最小的镜像 所有不必要的都剔除掉
#保证最小可运行的环境
#将webapps.dist/ 下所有文件拷贝到webapps下
root@b992bf9c3d9f:/usr/local/tomcat# cp -r webapps.dist/* webapps

思考问题:我们以后要部署项目,如果每次都要进入容器是不是十分麻烦?我要是可以在容器外部提供一个映射路径,webapps,我们在外部放置项目,就自动同步到内部就好了

作业:部署es+kibana

#es 暴露的端口很多
#es 十分的耗内存
#es 的数据一般需要放置到安全目录挂载
# --net somenetwork?网络配置

#启动
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

#启动 linux卡住  docker stats 查看 cpu的状态

#es 是十分耗内存的

#查看cpu的内存  docker stats
#增肌内存的限制
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m-Xmx512m" elasticsearch:7.6.2

#查看 docker stats

可视化


  • portainer(先用这个)

    docker run -d -p 8008:9000 \
    --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
    
  • Rancher(CI/CD再用)

    什么pottainer?

    Docker图形化界面管理工具

    docker run -d -p 8008:9000 \
    --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
    

    访问测试
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值