虚拟化容器,大数据,DBA,中间件,监控。

手动制作Docker镜像及dockerfile初步入门(五)

11 11月
作者:admin|分类:容器虚拟化

1.手动制作docker镜像

手动构建docker镜像的思路:首先运行一个容器,进入容器里面,安装所需程序,退出容器,提交容器为镜像即可

1.1.手动构建centos镜像

1.1.1.配置容器

1.首先运行一个容器
[root@docker01 ~]# docker run -d -it --privileged -p 2222:22 --name centos8.2 centos:latest  /usr/sbin/init 
--privileged: 指定容器是否为特权容器,特权容器拥有所有的功能
想要启动服务一定要首先运行/usr/sbin/init如果不运行init第一个进程的话启动服务会报错,报错内容:    System has not been booted with systemd as init system (PID 1). Can't operate.

2.配置yum仓库
[root@docker01 ~]# docker exec -it centos8.2 /bin/bash
[root@af89d2152479 /]# cd /etc/yum.repos.d/
[root@af89d2152479 yum.repos.d]# rm -rf *
[root@af89d2152479 yum.repos.d]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-8repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

3.安装并启动ssh服务
[root@af89d2152479 yum.repos.d]# yum -y install openssh-server
[root@af89d2152479 yum.repos.d]# systemctl start sshd
[root@af89d2152479 ~]# systemctl enable sshd

4.设置root口令
[root@af89d2152479 ]# yum -y install passwd
[root@af89d2152479 ~]# echo redhat | passwd --stdin root

5.安装其他所需软件
[root@af89d2152479 ~]# yum -y install vim 
[root@af89d2152479 ~]# yum -y install libvirt
[root@af89d2152479 ~]# yum -y install man


6.设置命令别名
[root@af89d2152479 ~]# vim .bashrc 
alias ls='ls --color'

7.通过ssh连接容器
[C:\~]$ ssh 192.168.81.210 2222

在这里插入图片描述

1.1.2.提交容器为镜像并测试

语法格式:

​ docker container commit 容器名/id 镜像名:版本

1.提交容器为镜像
[root@docker01 ~]# docker container commit centos8.2 jxl_centos8.2:v1
sha256:9a83652bb4121eab62ee6e2e3f254e97eccb9f4ec62aeb0c1d6e3c9421e3e323

