当前位置:   article > 正文

DockerPodman使用入门---基础命令篇_podman-docker

podman-docker

文章目录

centos8 中一律使用podman工具代替了docker命令,具体用法基本一致

1.帮助命令

docker version       # 显示docker的版本信息
docker info          # 显示docker的系统信息,包括镜像和容器的数量
docker 命令xxx  --help   # 帮助命令
  • 1
  • 2
  • 3

2.镜像命令

1).docker images 查看所有本地主机上的镜像
[root@AutoTestServer ~]# podman images

REPOSITORY                                 TAG     IMAGE ID      CREATED      SIZE
docker.io/library/redis                    latest  621ceef7494a  5 weeks ago  108 MB
<none>                                     <none>  6060df96cef3  5 weeks ago  108 MB
docker.io/library/mysql                    latest  d4c3cafb11d5  5 weeks ago  551 MB
uhub.service.ucloud.cn/ucloud/centos7-ssh  latest  30596dd4fa80  3 years ago  223 MB
docker.io/ansible/centos7-ansible          latest  688353a31fde  4 years ago  463 MB


# 解释
REPOSITORY  镜像的仓库源
TAG         镜像的标签
IMAGE ID    镜像的id
CREATED     镜像的创建时间

# 可选项
	-a, --all      # 列出所有的镜像
	-q, --quiet    # 只显示镜像的id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2).docker search 搜索镜像
[root@AutoTestServer ~]# docker search mysql
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
INDEX        NAME                                                   DESCRIPTION                                       STARS   OFFICIAL   AUTOMATED

redhat.io    registry.redhat.io/rhscl/mysql-80-rhel7                This container image provides a containerize...   0
docker.io    docker.io/library/mysql                                MySQL is a widely used, open-source relation...   10516   [OK]
docker.io    docker.io/mysql/mysql-server                           Optimized MySQL Server Docker images. Create...   772                [OK]
docker.io    docker.io/bitnami/mysql                                Bitnami MySQL Docker Image                        48                 [OK]
docker.io    docker.io/circleci/mysql                               MySQL is a widely used, open-source relation...   20
docker.io    docker.io/mysql/mysql-cluster                          Experimental MySQL Cluster Docker images. Cr...   79
docker.io    docker.io/schickling/mysql-backup-s3                   Backup MySQL to S3 (supports periodic backup...   29                 [OK]
docker.io    docker.io/centos/mysql-57-centos7                      MySQL 5.7 SQL database server                     86
docker.io    docker.io/library/mariadb                              MariaDB is a community-developed fork of MyS...   3927    [OK]
docker.io    docker.io/deitch/mysql-backup                          REPLACED! Please use http://hub.docker.com/r...   41                 [OK]
docker.io    docker.io/ansibleplaybookbundle/mysql-apb              An APB which deploys RHSCL MySQL                  2                  [OK]


# 可选项
--filter , -f		Filter output based on conditions provided


# 选择starts 大于等于5000的镜像源

[root@AutoTestServer ~]# docker search mysql -f=stars=5000
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
INDEX       NAME                      DESCRIPTION                                       STARS   OFFICIAL   AUTOMATED
docker.io   docker.io/library/mysql   MySQL is a widely used, open-source relation...   10516   [OK]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
3).docker pull 下载镜像
docker pull  docker.io/library/mysql

# docker.io/library/mysql,这个版本是我们搜索到的,stars 最多的版本
# 默认下载最新的版本


docker pull mysql:5.7
# 指定某个版本下载
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 上述拉取镜像源, 默认是从国外的docker hub 源,速度肯定很慢的,你懂的
  • 如果是阿里云服务器,有个镜像加速服务,可以配置一下,下载默认会从阿里的镜像源
  • 还有网易hub源等等,根据服务器的服务商,找到最优方案吧
  • 我使用的是一款小众服务器,UCloud,服务器官网有个页面,把常规需要的镜像源都下载好了,直接搜索一下,然后再下载会非常非常的快!

在这里插入图片描述

在这里插入图片描述

通过上图:
发现我们再拉取的镜像源,都是来自ucloud服务器了!

4).docker rmi xxx 删除镜像
docker images 						# 查看拉取的本地镜像
docker rmi   IMAGE ID 				# 删除镜像




[root@AutoTestServer AutoTestEnv]# docker images
REPOSITORY                                 TAG     IMAGE ID      CREATED        SIZE
localhost/centos7-alien                    1.0     8cdba1332c7b  3 minutes ago  1.05 GB
docker.io/library/redis                    latest  621ceef7494a  5 weeks ago    108 MB
<none>                                     <none>  6060df96cef3  5 weeks ago    108 MB
docker.io/library/mysql                    latest  d4c3cafb11d5  5 weeks ago    551 MB
uhub.service.ucloud.cn/ucloud/centos7-ssh  latest  30596dd4fa80  3 years ago    223 MB
docker.io/ansible/centos7-ansible          latest  688353a31fde  4 years ago    463 MB



