首页 > 系统相关 >Linux容器篇-Docker镜像的使用

Linux容器篇-Docker镜像的使用

时间:2024-07-01 17:02:01浏览次数:3  
标签:Linux nginx ubuntu 镜像 Docker root docker localhost

文章目录


前言

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。


一、列出镜像列表

我们可以使用 docker images 来列出本地主机上的镜像。

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
test/ubuntu   v1        f67ec2cf1908   2 days ago    72.8MB
ubuntu        latest    ba6acccedd29   2 years ago   72.8MB

各个选项说明:

  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

我们如果要使用版本为18.04的ubuntu系统镜像来运行容器时,命令如下:

[root@localhost ~]# docker run -i -t ubuntu:18.04 /bin/bash
Unable to find image 'ubuntu:18.04' locally
18.04: Pulling from library/ubuntu
284055322776: Pull complete 
Digest: sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
Status: Downloaded newer image for ubuntu:18.04
root@6e5bc8f53b89:/# 

参数说明:

  • -i: 交互式操作。
  • -t: 终端。
  • ubuntu:18.04: 这是指用 ubuntu 18.04 版本镜像为基础来启动容器。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。

二、获取一个新镜像

当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
test/ubuntu   v1        f67ec2cf1908   2 days ago    72.8MB
ubuntu        latest    ba6acccedd29   2 years ago   72.8MB
ubuntu        18.04     5a214d77f5d7   2 years ago   63.1MB

[root@localhost ~]# docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
test/ubuntu   v1        f67ec2cf1908   2 days ago    72.8MB
ubuntu        20.04     ba6acccedd29   2 years ago   72.8MB
ubuntu        latest    ba6acccedd29   2 years ago   72.8MB
ubuntu        18.04     5a214d77f5d7   2 years ago   63.1MB

下载完成后,我们可以直接使用这个镜像来运行容器。

三、查找镜像

我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/,国内可能会因为网络的原因无法访问,这也是正常的。

我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

[root@localhost ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL
nginx                              Official build of Nginx.                        19975     [OK]
unit                               Official build of NGINX Unit: Universal Web …   32        [OK]
nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers fo…   92        
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  152       
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter for NGINX and NGIN…   42        
nginx/unit                         This repository is retired, use the Docker o…   63        
nginx/nginx-ingress-operator       NGINX Ingress Operator for NGINX and NGINX P…   2         
nginxinc/nginx-s3-gateway          Authenticating and caching gateway based on …   6         
nginx/nginx-quic-qns               NGINX QUIC interop                              1         
nginxinc/amplify-agent             NGINX Amplify Agent docker repository           1         
nginxproxy/nginx-proxy             Automated nginx proxy for Docker containers …   140       
nginxinc/ingress-demo              Ingress Demo                                    4         
nginx/unit-preview                 Unit preview features                           0         
nginxproxy/acme-companion          Automated ACME SSL certificate generation fo…   134       
bitnami/nginx                      Bitnami container image for NGINX               190       
ubuntu/nginx                       Nginx, a high-performance reverse proxy & we…   114       
nginxproxy/docker-gen              Generate files from docker container meta-da…   17        
nginxinc/mra-fakes3                                                                0         
kasmweb/nginx                      An Nginx image based off nginx:alpine and in…   8         
nginxinc/ngx-rust-tool                                                             0         
nginxinc/mra_python_base                                                           0         
bitnami/nginx-ingress-controller   Bitnami container image for NGINX Ingress Co…   34        
bitnami/nginx-exporter             Bitnami container image for NGINX Exporter      5         
rancher/nginx                                                                      2
  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
  • AUTOMATED: 自动构建。

四、拉取镜像

我们决定使用上面的 nginx 官方版本的镜像,使用命令 docker pull 来下载镜像。

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

下载完成后,我们就可以使用这个镜像了。

[root@localhost ~]# docker run nginx

五、删除镜像

镜像删除使用 docker rmi 命令,比如我们删除 nginx 镜像:

[root@localhost ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85

-f 强制删除

六、创建镜像

当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

1、从已经创建的容器中更新镜像,并且提交这个镜像
2、使用 Dockerfile 指令来创建一个新的镜像

1、更新镜像

更新镜像之前,我们需要使用镜像来创建一个容器。

[root@localhost ~]# docker run -it ubuntu:18.04 /bin/bash
root@851650e41f41:/# 

在运行的容器内使用 apt-get update 命令进行更新。

root@851650e41f41:/# apt-get update 
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [102 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [23.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [102 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [1688 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB] 
Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]     
Get:9 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]          
Get:10 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [3373 kB]   
Get:11 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1637 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]                                                                                                                                                               
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1728 kB]                                                                                                                                                       
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3786 kB]                                                                                                                                                             
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [30.8 kB]                                                                                                                                                       
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2411 kB]                                                                                                                                                         
Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [64.0 kB]                                                                                                                                                           
Get:18 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [20.6 kB]                                                                                                                                                       
Fetched 28.2 MB in 8s (3357 kB/s)                                                                                                                                                                                                                
Reading package lists... Done

