Docker - DockerFile create your own image

echo edited, welcome to reprint, please declare the source of the article. Welcome to add echo micro letter (Micro Signal: t2421499075) the exchange of learning. Battle undefeated, according to not claiming victorious, defeat after defeat is not decadent, according to energy struggling to move forward. - This is the real rated power! ! !


When we use the library docker many image, we can look into his generation process, this will help our own package own image. Correct the introduction is mainly used to generate its own dockerfile Image, describes the basic operation example and a skilled can skip

image generated key dockerfile

Dockerfile is a text configuration file format, you can use Dockerfile quickly create a custom image. Generally, Dockerfile divided into four parts: the base image information, perform a maintainer information, instructions and image containers start command. We can find the docker's official log on github repository: https: //github.com/docker-library/mysql/blob/master/5.7/Dockerfile find mysql corresponding dockerfile to analyze. (Which comments are all for the convenience of interpretation followed by the go, and none of these annotations on the official website of the original address)

# FROM指定基础镜像,比如 FROM ubuntu:14.04
FROM debian:stretch-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
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

# ENV :设置变量的值,ENV MYSQL_MAJOR 5.7,可以通过docker run --e key=value修改,后面可以直接使 用${MYSQL_MAJOR}
ENV GOSU_VERSION 1.7
RUN set -x \
    && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \
    && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \
    && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \
    && export GNUPGHOME="$(mktemp -d)" \
    && gpg --batch --keyserver ha.pool.sks-keyservers.net --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 \
    && chmod +x /usr/local/bin/gosu \
    && gosu nobody true \
    && apt-get purge -y --auto-remove ca-certificates wget

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 \
    && rm -rf /var/lib/apt/lists/*

RUN set -ex; \
# gpg: key 5072E1F5: public key "MySQL Release Engineering <[email protected]>" imported
    key='A4A9406876FCBD3C456770C88C718D3B5072E1F5'; \
    export GNUPGHOME="$(mktemp -d)"; \
    gpg --batch --keyserver ha.pool.sks-keyservers.net --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.28-1debian9

RUN echo "deb http://repo.mysql.com/apt/debian/ stretch mysql-${MYSQL_MAJOR}" > /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}" && 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 777 /var/run/mysqld \
# 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

# VOLUME : 指定数据的挂在目录
VOLUME /var/lib/mysql

# COPY : 将主机的文件复制到镜像内,如果目录不存在,会自动创建所需要的目录,注意只是复制,不会提取和解压
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]

# EXPOSE : 指定镜像要暴露的端口,启动镜像时,可以使用-p将该端口映射给宿主机
EXPOSE 3306 33060
CMD ["mysqld"]

The basic finishing instructions

instruction Explanation
FROM Specifies the base image to create a mirror image
MAINTAINER Designated maintainer information
RUN Run command
CMD Default command to execute when starting a container
LABEL Specified metadata tag information generation mirroring
EXPOSE Mirroring statement within the service is listening on port
ENV Specify environment variables
ADD Assignment specified Path to the contents of the container Under Path, It can be a URL; if the tar file, it will automatically unzip to Under Path
COPY Assignment of the local host Path to the contents of the container Under path; under normal circumstances is recommended rather than ADD COPY
ENTRYPOINT Specifies default entry mirroring
VOLUME Create a data mount point
USER User name or UID run when the specified container
WORKDIR Configuring working directory
ARG Specified parameters (e.g., version number, etc.) used in the image
ONBUILD Create a command operation when configuring the current mirror created as the basis for a mirror image of the other, performed
stoplight Container exit signals
HEALTHCHECK How to carry out health checks
SHELL Specifies the default type when using SHELL SHELL

Dockerfile combat Spring Boot project

Here are a project link: https: //git.coding.net/xlsorry/docker-demo.git

After downloading verification can start idea, and have access to the return value:
Here Insert Picture Description

After completing these basic verification, we will project packaged into jar

mvn clean package
  • After completion of these operations, we will have passed the jar package linux docker is installed.
  • Create a new directory "first-dockerfile" docker in the installation environment
  • Into the first-dockerfile, we will lay the jar package into the directory
  • Creating Dockerfile file, write the following contents
FROM openjdk:8
MAINTAINER [email protected]
LABEL name="dockerfile-demo" version="1.0" author="[email protected]"
COPY springboot-0.0.1-SNAPSHOT.jar dockerfile-image.jar
CMD ["java","-jar","dockerfile-image.jar"]

After completion of the following figure:
Here Insert Picture Description

  • Execute in that directory:docker build -t test-docker-image .

Here Insert Picture Description

  • After completion we can perform docker imagesto see if the package was successful.

Here Insert Picture Description
We can see the images in more than a test-docker-image, the name is when we execute a command, use the name. The image created by our dockerfile packed out.

  • Based on image creation container, we just packed docker run
docker run -d --name springboot01 -p 10080:8080 test-docker-image
  • After a successful start, we can access it in a browser to see if run successfully. The following interface will prove the success of our operations.
    Here Insert Picture Description

To be a bottom line of the main blog

Guess you like

Origin www.cnblogs.com/xlecho/p/12052832.html