Docker镜像制作!
Posted handsomeboy-东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker镜像制作!相关的知识,希望对你有一定的参考价值。
基于dockerfile制作镜像
Dockerfile操作指令
指令 | 含义 |
---|---|
FROM镜像 | 指定镜像所基于的镜像,第一条命令必须是FROM指令,没创建一个镜像就要一条FROM指令 |
MAINTAINER名字 | 说明镜像信息 |
CMD[“要运行的程序”,“参数1”,“参数2”] | 容器启动时默认允许的命令或者脚本,该指令只能有一条,如果有多条则只有最后一条被执行 |
EXPOSE端口号 | 指定镜像加载到docker是要开启的端口 |
ENV 环境变量 变量值 | 设置环境变量 |
ADD 源文件/目录 目标文件/目录 | 将源文件复制到目标文件,如果是压缩包则可以直接解压,可以复制跨界点数据 |
COPY 源文件/目录 目标文件/目录 | 将宿主机指定的文件复制到image层 |
VOLUME[“目录”] | 在容器中创建一个挂载点 |
USER用户名/UID | 指定允许容器时的用户 |
WORKDIR 路径 | 相当于cd命令,路径跳转 |
ONBUILD 命令 | 指定所生成的镜像为基础镜像时要运行的命令 |
HEALTHCHECK | 健康检查 |
制作nginx镜像
- 准备工作:下载安装好docker,然后执行下面命令
[root@docker ~]# mkdir docker
[root@docker ~]# mkdir nginx && cd nginx/ #拷贝nginx-1.12.2.tar.gz包到目录下
[root@docker nginx]# vim Dockerfile
FROM centos:7 ##基于基础环境
MAINTAINER this is nginx image ##镜像信息
RUN yum -y update && yum -y install pcre-devel zlib-devel gcc gcc-c++ make &> /dev/nulll #依赖环境
RUN useradd -M -s /sbin/nologin nginx ##创建nginx用户
ADD nginx-1.12.2.tar.gz /usr/local/src ##解压包
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
RUN ./configure --prefix=/usr/local/nginx \\ ##指定工作目录
--user=nginx \\
--group=nginx \\
--with-http_stub_status_module && make && make install &> /dev/null
ENV PATH /usr/local/nginx/sbin:$PATH ##设置环境变量
EXPOSE 80 ##暴露端口
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
CMD nginx ##执行默认命令开启nginx
####CMD和ENTRYPOINT的区别
1、ENRYPOINT指开启容器前镜像就已经执行了括号内的命令
2、CMD是开启容器时,要执行的指令,设置容器启动后默认执行的命令及其参数,但 CMD 能够被 docker run 后面跟的命令行参数替换
3、基于Dockerfile内有CMD或者ENTRYPOINT创建镜像时,docker run 后面就不要加指令(/bin/bash)了,会覆盖掉Dockerfile中的指令或者语法报错
[root@docker nginx]# docker build -f Dockerfile -t nginx:test .
####基于dockerfile文件构建镜像,可以另开一个终端执行“watch -n 1 docker ps -a”可以看到,dockerfile中每条命令都会生成一个镜像,生成一个容器执行,表现了dockerfile的镜像分层原理
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx test c94e53da8234 13 seconds ago 539MB
<none> <none> 6bdbbd0cb566 2 minutes ago 524MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker nginx]# docker run -d -P nginx:test
291c42a12debf73303cee4fd258cf91eb832aec8cfa45f5b1b9d60403cf216dd
[root@docker nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
291c42a12deb nginx:test "/bin/sh -c nginx" About a minute ago Up About a minute 0.0.0.0:49153->80/tcp, :::49153->80/tcp zen_mendel
nginx镜像优化
- 将不需要输出的命令丢入/dev/nulll
- 减少RUN构建
- 多阶段构建(使用FROM命令生成多个镜像,将指定的镜像做为其他镜像的基础镜像环境来构建)
[root@docker ~]# mkdir nginx && cd nginx/
[root@docker nginx]# vim Dockerfile
FROM centos:7 as build
ADD nginx-1.12.2.tar.gz /opt
WORKDIR /opt/nginx-1.12.2
RUN -y update && \\
yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \\
yum clean all && \\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \\
./configure --prefix=/usr/local/nginx &> /dev/null && \\
make &> /dev/null && \\
make install &> /dev/null && \\
rm -rf /opt/nginx-1.12.2
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker nginx]# docker build -f Dockerfile -t nginx:test1 .
[root@docker nginx]# docker images ##查看镜像,大小减少很多
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 0ebc2fe5dbf1 10 seconds ago 210MB
centos 7 8652b9f0cb4c 9 months ago 204MB
制作Tomcat镜像
[root@docker docker]# mkdir tomcat && cd tomcat
[root@docker tomcat]# vim Dockerfile
FROM centos:7
RUN yum -y update
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/locla/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH .:$JAVA_HOME/lib:$JRE_HOME/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out
[root@docker tomcat]# docker build -f Dockerfile -t tomcat:new .
[root@docker tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new 547e30ef3d36 13 seconds ago 1.21GB
nginx test c94e53da8234 About an hour ago 539MB
centos 7 8652b9f0cb4c 9 months ago 204MB
[root@docker tomcat]# docker run -d -P tomcat:new /bin/bash
70cd4660c645b679badb749792fed55494bcbb5952e5dc51f5b39922de2dc822
[root@docker tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70cd4660c645 tomcat:new "/bin/sh -c '/usr/lo…" 3 seconds ago Up 3 seconds 0.0.0.0:49159->8080/tcp, :::49159->8080/tcp clever_swanson
291c42a12deb nginx:test "/bin/sh -c nginx" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp zen_mendel
构建mysql镜像
[root@docker docker]# mkdir mysql && cd mysql
[root@docker mysql]# vim Dockerfile
FROM docker.io/centos:7
RUN yum -y install gcc gcc-c++ make autoconf make cmake wget
RUN groupadd mysql; useradd -r -M -u 3306 -s /sbin/nologin -g mysql mysql
RUN mkdir /usr/local/mysql; mkdir /data/mysql -pv
RUN yum install gcc gcc-c++ ncurses-devel bison bison-devel -y
RUN wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.36.tar.gz
RUN tar xf mysql-5.6.36.tar.gz -C /usr/local/src/
WORKDIR /usr/local/src/mysql-5.6.36
RUN cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-CLIENT-LD-FLAGS=-all-static -DWITH_DEBUG=0 && gmake && gmake install
RUN chown -R root:mysql /usr/local/mysql/ && chown -R mysql:mysql /data/mysql
RUN chmod 755 /usr/local/src/mysql-5.6.36/scripts/mysql_install_db.sh
RUN /usr/local/src/mysql-5.6.36/scripts/mysql_install_db.sh --basedir=/usr/local/mysql --datadir=/data/mysql --no-defaults --user=mysql
RUN cp /usr/local/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf
RUN cp /usr/local/src/mysql-5.6.36/support-files/mysql.server /etc/init.d/mysqld
RUN chmod 775 /etc/init.d/mysqld && /etc/init.d/mysqld start
RUN echo -e '#!/bin/bash\\nexport PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh
RUN source /etc/profile
EXPOSE 3306
[root@docker mysql]# docker build -t centos:mysql-5.6 .
[root@docker mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos mysql-5.6 5fa429158de6 22 minutes ago 5.23GB
[root@docker-lnmp mysql]# docker run -dit --name mysql01 -p 3306:3306 --device-write-bps /dev/sda:10M 5fa429158de6 /bin/bash
21bbe49c32fa10b1ce799247f928609312c81ac6a718a4d790f09525b9e2c66
[root@docker-lnmp mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21bbe49c32fa 5fa429158de6 "/bin/bash" 31 seconds ago Up 29 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql01
以上是关于Docker镜像制作!的主要内容,如果未能解决你的问题,请参考以下文章
docker第四天:制作docker镜像——手工制作docker镜像