DockerFile analysis-notes-full

What is it

DockerFile is a build file used to build a Docker image. It is a script composed of a series of commands and parameters.

Three steps to build:

     Write dockerfile file

     docker build

     docker run

What does the file look like? Take the familiar centos as an example, https://hub.docker.com/_/centos

  

DockerFile build process analysis

DockerFile content basics

  1. Each reserved word instruction must be a capital letter and must be followed by at least one parameter

  2. The instructions are executed in order from top to bottom

  3. # means comment

  4. Each instruction will create a new mirror layer and submit the mirror.

The general process of Docker executing DockerFile

 1.Docker runs a container from the base image

 2. Execute an instruction and modify the container

 3. Perform an operation similar to docker commit to submit a new mirror layer

 4. Docker runs a new container based on the image just submitted

 5. Execute the next instruction in the dockerfile until all instructions are executed

Small summary

From the perspective of application software, Dockerfile, Docker image and Docker container represent three different stages of software.

* Dockerfile is the raw material of the software

* Docker image is a software deliverable

* Docker container can be considered as the running state of the software.

Dockerfile is oriented to development, Docker image has become the delivery standard, and Docker container involves deployment and operation and maintenance. The three are indispensable and work together to serve as the cornerstone of the Docker system.

1 Dockerfile, need to define a Dockerfile, Dockerfile defines everything the process needs. The content of Dockerfile includes executable code or files, environment variables, dependent packages, runtime environment, dynamic link library, operating system release, service process and kernel process (when the application process needs to deal with system service and kernel process, this Need to consider how to design namespace access control) and so on;

DockerFile architecture (reserved word commands)

 from-Basic image, which image is the current new image based on.

 maintainer-the name and email address of the maintainer of the image.

 run-command that needs to be run when the container is built

 expose-the current port exposed by the container

 workdir-specify the working directory that the terminal will log in by default after the container is created, a foothold

 env-used to set environment variables during the image building process

     ENV MY_PATH /usr/mytest

     This environment variable can be used in any subsequent RUN instructions, just like the environment variable prefix is ​​specified in front of the command;

     You can also use these environment variables directly in other commands,

     For example: WORKDIR $MY_PATH

 add-copy the files in the host directory into the mirror and the add command will automatically process the URL and decompress the tar compressed package

 copy-similar to add, copy files and directories to the mirror. Copy the files/directories from the <source path> in the build context directory to the <target path> location in the mirror of the new layer. copy src dest;copy["src","dest"]

 volume-container data volume, used for data storage and persistence

 cmd-specify a command to run when the container starts

   

    There can be multiple cmd commands in dockerfile, but only one takes effect, cmd will be replaced by the parameters after docker run

 entrypoint- specifies the command to be started when a container is specified. The purpose of entrypoint is the same as cmd, which is to specify the container startup program and parameters

 onbuild-Run the command when building an inherited dockerfile, the parent image's onbuild is triggered after being inherited by the child

   

 

Case

base mirror (scratch):

99% of the images in Docker Hub are built with the software required for installation and configuration in the base image.

Custom mirror mycentos

 1. Writing:

What is the Hub default centos mirror

The purpose of customizing mycentos is to make our own mirror as follows:

         Default path after login

         vim editor

         View network configuration ifconfig support

Prepare to write DockerFile file

mycentos content dockerfile

2 Build: docker build -t New image name: TAG

3 Run: docker run -it New image name: TAG

4 List the change history of the mirror

 docker history image name

cmd/entrypoint mirroring case

  Are commands to run when a container starts

  cmd

       There can be multiple cmd commands in dockerfile, but only the last one takes effect, cmd will be replaced by the parameters after docker run

       Explanation and demonstration of case tomcat, docker run -it -p 8888:8080 tomcat ls -l

entrypoint 

  The parameters after docker run will be passed to entrypoint as parameters to form a new command combination.

 case

     

  Make a container with cmd version that can query ip information

     curl command explanation

          

The curl command can be used to perform operations such as downloading, sending various HTTP requests, and specifying HTTP headers.

If the system does not have curl, you can use yum install curl to install or download and install.

curl is to output the downloaded file to stdout 

Use the command: curl http://www.baidu.com

After execution, the html of www.baidu.com will be displayed on the screen

This is the easiest way to use it. Use this command to get the page pointed to by http://curl.haxx.se. Similarly, if the URL here points to a file or a picture, it can be directly downloaded to the local. If the download is an HTML document, then only the file header will be displayed by default, that is, the header of the HTML document. To display all, please add parameter -i

Question: If we want to display HTTP header information, we need to add the -i parameter

why?

We can see that the executable file not found error, executable file not found.

As we said before, after the image name is command, the default value of CMD will be replaced at runtime .

Therefore, the -i here replaces the original CMD instead of adding it after the original curl -s http://ip.cn. And -i is not a command at all, so it is naturally not found.

Then if we want to add the -i parameter, we must re-enter this command completely:

$ docker run myip curl -s http://ip.cn -i

Create entrypoint version to query ip information container

FROM centos

RUN yum install -y curl

ENTRYPOINT [ "curl", "-s", "http://ip.cn" ]

Custom mirror tomcat9

1、mkdir -p /zzyyuse/mydocerfile/tomcat9

2. Touch c.txt in the above directory

3. Copy the compressed package installed by jdk and tomcat into the previous directory-apache-tomcat-9.0.8.tar.gz; jdk-8u171-linux-x64.tar.gz

4. Create a new dockerfile file in the /zzyyuse/mydockerfile/tomcat9 directory

5. Build

Build completed

6、run

docker run -d -p 9080:8080 --name myt9 -v /zzyyuse/mydockerfile/tomcat9/test:/usr/local/apache-tomcat-9.0.8/webapps/test -v /zzyyuse/mydockerfile/tomcat9/tomcat9logs/:/usr/local/apache-tomcat-9.0.8/logs --privileged=true zzyytomcat9

Remarks

Docker mount host directory Docker access cannot open directory.: Permission denied

Solution: add one more --privileged=true parameter after mounting the directory

7. Verification

8. Publish the tested web server test based on the aforementioned container volume

  General overview:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns="http://java.sun.com/xml/ns/javaee"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  id="WebApp_ID" version="2.5">

  

  <display-name>test</display-name>

 

</web-app>

a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

  </head>

  <body>

    -----------welcome------------

    <%="i am in docker tomcat self "%>

    <br>

    <br>

    <% System.out.println("=============docker tomcat self");%>

  </body>

</html>

test

Small summary

 

Guess you like

Origin blog.csdn.net/pshdhx/article/details/109253725