Docker--DockerFile creates its own image

Keywords: Programming MySQL Docker github shell

Echo edit, welcome to reprint, reprint please declare article source.Welcome to add echo WeChat (Micro Signal: t2421499075) to exchange learning.Never lose a hundred battles, never claim to be a constant winner, never fail, and always strive to move forward.- That's really powerful!!!

As we use many of the images in the docker library, we can look at how they are generated to help us encapsulate our own images.Changing the article is mainly used to introduce dockerfile to generate its own image, introducing basic operations and an example, skilled enough to skip

Key dockerfile s generated by image

Dockerfile is a configuration file in text format that allows users to quickly create custom images using Dockerfile.Generally speaking, Dockerfile is divided into four parts: basic mirror information, maintainer information, mirror operation instructions, and execution instructions at container startup.We can log in to github to find the docker's official repository: https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile Find the dockerfile corresponding to mysql to analyze.(The notes are all for easy interpretation and are not available on the original address of the website)

# FROM specifies the base image, such as 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 s execute commands inside the mirror, such as installing software, configuring the environment, etc. Line breaks can use ""
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: Set the value of the variable, ENV MYSQL_MAJOR 5.7, which can be modified through docker run --e key=value, followed by ${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 <mysql-build@oss.oracle.com>" 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: The specified data is suspended in the directory
VOLUME /var/lib/mysql

# COPY: Copy the host's files into a mirror. If the directory does not exist, it will automatically create the required directory. Note that only copying, not extracting and decompressing
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: Specifies the port to be exposed by the mirror, which can be mapped to the host using -p when the mirror is started
EXPOSE 3306 33060
CMD ["mysqld"]

Basic Instruction Cleanup

instructions Explain
FROM Specifies the base image of the created image
MAINTAINER Specify Maintainer Information
RUN Run Command
CMD Specify the command to execute by default when starting the container
LABEL Specify metadata label information to generate the mirror
EXPOSE Declare the port on which the service listens in the mirror
ENV Specify environment variables
ADD Assign the contents of the specified <src>path to the <dest>path in the container, <src>can be a URL; if it is a tar file, it is automatically extracted to the <dest>path
COPY Assign content from the <scr>path of the local host to the <dest>path in the container; COPY is generally recommended instead of ADD
ENTRYPOINT Specify the default entry for the mirror
VOLUME Create a data mount point
USER Specify user name or UID when running container
WORKDIR Configure working directory
ARG Specify the parameters used within the mirror (e.g. version number information, etc.)
ONBUILD Commands to create operations when configuring the currently created image as the base image for other mirrors
STOPSIGNAL Signal for container exit
HEALTHCHECK How to perform a health check-up
SHELL Specify the default SHELL type when using SHELL

Dockerfile Actual Warfare Spring Boot Project

Here is an item with a link: https://git.coding.net/xlsorry/docker-demo.git

After downloading, validation can be started in the idea and access has a return value:

After completing these basic validations, we packaged the project into a jar

mvn clean package
  • Once we've done that, we'll pass the jar package into the linux where docker is already installed.
  • Create a new directory "first-dockerfile" in the docker installed environment
  • Go to the first-dockerfile and put our typed jar packages in that directory
  • Create a Dockerfile and write the following
FROM openjdk:8
MAINTAINER 2421499075@qq.com
LABEL name="dockerfile-demo" version="1.0" author="2421499075@qq.com"
COPY springboot-0.0.1-SNAPSHOT.jar dockerfile-image.jar
CMD ["java","-jar","dockerfile-image.jar"]

Once completed, the following figure is shown:

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

  • After that, we can execute docker images to see if the package is successful.

We can see that there is an additional test-docker-image in the images, which we used when we executed the command.This image was packaged by creating a dockerfile.

  • Create a container based on an image and run the docker we just packaged
docker run -d --name springboot01 -p 10080:8080 test-docker-image
  • After successful startup, we can check in the browser to see if it runs successfully.The following interface proves our success.

Be a bottom-line Blogger

Posted by cap97 on Mon, 16 Dec 2019 19:06:50 -0800