在完成操作之后,输入 exit 命令来退出这个容器。

root@851650e41f41:/# exit
exit
[root@localhost ~]#

此时 ID 为851650e41f41 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。

[root@localhost ~]# docker commit -m "already update" -a="Logan"  851650e41f41 Logan/ubuntu:v2
sha256:aac24c1bc19d27ccc4da2f1652625a1e1169ff8af718293746c43d4558adee8a
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
Logan/ubuntu   v2        aac24c1bc19d   3 seconds ago   109MB
test/ubuntu    v1        f67ec2cf1908   2 days ago      72.8MB
ubuntu         20.04     ba6acccedd29   2 years ago     72.8MB
ubuntu         latest    ba6acccedd29   2 years ago     72.8MB
ubuntu         18.04     5a214d77f5d7   2 years ago     63.1MB

各个参数说明:

  • -m: 提交的描述信息
  • -a: 指定镜像作者
  • 851650e41f41:容器 ID
  • Logan/ubuntu:v2: 指定要创建的目标镜像名

2、构建镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

关键字

FROM         # 基础镜像,当前新镜像是基于哪个镜像的
MAINTAINER   # 镜像维护者的姓名混合邮箱地址
RUN          # 容器构建时需要运行的命令
EXPOSE       # 当前容器对外保留出的端口
WORKDIR      # 指定在创建容器后,终端默认登录的进来工作目录,一个落脚点
ENV          # 用来在构建镜像过程中设置环境变量
ADD          # 将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
COPY         # 类似ADD,拷贝文件和目录到镜像中!
VOLUME       # 容器数据卷,用于数据保存和持久化工作
CMD          # 指定一个容器启动时要运行的命令,dockerFile中可以有多个CMD指令,但只有最后一个生效!
ENTRYPOINT   # 指定一个容器启动时要运行的命令!和CMD一样
ONBUILD      # 当构建一个被继承的DockerFile时运行命令,父镜像在被子镜像继承后,父镜像的ONBUILD被触发

编写一个Dockerfile,如下示例:

[root@localhost ~]# cat Dockerfile 
FROM centos:6.7
MAINTAINER logan
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN useradd admin
RUN     /bin/echo 'admin:123456' |chpasswd
EXPOSE 80

CMD echo $MYPATH
CMD echo "---Hello wokld!---"
CMD /bin/bash

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的
第一条FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令,安装了什么
然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像

[root@localhost ~]# docker build -f Dockerfile -t mycentos:v1.0 .
[+] Building 16.0s (8/8) FINISHED                                                                                                                                                                                                  docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                                         0.0s
 => => transferring dockerfile: 243B                                                                                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/centos:6.7                                                                                                                                                                               15.2s
 => [internal] load .dockerignore                                                                                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                                                                                              0.0s
 => [1/4] FROM docker.io/library/centos:6.7@sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1                                                                                                                          0.0s
 => CACHED [2/4] WORKDIR /usr/local                                                                                                                                                                                                          0.0s
 => [3/4] RUN useradd admin                                                                                                                                                                                                                  0.2s
 => [4/4] RUN     /bin/echo 'admin:123456' |chpasswd                                                                                                                                                                                         0.2s
 => exporting to image                                                                                                                                                                                                                       0.2s
 => => exporting layers                                                                                                                                                                                                                      0.1s
 => => writing image sha256:81ee6a7ca5774eac3c684e221ca8942b497d5c6abdb8759099c9de747c5c2161                                                                                                                                                 0.0s
 => => naming to docker.io/library/mycentos:v1.0                                                                                                                                                                                             0.0s
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
mycentos       v1.0      81ee6a7ca577   10 seconds ago   191MB

参数说明:

  • -t :指定要创建的目标镜像名
  • -f : 指定Dockerfile
  • . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

七、设置镜像标签

我们可以使用 docker tag 命令,为镜像添加一个新的标签。
使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。