2.查看镜像列表
[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jxl_centos8.2       v1                  9a83652bb412        3 minutes ago       551MB
centos              latest              831691599b88        6 days ago          215MB
nginx               latest              2622e6cca7eb        13 days ago         132MB
httpd24             latest              ccbcea8a6757        2 weeks ago         166MB
httpd               2.4                 ccbcea8a6757        2 weeks ago         166MB
http_test           latest              e06c3dbbfe23        2 years ago         171MB
第一个就是,3分钟前创建

3.验证容器是否可用
[root@docker01 ~]# docker run -d -it -p 4444:22 jxl_centos8.2:v1 /usr/sbin/sshd -D
100a00f81bb8f00b33c2aa0c655ef9fa54884adc4f0a00c5bff34b9a9a138f99

[root@docker01 ~]# docker ps -a -l
CONTAINER ID        IMAGE               COMMAND               CREATED              STATUS              PORTS                  NAMES
100a00f81bb8        jxl_centos8.2:v1    "/usr/sbin/sshd -D"   About a minute ago   Up About a minute   0.0.0.0:4444->22/tcp   great_joliot

4.登录容器
[root@docker01 ~]# docker exec -it great_joliot /bin/bash
[root@100a00f81bb8 /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 14:21 pts/0    00:00:00 /usr/sbin/sshd -D

在这里插入图片描述

1.2.手动构建nginx镜像

1.2.1.配置nginx容器

1.运行一个空镜像
[root@docker01 ~]# docker run -itd --privileged -p 80:80 centos:latest /usr/sbin/init
8c1143a3026345b40c5377f326036570c16426f4c27ce4f5b0d3eba110b05dd8


2.进入容器
[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
8c1143a30263        centos:latest       "/usr/sbin/init"    2 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp   nice_liskov
[root@docker01 ~]# docker exec -it nice_liskov /bin/bash

3.配置yum仓库
root         302     289  0 07:08 pts/1    00:00:00 ps -ef
[root@8c1143a30263 /]# cd /etc/yum.repos.d/
[root@8c1143a30263 yum.repos.d]# rm -rf *

4.安装nginx镜像
[root@8c1143a30263 yum.repos.d]# yum -y install nginx

5.启动镜像
[root@8c1143a30263 yum.repos.d]# systemctl start nginx

1.2.2.提交容器为镜像并测试

1.提交容器
[root@docker01 ~]# docker commit 8c1143a30263 centos82_nginx:v1
sha256:b5f6155f23898487672546f90de321457369016284011dec2614ba4469810558
[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos82_nginx      v1                  b5f6155f2389        7 seconds ago       313MB

2.运行测试镜像
[root@docker01 ~]# docker run -itd -p 80:80 centos82_nginx:v1 nginx -g 'daemon off;'
4bd852eb2289901efba6c537c6803009f6ebae8d6d1e28016330d0994a7b7e94

3.访问

在这里插入图片描述

1.3.手动构建nginx+ssh镜像

这次我们采用centos6.9作为系统底层

1.3.1.配置centos6.9容器

1.运行一个空的容器
[root@docker01 ~]# docker run -itd centos:6.9 
4291df5ab92dfd46d27fb5f756158c51318db5ab7a513e8b2b646613a4d29532
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4291df5ab92d        centos:6.9          "/bin/bash"         3 seconds ago       Up 3 seconds                            hopeful_edison

2.进入容器
[root@docker01 ~]# docker exec -it hopeful_edison /bin/bash

3.配置yum仓库
[root@4291df5ab92d /]# rm -rf /etc/yum.repos.d/*
[root@4291df5ab92d /]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

4.安装ssh、nginx服务
[root@4291df5ab92d /]# yum -y install nginx openssh-server passwd lrzsz vim

5.启动服务
ssh第一次启动会生成3对密钥用来远程连接,密钥存放在/etc/ssh
[root@4291df5ab92d /]# /etc/init.d/sshd start
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@4291df5ab92d /]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

6.由于要让容器运行时启动两个服务,并且最后一个服务要一直夯住也就是停留在终端,因此我们要写一个启动脚本,运行镜像时指定脚本即可
[root@3b6a5176ebd2 ~]# cat init.sh 
#!/bin/bash
/etc/init.d/sshd start
nginx -g 'daemon off;'

1.3.2.提交容器为镜像

1.提交容器为镜像
[root@docker01 ~]# docker commit hopeful_edison centos69_nginx_ssh:v1 
sha256:7fcf3792254968fc6249b806639eb3b08b2a364ea1df9ed02bc0a7b47fd5bdf8
[root@docker01 ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos69_nginx_ssh   v1                  7fcf37922549        5 seconds ago       488MB

2.测试并运行镜像
启动时指定两个服务分别映射的端口和启动服务的脚本
[root@docker01 ~]# docker run -it -d -p 80:80 -p 2222:22 centos69_nginx_ssh:v1 /bin/bash /root/init.sh
3b6a5176ebd2f01d688f07816fc07b3812cfa59faa186fc470cd46be60e95623
[root@docker01 ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                      NAMES
3b6a5176ebd2        centos69_nginx_ssh:v1   "/bin/bash /root/ini…"   3 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp, 0.0.0.0:2222->22/tcp   keen_haslett

3.测试
[root@docker01 ~]# curl -I 192.168.81.210
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 25 Jun 2020 09:05:18 GMT
Content-Type: text/html
Content-Length: 3698
Last-Modified: Tue, 07 May 2019 06:09:40 GMT
Connection: keep-alive
ETag: "5cd12124-e72"
Accept-Ranges: bytes

[root@docker01 ~]# ssh 192.168.81.210 -p 2222
The authenticity of host '[192.168.81.210]:2222 ([192.168.81.210]:2222)' can't be established.
RSA key fingerprint is SHA256:dj5jl/rpWB82pYSvnrWr4eZTJt1dxEfUX5MbuinM1Eg.
RSA key fingerprint is MD5:06:ec:4a:13:3d:92:5f:5f:70:7c:62:37:9d:18:c6:d8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.81.210]:2222' (RSA) to the list of known hosts.
root@192.168.81.210's password: 
[root@3b6a5176ebd2 ~]# exit 
logout
Connection to 192.168.81.210 closed.

2.利用dockerfile自动构建镜像

2.1.dockerfile简介

dockerfile主要组成部分

​ 1.基础镜像信息 FROM centos:6.9

​ 即使镜像不存在只要镜像名称和版本对了会去docker官方下载

​ 2.制作镜像操作指令 RUN yum -y install openssh-server

​ 3.容器启动时执行的指令 CMD ["/bin/bash"]

dockfile语法格式

​ 基础镜像信息,也就是基于哪个镜像去构建:FROM 镜像名:版本

​ 制作容器操作指令,制作镜像是需要用到的命令:RUN 命令

​ 传输文件:ADD 本地文件路径 容器文件路径

​ 容器启动时执行的指令,也就是docker run运行容器时默认的操作命令:CMD [“命令”,“参数1”,“参数2”]

创建dockerfile时文件名称一定要叫dockerfile

2.2.自动构建ssh镜像

1.创建ssh dockerfile存放目录
[root@docker01 ~]# mkdir /etc/dockerfile
[root@docker01 ~]# cd /etc/dockerfile
[root@docker01 dockerfile]# mkdir centos69_ssh
[root@docker01 dockerfile]# cd centos69_ssh

2.编写dockerfile内容
[root@docker01 centos69_ssh]# vim dockerfile
FROM centos:6.9

RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum -y install openssh-server
RUN /etc/init.d/sshd start
RUN echo redhat | passwd --stdin root

CMD ["/usr/sbin/sshd","-D"]

3.运行dockerfile自建镜像
[root@docker01 centos69_ssh]# docker build -t centos69_ssh_df:v2 .
............
Successfully built aea6ac3ac89c
Successfully tagged centos69_ssh_df:v2

4.查看镜像
[root@docker01 centos69_ssh]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos69_ssh_df      v2                  aea6ac3ac89c        7 seconds ago       367MB

5.运行镜像并验证
[root@docker01 centos69_ssh]# docker run -d -p 3333:22 centos69_ssh_df:v2 
3229194bcca16e0fe82e35c9c59e5ed1418092f21f31548edd14fa4022a1ed92
[root@docker01 centos69_ssh]# ssh 192.168.81.210 -p3333
root@192.168.81.210's password: 
[root@3229194bcca1 ~]# exit



6.扩展
dockerfile中的CMD还可以换种写法,利用ADD指令,将运行指令搞成执行脚本
[root@docker01 centos69_ssh]# cat dockerfile 
FROM centos:6.9

RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum -y install openssh-server
RUN /etc/init.d/sshd start
RUN echo redhat | passwd --stdin root
ADD init.sh /root/init.sh

CMD ["/bin/bash","/root/init.sh"]

在这里插入图片描述

2.3.自动构建ssh+nginx镜像

1.创建dockerfile目录
[root@docker01 dockerfile]# mkdir centos69_ssh_nginx
[root@docker01 dockerfile]# cd centos69_ssh_nginx

2.编写dockerfile
[root@docker01 centos69_ssh_nginx]# vim dockerfile
FROM centos:6.9
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ;curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum -y install openssh-server nginx vim lrzsz passwd
RUN /etc/init.d/sshd start
RUN /etc/init.d/nginx start
RUN echo redhat | passwd --stdin root
ADD init.sh /root/init.sh
CMD ["/bin/bash","/root/init.sh"]

3.编写启动脚本
[root@docker01 centos69_ssh_nginx]# vim init.sh
#!/bin/bash
/etc/init.d/sshd start
nginx -g 'daemon off;'

4.运行dockerfile自建镜像
[root@docker01 centos69_ssh_nginx]# docker build -t centos69_ssh_nginx_df:v1 .
........
Successfully built 29e8ca789370
Successfully tagged centos69_ssh_nginx_df:v1

5.查看镜像
[root@docker01 centos69_ssh_nginx]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
centos69_ssh_nginx_df   v1                  29e8ca789370        About a minute ago   488MB

6.运行镜像
[root@docker01 centos69_ssh_nginx]# docker run -d -p 85:80 -p 2022:22 centos69_ssh_nginx_df:v1 
ad29713f993190e18104c73d297b2236be9e4ffd327446882e7d5b6f11d7703a

7.检测
[root@docker01 centos69_ssh_nginx]#  docker run -d -p 85:80 -p 2022:22 centos69_ssh_nginx_df:v1 
a8f9077a126cf2aa0b76944f5082d3f6dbc7e611651c6b809aca783aa633d55d
[root@docker01 centos69_ssh_nginx]# curl -I 192.168.81.210:85
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 25 Jun 2020 10:39:56 GMT
Content-Type: text/html
Content-Length: 3698
Last-Modified: Tue, 07 May 2019 06:09:40 GMT
Connection: keep-alive
ETag: "5cd12124-e72"
Accept-Ranges: bytes

[root@docker01 centos69_ssh_nginx]# ssh 192.168.81.210 -p2022
root@192.168.81.210's password: 
[root@a8f9077a126c ~]# 

第二次执行会很快原因是有缓存

在这里插入图片描述

浏览401 评论0
返回
目录
返回
首页
Dockerfile指令详解(六) Docker网络、端口映射详解篇(三)