Docker 基础命令+容器镜像

Docker镜像概述

有关Docker的一些理论和关于镜像的理论我在上一篇博客中已经写过,有需要的可以直接翻看上一篇微博。
这里是上一篇微博的链接,点我跳转!!

Docker镜像制作的三种方式

  1. 基于已有镜像创建
  2. 基于本地模板创建
  3. 基于Dockerfile
    在这里插入图片描述Dockerfile中每个指令都会创建一个新的镜像层
    镜像层将被缓存和复用
    当Dockerfile的指令修改了,复制的文件变化了,或者构建镜像时指定的变量不同了,对应的镜像层缓存就会失效
    某一层的镜像缓存失效之后,它之后的镜像层缓存都会失效
    镜像层是不可变的,如果在某一层中添加一个文件,然后再下一层中删除它,则镜像中依然会包含该文

Dockerfile

Dockerfile是由一组指令组成的文件
Dockerfile结构分成四个部分:

  1. 基础镜像信息
  2. 维护者信息
  3. 镜像操作指令
  4. 容器启动时指定指令
    在这里插入图片描述

直接展示!

镜像建立

一、基于已有镜像创建

将容器里运行的程序和运行环境打包成新的镜像
docker commit 选项 容器ID/名称 仓库名称:标签
常见选项:
-m 说明信息
-a 作者信息
-p 生成过程中停止容器的运行

[root@5centos ~]# docker create -it centos:8 /bin/bash	##创建容器
d61a1f121f3a48ed81ef00da86bb7c573f81235a1fcffb6115e439fd64c328bd
[root@5centos ~]# docker ps -a	##查看ID
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d61a1f121f3a        centos:8            "/bin/bash"         30 seconds ago      Created                                 wizardly_varahamihira
[root@5centos ~]# docker commit -m "基于镜像建" -a "橘籽酱" d61a1f121f3a oracentos:8		##基于已有容器镜像创建
sha256:cd067ed5ce2254d10b1d7f96b97ba371cfac18899e53b1d39594ac56dd21f73b
[root@5centos ~]# docker images	##查看镜像
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
oracentos            8                   cd067ed5ce22        36 seconds ago      237MB
2233466866/centos8   latest              a2f2e369e78e        6 months ago        237MB
centos               8                   a2f2e369e78e        6 months ago        237MB

二、基于本地模板创建

通过导入系统模板文件可以生成镜像,模板可以从OPENVZ开源项目下载
地址为:https://wiki.openvz.org/Download/template/precreated
或者使用wget下载:wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz

我在 /opt 下有一个 centos8的镜像

[root@5centos opt]# ls
centos8  containerd  rh
[root@5centos opt]# cat centos8 |docker import - os8:cent
sha256:9997abab01f04c169446e89555a155bd36f040f261134a1740c78a794cba9e3e
[root@5centos opt]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
os8                  cent                9997abab01f0        22 seconds ago      245MB
oracentos            8                   cd067ed5ce22        8 minutes ago       237MB
2233466866/centos8   latest              a2f2e369e78e        6 months ago        237MB
centos               8                   a2f2e369e78e        6 months ago

三、基于Dockerfile创建

使用Dockerfile创建一个httpd镜像并运行(不为别的,就因为apahce的httpd简单好装)
Dockerfile是由一组指令组成的文件
Dockerfile结构分成四个部分:

  1. 基础镜像信息
  2. 维护者信息
  3. 镜像操作指令
  4. 容器启动时指定指令
[root@5centos opt]# mkdir /apache
[root@5centos opt]# cd /apache
[root@5centos apache]# vim Dockerfile
FROM centos:8	##基于的基础镜像
MAINTAIER Ora <CN-Ora>	##维护人员信息
RUN yum -y update	##升级所有包同时也升级软件和系统bai内核du
RUN yum -y install httpd	##安装httpd
EXPOSE 80	##开启内部端口80
ADD index.html /var/www/html/index.html	##导入文件
ADD run.sh /run.sh	##同上
RUN chmod +x /run.sh	##加权
CMD ["/run.sh"]		##启动容器时启动脚本

