IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> Docker 镜像、容器操作--小试牛刀 -> 正文阅读

[PHP知识库]Docker 镜像、容器操作--小试牛刀

一、镜像操作

1.1 搜索镜像

docker search 镜像名称 
docker search 镜像名称:标签名称
[root@docker ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15404     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2063                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   816                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   240                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   152                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   140                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        135                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   106                  [OK]
jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   92                   [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   72                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   56                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  47                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   19                   
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
raulr/nginx-wordpress             Nginx front-end for the official wordpress:f…   13                   [OK]
flashspys/nginx-static            Super Lightweight Nginx Image                   10                   [OK]
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
[root@docker ~]# 

1.2 下载镜像

docker pull 镜像名称
[root@docker ~]# docker pull nginx
Using default tag: latest //如果下载时不指定标签,会默认下载最新的镜像,latest表示最新
latest: Pulling from library/nginx
a330b6cecb98: Pull complete 
5ef80e6f29b5: Pull complete 
f699b0db74e3: Pull complete 
0f701a34c55e: Pull complete 
3229dce7b89c: Pull complete 
ddb78cb2d047: Pull complete 
Digest: sha256:c442ff5a3d9f6dc92dd8aee388ece42d965e002805e3edaaff34e79d8ee6d0a4
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@docker ~]# 

1.3 镜像打标签

docker tag 镜像名称:旧标签名 镜像名称:新标签名
[root@docker ~]# docker images  //查看镜像列表
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        latest    822b7ec2aaf2   54 minutes ago   133MB
[root@docker ~]# docker tag nginx:latest nginx:lnmp
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        latest    822b7ec2aaf2   55 minutes ago   133MB
nginx        lnmp      822b7ec2aaf2   55 minutes ago   133MB
[root@docker ~]# 

1.4 查看镜像信息

docker inspect 镜像id
[root@docker ~]# docker inspect 822b7ec2aaf2
[
    {
        "Id": "sha256:822b7ec2aaf2122b8f80f9c7f45ca62ea3379bf33af4e042b67aafbf6eac1941",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-03T07:40:16.355730864Z",
        "Container": "367d32086ac12447d36e75c9b7acbe1b5156a34a91370b9200e68783be75506c",
        "ContainerConfig": {
            "Hostname": "367d32086ac1",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.21.1",
                "NJS_VERSION=0.6.1",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
......省略部分内容
[root@docker ~]# 

1.5 删除镜像

docker rmi 镜像ID 或者 镜像名称:标签名
[root@docker ~]# docker rmi 822b7ec2aaf2
Error response from daemon: conflict: unable to delete 822b7ec2aaf2 (must be forced) - image is referenced in multiple repositories  //错误信息说:冲突:无法删除822b7ec2aaf2(必须强制)-映像在多个存储库中被引用
//如果想要删除该镜像,使用docker rmi 镜像名称:标签名 的方式一个一个的删除
[root@docker ~]# 
[root@docker ~]# docker rmi nginx:lnmp
Untagged: nginx:lnmp
[root@docker ~]# docker rmi nginx:latest 
Untagged: nginx:latest //直到没有引用了,才会删除
Untagged: nginx@sha256:c442ff5a3d9f6dc92dd8aee388ece42d965e002805e3edaaff34e79d8ee6d0a4
Deleted: sha256:822b7ec2aaf2122b8f80f9c7f45ca62ea3379bf33af4e042b67aafbf6eac1941
Deleted: sha256:47dec9bde9e483e6265a6f52ab1e726724927e2e9d2ac358fdf58fbfcd6cf418
Deleted: sha256:7920a27f48f198550d59f64681b99441bbc3d2ce4778a855ce1ef9bafc96ae69
Deleted: sha256:a3c5a94eb1ea071c73dcea1969e0b71beea445d3b9d0735eaf6715d8e351434c
Deleted: sha256:e73eb58ed241e67a7a2c8589dde85eb72811eac1eb4cf3b586e40d2b9cc9d0c1
Deleted: sha256:b5d976dc9b0fa380affe1f6a17df18f02ab7debec2d35a0407fb863338591ed7
Deleted: sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046
[root@docker ~]# 

1.6 运行镜像

[root@docker ~]# docker run nginx //一次性运行镜像,在执行run操作的时候,如果没有该镜像,会先下载,在start该镜像
Unable to find image 'nginx:latest' locally //在本地没有发现nginx的镜像
latest: Pulling from library/nginx //没有找到镜像,就去公共仓库下载镜像
a330b6cecb98: Pull complete 
5ef80e6f29b5: Pull complete 
f699b0db74e3: Pull complete 
0f701a34c55e: Pull complete 
3229dce7b89c: Pull complete 
ddb78cb2d047: Pull complete 
Digest: sha256:c442ff5a3d9f6dc92dd8aee388ece42d965e002805e3edaaff34e79d8ee6d0a4
Status: Downloaded newer image for nginx:latest //下载完成
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/03 08:52:50 [notice] 1#1: using the "epoll" event method
2021/09/03 08:52:50 [notice] 1#1: nginx/1.21.1
2021/09/03 08:52:50 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/09/03 08:52:50 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2021/09/03 08:52:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
......省略部分内容
2021/09/03 08:52:57 [notice] 1#1: exit //这里执行的是 CTRL+C
[root@docker ~]#

1.7 镜像的导出、导入

[root@docker ~]# docker save -o nginx:latest nginx:latest  //导出镜像
//导出镜像 第一个nginx:latest 表示的导出镜像以后指定的镜像文件名
//导出镜像 第二个nginx:latest 表示的导出哪个镜像
[root@docker ~]# ls           
anaconda-ks.cfg  initial-setup-ks.cfg  nginx:latest  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@docker ~]# 
[root@docker ~]# docker rmi 822b7ec2aaf2 //删除镜像
Untagged: nginx:latest
Untagged: nginx@sha256:c442ff5a3d9f6dc92dd8aee388ece42d965e002805e3edaaff34e79d8ee6d0a4
Deleted: sha256:822b7ec2aaf2122b8f80f9c7f45ca62ea3379bf33af4e042b67aafbf6eac1941
Deleted: sha256:47dec9bde9e483e6265a6f52ab1e726724927e2e9d2ac358fdf58fbfcd6cf418
Deleted: sha256:7920a27f48f198550d59f64681b99441bbc3d2ce4778a855ce1ef9bafc96ae69
Deleted: sha256:a3c5a94eb1ea071c73dcea1969e0b71beea445d3b9d0735eaf6715d8e351434c
Deleted: sha256:e73eb58ed241e67a7a2c8589dde85eb72811eac1eb4cf3b586e40d2b9cc9d0c1
Deleted: sha256:b5d976dc9b0fa380affe1f6a17df18f02ab7debec2d35a0407fb863338591ed7
Deleted: sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046
[root@docker ~]# docker load < nginx\:latest 导入刚刚导出的镜像
d000633a5681: Loading layer [==================================================>]  72.53MB/72.53MB
63b5f2c0d071: Loading layer [==================================================>]  64.86MB/64.86MB
875b5b50454b: Loading layer [==================================================>]  3.072kB/3.072kB
ed94af62a494: Loading layer [==================================================>]  4.096kB/4.096kB
8e58314e4a4f: Loading layer [==================================================>]  3.584kB/3.584kB
d47e4d19ddec: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@docker ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    822b7ec2aaf2   20 hours ago   133MB
[root@docker ~]# 

二、容器操作

2.1 查看所有容器,包括不是启动状态的容器

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
a4c42a696a49   nginx     "/docker-entrypoint.…"   4 minutes ago   Exited (0) 4 minutes ago             naughty_swanson
[root@docker ~]# 

2.2 查看所有容器的id

[root@docker ~]# docker ps -aq
a4c42a696a49
[root@docker ~]# 

2.3 使容器开启并持续性运行

①:创建容器
docker create -it nginx:latest /bin/bash
-i:让容器的标准输入保持打开
-t:分配一个伪终端
-d:后台守护进程的方式运行
②:启动容器
docker start 容器id

[root@docker ~]# docker create -it nginx:latest /bin/bash
9ba3b6ffd7bf4e2587f415a203f6064432efc331335b0c9519278789a473dff6
[root@docker ~]# docker ps -a //容器id为:9ba3b6ffd7bf 状态为:已创建
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS     NAMES
9ba3b6ffd7bf   nginx:latest   "/docker-entrypoint.…"   9 seconds ago   Created                              romantic_banzai
a4c42a696a49   nginx          "/docker-entrypoint.…"   6 minutes ago   Exited (0) 5 minutes ago             romantic_stonebraker
[root@docker ~]# 
[root@docker ~]# docker start 9ba3b6ffd7bf 
9ba3b6ffd7bf
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
9ba3b6ffd7bf   nginx:latest   "/docker-entrypoint.…"   12 minutes ago   Up About a minute           80/tcp    romantic_banzai
a4c42a696a49   nginx          "/docker-entrypoint.…"   18 minutes ago   Exited (0) 17 minutes ago             romantic_stonebraker
[root@docker ~]#
有的时候持续性运行浪费资源,那么一次性执行如下操作
/usr/bin/bash 表示指定镜像运行的环境
-c 表示指定执行什么命令
[root@docker ~]# docker run centos:7 /usr/bin/bash -c ls /
anaconda-post.log
bin
dev
etc
home
lib
......省略部分内容
[root@docker ~]# docker ps -a //可以看出运行centos:7镜像,执行了ls操作之后就退出了
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
0f84d97ae586   centos:7       "/usr/bin/bash -c ls…"   32 seconds ago   Exited (0) 31 seconds ago             affectionate_bassi
9ba3b6ffd7bf   nginx:latest   "/docker-entrypoint.…"   15 minutes ago   Up 5 minutes                80/tcp    romantic_banzai
a4c42a696a49   nginx          "/docker-entrypoint.…"   21 minutes ago   Exited (0) 21 minutes ago             romantic_stonebraker
[root@docker ~]# 

2.4 容器的开启、关闭

docker start 容器id
docker stop 容器id

2.5 容器持续后台运行

docker run -d centos:7 /usr/bin/bash -c “while true;do echo hello; done”
-d 表示后台守护进程的方式运行
-c “while true;do echo hello; done” 做了一个死循环,模拟服务持续运行

[root@docker ~]# docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello; done"
c589495b247136e38713f72e81745bdddcd65df7e08311e22215da8354b5f38f
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
c589495b2471   centos:7       "/usr/bin/bash -c 'w…"   32 seconds ago   Up 32 seconds                         kind_wilbur
0f84d97ae586   centos:7       "/usr/bin/bash -c ls…"   13 minutes ago   Exited (0) 13 minutes ago             affectionate_bassi
9ba3b6ffd7bf   nginx:latest   "/docker-entrypoint.…"   28 minutes ago   Up 17 minutes               80/tcp    romantic_banzai
a4c42a696a49   nginx          "/docker-entrypoint.…"   34 minutes ago   Exited (0) 34 minutes ago             romantic_stonebraker
[root@docker ~]# 

2.6 进入容器

①docker run
②docker exec (容器必须是运行状态)

[root@docker ~]# docker run -it centos:7 /bin/bash
[root@795bf77b9a5c /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@795bf77b9a5c /]# exit  //这里使用的是 ctrl + d 组合快捷键退出容器
[root@docker ~]# 
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                            PORTS     NAMES
795bf77b9a5c   centos:7       "/bin/bash"              2 minutes ago    Exited (127) About a minute ago             happy_wozniak
c589495b2471   centos:7       "/usr/bin/bash -c 'w…"   9 minutes ago    Up 9 minutes                                kind_wilbur
0f84d97ae586   centos:7       "/usr/bin/bash -c ls…"   22 minutes ago   Exited (0) 22 minutes ago                   affectionate_bassi
9ba3b6ffd7bf   nginx:latest   "/docker-entrypoint.…"   37 minutes ago   Up 26 minutes                     80/tcp    romantic_banzai
a4c42a696a49   nginx          "/docker-entrypoint.…"   43 minutes ago   Exited (0) 42 minutes ago                   romantic_stonebraker
[root@docker ~]# docker exec -it c589495b2471 /bin/bash
[root@c589495b2471 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@c589495b2471 /]# exit //这里是手动输入 exit 退出容器
exit
[root@docker ~]# 

2.7 容器导出、导入

导出:docker export 容器id > 指定导出的容器文件名
导入(两种方式):
① docker import 导出的文件名 指定镜像名称:指定标签名称
② cat 导出的容器文件名 | docker import - 指定镜像名称:指定标签名称

[root@docker ~]# docker export c589495b2471 > centos7_01
[root@docker ~]# ls
anaconda-ks.cfg  centos7_01  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    822b7ec2aaf2   22 hours ago   133MB
centos       7         8652b9f0cb4c   9 months ago   204MB
[root@docker ~]# docker rm -f `docker ps -aq` //强制删除所有容器,这里为了演示容器的导出导入所以这样做,生产环境杜绝使用
795bf77b9a5c
c589495b2471
0f84d97ae586
9ba3b6ffd7bf
a4c42a696a49
[root@docker ~]# docker rmi 8652b9f0cb4c //删除centos:7的镜像文件
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
[root@docker ~]# docker import centos7_01 centos:7 //导入容器并生成镜像文件,centos:7表示指定的镜像文件名:标签名
sha256:45836fbbddb2c92905b73daf22b56b8b3b3c855d8e2dd4f080937ad47c164b70
[root@docker ~]# docker images //查看是否导入成功
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       7         45836fbbddb2   8 seconds ago   204MB
nginx        latest    822b7ec2aaf2   22 hours ago    133MB
[root@docker ~]# cat centos7_01 | docker import - centos:latest //另一种导入容器的方式
sha256:dda2c24c44187be74f4d690ad1833a58e3cc643c24a8d1288ccfd74b618a3647
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    dda2c24c4418   5 seconds ago   204MB
centos       7         45836fbbddb2   4 minutes ago   204MB
nginx        latest    822b7ec2aaf2   22 hours ago    133MB
[root@docker ~]# 
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-09-05 10:35:27  更:2021-09-05 10:36:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/29 20:06:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计