卧疾丰暇豫,翰墨时间作。这篇文章主要讲述docker镜像制作相关的知识,希望能为你提供帮助。
镜像里面没有内核,而是启动容器后,使用宿主机内核
镜像本身只提供相应的rootfs,即系统正常运行所必须的用户空间的文件系统,比如:/dev /proc /etc/等目录
所以容器当中基本是没有/boot目录的
/boot当中保存的就是与内核相关的文件和目录
Docker 的镜像制作分为:手动制作和自动制作(基于 DockerFile)
?手动制作yum版nginx镜像?
docker pull centos#基于基础镜像制作
docker run -it docker.io/centos/bin/bash
#1.下载yum源
yum install wget -y
cd /etc/yum.repos.d/
rm -rf ./*
wget -O /etc/yum.repos.d/CentOS Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#2.安装常用命令
yum install -y vim wget pcre tree pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
#3.nginx安装
yum install nginx -y
vim /etc/nginx/nginx.conf关闭nginx后台运
---------------------------------------
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
daemon off;
---------------------------------------
echo "docker nginx test" > /usr/share/nginx/html/index.html
#4.提交为镜像
docker commit -m "nginx image" 9fa5275f4bd6 project/centos-nginx:v1
#5.从自己镜像启动容器
docker run -d -p 81:80 --name my-centos-nginx project/centos-nginx:v1 /usr/sbin/nginx
?自动制作镜像?
?Dockerfile?
#编写Dockerfile
#生成镜像的时候会在执行命令的当前目录查找Dockerfile文件,且D必须大写
书写格式:
--------------------------------------------------------
#My Dockerfile
#除了注释行之外的第一行,必须是Form xxx(xxx基础镜像)
From centos#定义基础镜像,如果本地没有会从远程仓库下载
MAINTAINER taowenwu 794411268@qq.com #镜像维护者的信息
USER#指定该容器运行时的用户名和UID,后续的RUN命令也会使用这里指定的用户执行
WORKDIR /a
WORKDIR b#指定工作目录,最终为/a/b
VOLUME ["/dir_1" ,"/dir_2"..]#设置容器挂载主机目录
ENV name jack#设置容器变量,常用于向容器传递用户密码等
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel--release--latest--7.noarch.rpm
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.10.3.tar.gz /usr/local/src#自动解压压缩包
RUN cd /usr/local/src/nginx-1.10.3 & & ./configure --prefix=/usr/local/nginx --with-http_sub_module & & make & & make install
RUN cd /usr/local/nginx
ADD nginx.conf /usr/local/nginx/conf/nginx.conf#nginx.conf配置文件是自己准备的
RUN useradd nginx -s /sbin/nologin
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
RUN echo "test nginx page " > /usr/local/nginx/html/index.html
EXPOSE 80 443 #向外开放的端口,多个端口用空格做间隔,启动容器时候,-p 需要使用此端向外映射
CMD["nginx"]#运行的命令,每个Dockerfile 只有一条,如果有多条则只有最后一条被执行
#如果在从该镜像启动容器的时候也指定了命令,那么指定的命令会覆盖Dockerfile构建的镜像里面的CMD命令,即指定的命令优先级更高,Dockerfile的优先级较低一些
?jdk镜像制作?
docker pull centos
docker run -it docker.io/centos /bin/bash
cd /opt/
mkdir dockerfile/web/nginx,tomcat,jdk,apache,system/centos,ubuntu,redhat -pv
#目录结构按照业务类型或系统类型等方式划分,方便后期镜像比较多的时候进行分类
[root@c4085adb0055 opt]# tree
.
`-- dockerfile
|-- system
||-- centos
||-- redhat
|`-- ubuntu
`-- web
|-- apache
|-- jdk
|-- nginx
`-- tomcat
vim Dockerfile
------------------------------------------------------------------
#JDK Base Image
FROM centos:latest
MAINTAINER taowenwu 794411268@qq.com
ADD jdk-8u162-linux-x64.tar.gz /usr/local/src
RUN ln -sv /usr/local/src/jdk1.8.0_162 /usr/local/jdk
ADDprofile /etc/profile
ENV java_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASPATH $JAVA_HOME/lib/:$JRE_HOME/lib
ENV PATH $PATH:$JAVA_HOME/bin
RUN rm -f /etc/localtime & & ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime & & echo "Aisa/Shanghai" > /etc/timezone
------------------------------------------------------------------
准备文件
profile
jdk-8u162-linux-x64.tar.gz
Dockerfile
#构建镜像
docker build -t 192.168.47.113/project/centos-jdk:v1 .
#测试
root@ubuntu:/data/dockerfile/web/jdk# docker run -it --rm 192.168.47.113/project/centos-jdk:v1 bash
[root@770c22aba777 /]# java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
[root@770c22aba777 /]# date
Tue Jan 25 20:46:51 CST 2022
#上传到harbor
docker push 192.168.47.113/project/centos-jdk:v1
【docker镜像制作】?tomcat镜像制作?
#以jdk镜像构建tomcat 8 base镜像
vim Dockerfile
------------------------------------------------------------------
#Tomcat Base Image
FROM 192.168.47.113/project/centos-jdk:v1
MAINTAINER taowenwu 794411268@qq.com
RUN useradd www -u 2000
#ADD del_tomcatlog.sh /root/script
#RUN echo "0 2 * * */bin/bash /root/script/del_tomcatlog.sh & > /dev/null" > > /var/spool/cron/root
#env settinf
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.33
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR $CATALINA_HOME/webapps
#tomcat settinf
RUN mkdir /apps
ADD apache-tomcat-8.5.33.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.33 /apps/tomcat
------------------------------------------------------------------
准备文件
apache-tomcat-8.5.33.tar.gz
Dockerfile
#构建镜像
docker build -t 192.168.47.113/project/tomcat-base:v1 .
#测试
docker run -it--rm 192.168.47.113/project/tomcat-base:v1 bash
#上传到harbor
docker push 192.168.47.113/project/tomcat-base:v1
?tomcat业务镜像?
vim Dockerfile
---------------------------------------------
#Nginx Base Image
FROM 192.168.47.113/project/tomcat-base:v1
MAINTAINER taowenwu 794411268@qq.com
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
COPY server.xml /apps/tomcat/conf/server.xml
RUN mkdir /apps/tomcat/webapps/myapp
ADD index.html /apps/tomcat/webapps/myapp/
RUN chown www.www /apps/ -R
RUN chmod +x/apps/tomcat/bin/run_tomcat.sh
EXPOSE 808
推荐阅读
- #yyds干货盘点#HP服务器无法进入“Intelligent Provisioning”故障
- docker-网络
- docker仓库
- windows系统基础
- #yyds干货盘点#通过 SSH 管理远程连接
- #yyds干货盘点#YUV采样
- 单机编排之-Docker Compose
- docker介绍
- #yyds干货盘点#Linux之重定向