[root@5centos apache]# echo "<h1>This is Ora's Web</h1>" > index.html
[root@5centos apache]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/*		##清除容器内的httpd启动
exec /usr/sbin/apachectl -D FOREGROUND	##启动apache
[root@5centos apache]# ls
Dockerfile  index.html  run.sh
[root@5centos apache]# docker build -t httpd:centos .	##创建镜像,记得空格点
Sending build context to Docker daemon  4.096kB
Step 1/9 : FROM centos:8
 ---> a2f2e369e78e
Step 2/9 : MAINTAINER Ora <CN-Ora>
 ---> Running in dff28da90a0d
Removing intermediate container dff28da90a0d
 ---> 438c17c20473
Step 3/9 : RUN yum -y update
 ---> Running in bff3e9d792f1
 ……省略部分……
 Successfully tagged httpd:centos

查看镜像

[root@5centos apache]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
httpd                centos              6b85ffdb4c45        23 seconds ago      616MB

将这个镜像运行

[root@5centos apache]# docker run -d -p 1080:80 httpd:centos	##-p 指定端口号,-P 随机端口号'
a84372f77bc29944addfc436ea6246e399d72d1de123272e759bb04256d6790c
[root@5centos apache]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
a84372f77bc2        httpd:centos        "/run.sh"           36 seconds ago      Up 34 seconds       0.0.0.0:1080->80/tcp   keen_hellman

在这里插入图片描述

Docker建立私有仓库

私有仓库设置步骤

  1. 下载registry镜像
  2. 客户端设置daemon.json文件,指定私有仓库位置
  3. 生成registry容器,开放5000端口
  4. 镜像打标签
  5. 上传镜像,docker push
  6. 下载镜像,docker pull

下载registry镜像

[root@5centos /]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
cbdbe7a5bc2a: Pull complete 
47112e65547d: Pull complete 
46bcb632e506: Pull complete 
c1cc712bcecd: Pull complete 
3db6272dcbfa: Pull complete 
Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

客户端设置daemon.json文件,指定私有仓库位置

[root@5centos /]# vim /etc/docker/daemon.json
{
    
    
  "insecure-registries": ["20.0.0.5:5000"],	##切记逗号别忘了,registry默认端口为5000
  "registry-mirrors": ["https://xh6uo4bd.mirror.aliyuncs.com"]
}
[root@5centos /]# systemctl restart docker

生成registry容器,开放5000端口

[root@5centos /]# docker create -it registry /bin/bash
4485837c9052ccc202b343d15ad92dcce29018c842639b80389fde7d5c4697d9
[root@5centos /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
4485837c9052        registry            "/entrypoint.sh /bin…"   4 seconds ago       Created                                           optimistic_gould
a84372f77bc2        httpd:centos        "/run.sh"                12 minutes ago      Exited (137) 50 seconds ago                       keen_hellman
[root@5centos /]# docker run -d -p 5000:5000 -v /opt:/opt registry
447a57de666748033cc1155e5e76bc4dff9c4c674e0dbcfa8da02aa5b5681a1f

镜像打标签

[root@5centos /]# docker tag httpd:centos 20.0.0.5:5000/httpd
[root@5centos /]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
20.0.0.5:5000/httpd   latest              6b85ffdb4c45        16 minutes ago      616MB
httpd                 centos              6b85ffdb4c45        16 minutes ago      616MB
registry              latest              2d4f4b5309b1        3 months ago        26.2MB

上传镜像

[root@5centos /]# docker push 20.0.0.5:5000/httpd 
The push refers to repository [20.0.0.5:5000/httpd]
61e6c1eb3010: Pushed 
4f96d076e862: Pushed 
d9d346bd3bd3: Pushed 
51693b2732fd: Pushed 
3909c87a602a: Pushed 
2f00c0cf81bb: Pushed 
0683de282177: Pushed 
latest: digest: sha256:f7cbad7005013d3ef1958c86bf7c11f83ddb465f4a7ab922cc6827bbf0b66d44 size: 1783
[root@5centos /]# 

下载镜像测试

[root@5centos /]# curl -XGET http://20.0.0.5:5000/v2/_catalog
{
    
    "repositories":["httpd"]}	##获取私有库镜像列表
[root@5centos /]# docker pull 20.0.0.5:5000/httpd
Using default tag: latest
latest: Pulling from httpd
Digest: sha256:f7cbad7005013d3ef1958c86bf7c11f83ddb465f4a7ab922cc6827bbf0b66d44
Status: Image is up to date for 20.0.0.5:5000/httpd:latest
20.0.0.5:5000/httpd:latest

猜你喜欢

转载自blog.csdn.net/Ora_G/article/details/108695879