Docker容器化解决方案(镜像,容器的操作管理)

Docker镜像管理

bash 复制代码
[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL
nginx                              Official build of Nginx.                        19670     [OK]
unit                               Official build of NGINX Unit: Universal Web ...   25        [OK]
nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers fo...   88        
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  141       
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter for NGINX and NGIN...   38        
nginxinc/nginx-s3-gateway          Authenticating and caching gateway based on ...   6         
nginx/unit                         This repository is retired, use the Docker o...   64        
nginx/nginx-ingress-operator       NGINX Ingress Operator for NGINX and NGINX P...   2         
nginxinc/amplify-agent             NGINX Amplify Agent docker repository           1         
nginx/nginx-quic-qns               NGINX QUIC interop                              1         
nginxinc/ingress-demo              Ingress Demo                                    4         
nginxproxy/nginx-proxy             Automated nginx proxy for Docker containers ...   132       
nginxproxy/acme-companion          Automated ACME SSL certificate generation fo...   130       
bitnami/nginx                      Bitnami nginx Docker Image                      183       
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr...   32        
ubuntu/nginx                       Nginx, a high-performance reverse proxy & we...   112       
nginxproxy/docker-gen              Generate files from docker container meta-da...   16        
nginxinc/nginmesh_proxy_debug                                                      0         
nginxinc/mra-fakes3                                                                0         
kasmweb/nginx                      An Nginx image based off nginx:alpine and in...   7         
nginxinc/nginmesh_proxy_init                                                       0         
rancher/nginx-ingress-controller                                                   12        
nginxinc/mra_python_base                                                           0         
nginxinc/ngx-rust-tool                                                             0  
获取镜像 docker pull
bash 复制代码
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e1caac4eb9d2: Pull complete 
88f6f236f401: Pull complete 
c3ea3344e711: Pull complete 
cc1bb4345a3a: Pull complete 
da8fa4352481: Pull complete 
c7f80e9cdab2: Pull complete 
18a869624cb6: Pull complete 
Digest: sha256:c26ae7472d624ba1fafd296e73cecc4f93f853088e6a9c13c0d52f6ca5865107
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    e4720093a3c1   2 weeks ago    187MB
busybox      latest    3f57d9401f8d   6 weeks ago    4.26MB
ubuntu       18.04     f9a80a55f492   9 months ago   63.2MB
导出镜像
bash 复制代码
[root@localhost ~]# docker export -o upbeat_elbakyan.tar upbeat_elbakyan
[root@localhost ~]# ll
total 64000
-rw-------. 1 root root      706 Mar  4 06:39 anaconda-ks.cfg
-rw-------. 1 root root 65531904 Mar  6 16:34 upbeat_elbakyan.tar
导入镜像
bash 复制代码
[root@localhost ~]# scp upbeat_elbakyan.tar 192.168.142.139:~
The authenticity of host '192.168.142.139 (192.168.142.139)' can't be established.
ED25519 key fingerprint is SHA256:/w/yin6romP9ngxASS3yY3F5ubuzTP0RUwZdhAAkm/8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes  
Warning: Permanently added '192.168.142.139' (ED25519) to the list of known hosts.
root@192.168.142.139's password: 
upbeat_elbakyan.tar                                                                                                                      100%   62MB 125.4MB/s   00:00    

[root@service ~]# docker import upbeat_elbakyan.tar  wyx/ububtu:v1.0
sha256:6c33b82dae2f6112672aa55282e04935333b6565fcb031d1e534563c8d21b220
[root@service ~]# ll
总用量 64000
-rw-------. 1 root root     1293  9月  7 16:34 anaconda-ks.cfg
-rw-------. 1 root root 65531904  3月  6 16:43 upbeat_elbakyan.tar
删除镜像
bash 复制代码
$ docker image ls
REPOSITORY   TAG               IMAGE ID       CREATED                  SIZE
tomcat       8-jdk8-corretto   6aa794aeaf2e   Less than a second ago   379MB
nginx        1.21              fa5269854a5e   12 days ago              142MB

$ docker image rmi tomcat:8-jdk8-corretto
查看镜像详细信息
bash 复制代码
[root@localhost ~]# docker image inspect nginx:latest
[
    {
        "Id": "sha256:e4720093a3c1381245b53a5a51b417963b3c4472d3f47fc301930a4f3b17666a",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:c26ae7472d624ba1fafd296e73cecc4f93f853088e6a9c13c0d52f6ca5865107"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2024-02-14T18:24:57Z",
        "Container": "",
        "ContainerConfig": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": null,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "",
        "Author": "",
        "Config": {
            "Hostname": "",
            "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.25.4",
                "NJS_VERSION=0.8.3",
                "PKG_RELEASE=1~bookworm"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGQUIT"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 186721003,
        "VirtualSize": 186721003,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/b72b7d6b6e7c27959d83b9742515f24056bd33d3bbed689e531e4a928be38bc0/diff:/var/lib/docker/overlay2/d76e67b7ab328a999b4b27a1e87b6dbdd713cdedab1a29055420e831748994e7/diff:/var/lib/docker/overlay2/d655afb7c623c56785935b5b991b091a29124cbba48db1a90f2bd4ec00a9b0a2/diff:/var/lib/docker/overlay2/215485c9c16739b8dbdf42ae9a5bd5958278c125132105c7a6eb1b677bebc9b4/diff:/var/lib/docker/overlay2/e384eefe60a08be8edea94ed075e57e5c6a9848a3f41d1358fc30fd419de40b8/diff:/var/lib/docker/overlay2/3f926e4677932f685fb03c2f4d28ce29f9807bf0be8757636760673726ebe7f8/diff",
                "MergedDir": "/var/lib/docker/overlay2/85d73a86bba17fff3f5a635d9d47f8457d2cb30e5e857c8ac91753c3e398fdba/merged",
                "UpperDir": "/var/lib/docker/overlay2/85d73a86bba17fff3f5a635d9d47f8457d2cb30e5e857c8ac91753c3e398fdba/diff",
                "WorkDir": "/var/lib/docker/overlay2/85d73a86bba17fff3f5a635d9d47f8457d2cb30e5e857c8ac91753c3e398fdba/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ceb365432eec83dafc777cac5ee87737b093095035c89dd2eae01970c57b1d15",
                "sha256:84619992a45bb790ab8f77ff523e52fc76dadfe17e205db6a111d0f657d31d71",
                "sha256:3137f8f0c6412c12b46fd397866589505b4474e53580b4e62133da67bf1b2903",
                "sha256:7d52a4114c3602761999a4ea2f84a093c8fcc8662876acc4c3b92878b9948547",
                "sha256:188d128a188cafb013db48e94d9366f0be64083619f50b452cfd093e7affa260",
                "sha256:bcc6856722b7b251ad00728c9cd93b679c7836d5e6780b52316b56c20fd5be94",
                "sha256:61a7fb4dabcd05eba747fed22ff5264f82066d2bf8e16f78198f616e700f5aa7"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

容器的日常管理

容器的起/停
bash 复制代码
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS             PORTS     NAMES
70ef92bd8b03   ubuntu:18.04   "/bin/bash"   About an hour ago   Up About an hour             test
[root@localhost ~]#  docker container run -itd --name webserver -p 8080:80 nginx:latest
c1df05eda8cbe3ddfad22a48c5028f764740d10728982ed986f33146cbd36ab9
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS             PORTS                  NAMES
c1df05eda8cb   nginx:latest   "/docker-entrypoint...."   5 seconds ago       Up 4 seconds       0.0.0.0:8080->80/tcp   webserver
70ef92bd8b03   ubuntu:18.04   "/bin/bash"              About an hour ago   Up About an hour                          test
[root@localhost ~]# curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Wed, 06 Mar 2024 09:22:34 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 14 Feb 2024 16:03:00 GMT
Connection: keep-alive
ETag: "65cce434-267"
Accept-Ranges: bytes

[root@localhost ~]# docker container stop webserver
webserver
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS             PORTS     NAMES
70ef92bd8b03   ubuntu:18.04   "/bin/bash"   About an hour ago   Up About an hour             test
[root@localhost ~]# docker container rm webserver
webserver
[root@localhost ~]# curl -I http://localhost:8080
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Connection refused
进入容器的方法
bash 复制代码
[root@localhost ~]# docker container run -itd --name webserver -p 8080:80 nginx:latest
b568030e0f32188f35c90a6cde6ac57d9d1c122d3cf82db8d76452546a631b0a
[root@localhost ~]# docker container exec -it webserver /bin/bash
root@b568030e0f32:/# 
容器与宿主机间传输文件
bash 复制代码
[root@localhost ~]# docker cp  upbeat_elbakyan.tar upbeat_elbakyan:/tmp
Successfully copied 65.5MB to upbeat_elbakyan:/tmp
[root@localhost ~]# docker exec -it upbeat_elbakyan /bin/bash
root@70ef92bd8b03:/# ls /tmp
upbeat_elbakyan.tar
root@70ef92bd8b03:/# exit
exit
相关推荐
Java陈序员1 天前
轻量强大!一款现代化的 Kubernetes 集群管理与监控工具!
云原生·容器·kubernetes
爱吃橘子橙子柚子2 天前
3CPU性能排查总结(超详细)【Linux性能优化】
运维·cpu
Sheffield3 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield3 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
马艳泽3 天前
win10下运行Start Broker and Proxy报错解决
docker
舒一笑4 天前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
NineData4 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
用户13573999256604 天前
Windows 从 0 搭建 WSL2 原生 AI 开发环境:Codex + Docker + VSCode
docker
vi_h4 天前
在 macOS 上通过 Docker 安装并运行 Ollama(详细可执行教程)
macos·docker·ollama
黑心老魔4 天前
通过 Docker 创建开发环境
docker·开发环境