[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
mycentos       v1.0      81ee6a7ca577   4 minutes ago    191MB
[root@localhost ~]# 
[root@localhost ~]# docker tag 81ee6a7ca577 logan/centos76:v2.0
[root@localhost ~]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
mycentos         v1.0      81ee6a7ca577   6 minutes ago    191MB
logan/centos76   v2.0      81ee6a7ca577   6 minutes ago    191MB
[root@localhost ~]# 

总结

随手写写,没有总结,觉得对你有帮助,可以点个赞,不喜欢直接划走。

标签:Linux,nginx,ubuntu,镜像,Docker,root,docker,localhost
From: https://blog.csdn.net/qq_52494169/article/details/140087821

相关文章

  • 深入解析Linux的使用(上)
    深入解析Linux的使用(上)Linux作为一种开源操作系统,广泛应用于服务器、桌面计算机和嵌入式设备。它以稳定、安全和高效著称。对于新手来说,学习如何使用Linux是一个非常有价值的技能。本文将带你从基础开始,逐步深入了解Linux的使用,包括基本命令、文件系统、权限管理和软件安装......
  • 虚拟机安装麒麟Linux系统教程
    一、在windows11系统安装VirtualBox7.0.2控件B站详细视频讲解,跟踪视频步骤走即可(巨好用!)https://www.bilibili.com/video/BV1g8411Y75r/?spm_id_from=333.788.top_right_bar_window_custom_collection.content.click&vd_source=66c4e874516254978f3730ca1f92a213二、安装好虚拟......
  • 利用阿里云API获取实时天气信息|C语言Linux网络编程练手小项目
    文章目录目录文章目录前言一、HTTP数据请求1.HTTP概述2.数据请求方式3.JSON格式二、阿里云API获取,使用1.购买(白嫖)API接口步骤2.API使用代码编写3.编译程序 4.运行结果总结前言在项目开发时常常需要显示各种信息,如时间、天气、温度、空气质量指数等等。在......
  • 【Linux】解锁权限的神秘面纱,让你的系统更安全、更高效!
    XShell原理+权限1.Shell命令以及运行原理*1.1Shell外壳1.2shell周边知识2.Linux权限的概念*2.1用户2.2用户切换2.3sudo3.Linux权限管理*3.1文件访问者的分类3.2文件类型3.3file指令3.4文件访问权限3.5文件权限值的表示方法4.文件访问权限的设......
  • 【Docker】Docker安全与最佳实践:保护你的容器化应用程序
    Docker安全与最佳实践:保护你的容器化应用程序一、保持Docker更新*1\.使用容器编排工具2.蓝绿部署3.滚动更新4.就地更新5.监控和回滚二、最小权限原则三、网络隔离四、其他安全措施前言Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到......
  • 【Linux命令详解 - ssh命令】 ssh命令用于远程登录到其他计算机,实现安全的远程管理
    文章标题简介一,参数列表二,使用介绍*1\.连接远程服务器2.使用SSH密钥登录*2.1生成密钥对2.2将公钥复制到远程服务器3.端口转发*3.1本地端口转发3.2远程端口转发4.X11转发5.文件传输与远程命令执行*5.1文件传输*5.1.1从本地向远程......
  • Linux下二进制可执行文件分析 (nm,readelf,objdump 命令使用)
    最近在调试一些问题,发现几个命令很实用,记录一下。一 背景    也许大家都遇到过这种场景,就是有二进制代码,比如深度分析下此文件到底是什么格式的图片等,这篇文章就记录我分析下二进制可执行文件的过程,已经自己读写二进制文件的一些坑。分析的二进制执行文件为linux下......
  • docker
    dockerDocker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业余项目。它基于Google公司推出的Go语言实现。加了Linux基金会,遵从了Apache2.0协议,项目代码在上进行维护。容器化技术,区分于虚拟机,又类虚拟机,兼具虚拟机隔离的优点,又有速度快的优点容器......
  • 怎么在Linux上执行main方法
      有时候我们写了一个测试程序用的main方法但是我们一般用springboot然后用controller通过url调用 但是如果使用下面这个方法就可以直接把jar包打上去然后Java-jar直接执行main方法 在配置文件xml里面添加<!--JAR插件--><plugin>......
  • Linux系统的硬件信息
    查看Linux系统的硬件信息【1】查看内核信息uname用于显示系统的内核信息option-s:显示内核名称-r:显示内核版本[root@bogon/]#uname-aLinuxbogon4.18.0-513.5.1.el8_9.x86_64#1SMPFriSep2905:21:10EDT2023x86_64x86_64x86_64GNU/Linux[root@bogo......