Docker——Image的原理(四)(1)

Docker是如何实现的?


1、去GitHub上查看docker-library


Docker——Image的原理(四)(1)


2、点击进入


Docker——Image的原理(四)(1)


3.进入MySQL中发现其中有一个DockerFile文件


Docker——Image的原理(四)(1)


由此可见Image是由DockerFile文件生成的。


从DockerFile生成Image镜像


1,进入DockerHub中,找到MySQL,进入5.7版本中的DockerFile镜像


Docker——Image的原理(四)(1)


2,在CentOS中创建mysql-image文件夹,然后进入文件夹,创建Dockerfile文件


Docker——Image的原理(四)(1)


3,将官网的文件复制到DockerFile


Docker——Image的原理(四)(1)


4, 从DockerFile生成Image文件


docker build -t my-mysql-image .


Docker——Image的原理(四)(1)


MySQL中Dockerfile的命令解析


#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
基础镜像信息是FROM
FROM debian:buster-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
 镜像操作指令 RUN 构建镜像时执行的命令
RUN groupadd -r mysql && useradd -r -g mysql mysql

 镜像操作指令 RUN 构建镜像时执行的命令
RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*

# add gosu for easy step-down from root
# https://github.com/tianon/gosu/releases
设置环境变量 ENV
ENV GOSU_VERSION 1.12
RUN set -eux; \
    savedAptMark="$(apt-mark showmanual)"; \
    apt-get update; \
    apt-get install -y --no-install-recommends ca-certificates wget; \
    rm -rf /var/lib/apt/lists/*; \
    dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
    wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
    wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
    export GNUPGHOME="$(mktemp -d)"; \
    gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
    gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
    gpgconf --kill all; \
    rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
    apt-mark auto '.*' > /dev/null; \
    [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    chmod +x /usr/local/bin/gosu; \
    gosu --version; \
    gosu nobody true

RUN mkdir /docker-entrypoint-initdb.d

RUN apt-get update && apt-get install -y --no-install-recommends \
# for MYSQL_RANDOM_ROOT_PASSWORD
        pwgen \
# for mysql_ssl_rsa_setup
        openssl \
# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
# File::Basename
# File::Copy
# Sys::Hostname
# Data::Dumper
        perl \
# install "xz-utils" for .sql.xz docker-entrypoint-initdb.d files
        xz-utils \
    && rm -rf /var/lib/apt/lists/*

RUN set -ex; \
# gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
    key='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \
    export GNUPGHOME="$(mktemp -d)"; \
    gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key"; \
    gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/mysql.gpg; \
    gpgconf --kill all; \
    rm -rf "$GNUPGHOME"; \
    apt-key list > /dev/null

ENV MYSQL_MAJOR 5.7
ENV MYSQL_VERSION 5.7.34-1debian10

RUN echo 'deb http://repo.mysql.com/apt/debian/ buster mysql-5.7' > /etc/apt/sources.list.d/mysql.list

# the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
# also, we set debconf keys to make APT a little quieter
RUN { \
        echo mysql-community-server mysql-community-server/data-dir select ''; \
        echo mysql-community-server mysql-community-server/root-pass password ''; \
        echo mysql-community-server mysql-community-server/re-root-pass password ''; \
        echo mysql-community-server mysql-community-server/remove-test-db select false; \
    } | debconf-set-selections \
    && apt-get update \
    && apt-get install -y \
        mysql-server="${MYSQL_VERSION}" \
# comment out a few problematic configuration values
    && find /etc/mysql/ -name '*.cnf' -print0 \
        | xargs -0 grep -lZE '^(bind-address|log)' \
        | xargs -rt -0 sed -Ei 's/^(bind-address|log)/#&/' \
# don't reverse lookup hostnames, they are usually another container
    && echo '[mysqld]\nskip-host-cache\nskip-name-resolve' > /etc/mysql/conf.d/docker.cnf \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \
    && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \
# ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
    && chmod 1777 /var/run/mysqld /var/lib/mysql

/*注释 VOLUME:添加卷,用于指定持久化目录 */
VOLUME /var/lib/mysql

/* ADD:将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问网络资源,类似wget */
/* COPY:功能类似ADD,但是是不会自动解压文件,也不能访问网络资源,即将物理资源拷贝到容器中 */
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
/*注释:ENTRYPOINT:配置容器,使其可执行化。配合CMD可省去"application",只使用参数。*/
ENTRYPOINT ["docker-entrypoint.sh"]

/*注释,EXPOSE:指定暴露镜像的端口供主机做映射*/
EXPOSE 3306 33060
/* CMD:构建容器后调用,也就是在容器启动时才进行调用,存在多个CMD时只有最后一个生效,也支持exec语法。 */
CMD ["mysqld"]


自定义Image镜像,将Spring boot项目打包成Image镜像


  1. 创建一个Spring Boot项目


  1. 写一个controller


@RestController
public class DockerController {
@GetMapping("/dockerfile")
@ResponseBody
String dockerfile() {
return "hello docker" ;
 }
}


mvn clean package打成一个jar包


在target下找到"dockerfile-demo-0.0.1-SNAPSHOT.jar"


在docker环境中新建一个目录"first-dockerfile"


上传"dockerfile-demo-0.0.1-SNAPSHOT.jar"到该目录下,并且在此目录创建Dockerfile


创建Dockerfile文件,编写内容


FROM openjdk:8
MAINTAINER itcrazy2016
LABEL name="HelloDocker-Ysy" version="1.0" author="yueshaoyang"
COPY dockerfile-demo-0.0.1-SNAPSHOT.jar dockerfile-image.jar
CMD ["java","-jar","dockerfile-image.jar"]


Docker——Image的原理(四)(1)


上一篇:Docker——Docker常用命令实践 (三)(1)


下一篇:Docker——Docker常用命令实践 (三)(2)