[root@AutoTestServer AutoTestEnv]# docker rmi 8cdba1332c7b
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
Untagged: localhost/centos7-alien:1.0
Deleted: 8cdba1332c7b75790e8347bf14f31e86b9fab9428f00404019c271a6c104c325
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.容器命令

1).docker run 命令
docker run [可选参数] image

# 参数说明
--name = "Name"    容器名字  tomcat01,tomcat02,用来区分容器
-d                 后台方式运行
-it                使用交互方式运行,进入容器查看区分
-p                 指定容器的端口 -p 8080:8080



    -p ip:主机端口:容器端口
    -p 主机端口:容器端口(常用)
    -p 容器端口
    容器端口
-p                 随机指定端口




[root@AutoTestServer ~]# docker images
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
REPOSITORY                                 TAG     IMAGE ID      CREATED      SIZE
docker.io/library/redis                    latest  621ceef7494a  5 weeks ago  108 MB
<none>                                     <none>  6060df96cef3  5 weeks ago  108 MB
docker.io/library/mysql                    latest  d4c3cafb11d5  5 weeks ago  551 MB
uhub.service.ucloud.cn/ucloud/centos7-ssh  latest  30596dd4fa80  3 years ago  223 MB
docker.io/ansible/centos7-ansible          latest  688353a31fde  4 years ago  463 MB



# 因为我本地有2个centos7镜像,而我想使用的镜像是centos7-ssh
# 最终启动容器方案:
docker run -it -d --name uc_centos7 uhub.service.ucloud.cn/ucloud/centos7-ssh /bin/bash


# 从容器中退回主机
exit

# 容器不停止退出
Ctrl + P + Q    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
2).docker rm 删除容器:
docker rm 容器id                  # 删除指定容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)    # 删除所有的容器
docker ps -aq|xargs docker rm    # 删除所有的容器
  • 1
  • 2
  • 3
3).启动和停止容器的操作:
docker start 容器id        # 启动容器
docker restart 容器id      # 重启容器
docker stop 容器id         # 停止当前正在运行的容器
docker kill 容器id         # 强制停止当前容器
  • 1
  • 2
  • 3
  • 4

4.其他命令

1).后台启动容器
# 命令 docker run -d 镜像名
[root@AlibabaECS /]# docker run -d centos

# 问题docker ps, 发现 centos 停止了

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



# 以上内容,来源网络,我再centos7 & centos8 都尝试过,发现容器没有停止。
# 大致查询了一下,也没找到何种情况下会停止,何时不会停止?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
2).docker logs 查看日志
docker logs [OPTIONS] CONTAINER

-f : 跟踪日志输出

--since :显示某个开始时间的所有日志

-t : 显示时间戳

--tail :仅列出最新N条容器日志


# 查看某个容器,最新的10条日志信息
docker logs -ft --tail 10 ebd1f86a2exxx


# 查看某个容器,从某一日期到现在的所有日志
docker logs --since="2021-02-18"  ebd1f86a2xxx



# 查看日志, 从某一日期的某个时间点到现在的所有日志
docker logs --since="2021-02-18T07:43:27.352723Z"  ebd1f86a2xxx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

在这里插入图片描述

3).docker top xxx 查看容器的进程
# 这个方案是在容器外,即可查看容器内的进程情况

[root@AutoTestServer logs]# docker top ebd1f86a2edd

USER    PID   PPID   %CPU    ELAPSED               TTY   TIME     COMMAND
mysql   1     0      0.112   884h8m56.78115058s    ?     59m31s   mysqld
root    227   1      0.000   572h10m46.78121771s   ?     0s       [mysql]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
4).docker inspect xxxx 查看容器详情数据
docker inspect ebd1f86a2xxx

# 在容器详情数据中,搜索logs字段前后5行的内容
docker inspect ebd1f86a2edd | grep -C 5 logs


-A 后面N行
-B 前面N行
-C 前后N行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
4).docker exec -it xxx 进入容器
# 进入容器后开启一个新的终端,可以在里面操作(常用)
docker exec -it 容器id  /bin/bash


# 不建议使用
# 进入容器正在执行的终端,不会启动新的进程
# 当多个窗口同时使用该命令进入该容器时,所有的窗口都会同步显示。如果有一个窗口阻塞了,那么其他窗口也无法再进行操作。
docker attach 容器id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
5).docker cp 容器 & 主机 相互拷贝文件
# 从容器拷贝到主机文件

docker cp [r] 容器id :容器内路径   主机路径
docker cp 8f0008028xxx:/alien_test.txt  /root/

# 从主机拷贝文件到容器中
docker cp [r] 主机路径  容器id :容器内路径
docker cp local_file.txt   8f0008028cb2:/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5.终极宝典—所有命令图谱

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/904069
推荐阅读
相关标签
  

闽ICP备14008679号