What is the

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

  • Building three steps
  1. Write a Dockerfile file
  2. docker build
  3. docker run



  • Dockerhub is too slow to search for container images. Instead, use Github to find dockerfiles.

    Github.com/docker-libr…, you can directly search for the image name



    Some images are not available directly on Github search will be available

DockerFile build process parsing

Dockerfile content basics

  • 1: Each reserved word instruction must be uppercase and must be followed by at least one argument
  • 2: Commands are executed from top to bottom
  • 3: # indicates a comment
  • 4: Each instruction creates a new image layer and commits the image

Docker executes the general process of Dockerfile

(1) Docker runs a container from the base image (2) executes an instruction and makes changes to the container (3) commits a new image layer like Docker Commit (4) Docker then runs a new container based on the image just committed (5) Execute the next instruction in dockerfile until all instructions have been executed

A small summary

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

  • Dockerfile is the raw material of the software
  • Docker images are deliverables of software
  • The Docker container can be considered as the running state of the software.

Dockerfile is development oriented, Docker image becomes the delivery standard, and Docker container involves deployment and operation and maintenance. The three are indispensable, and together act as the cornerstone of Docker system.

  1. Dockerfile, you need to define a Dockerfile, and a Dockerfile defines everything that the process needs. Dockerfile involves executing code or files, environment variables, dependency packages, runtime environments, dynamic link libraries, operating system distributions, service processes and kernel processes (when the application process needs to deal with system services and kernel processes, then need to consider how to design namespace permission control), etc.
  2. Docker image, after using Dockerfile to define a file, Docker build will produce a Docker image, when running Docker image, will really start to provide services;
  3. Docker container, which provides services directly.

DockerFile Architecture (Reserved word instructions)

  • FROM: Base mirror, which mirror the current new mirror is based on
  • MAINTAINER: Specifies the name and email address of the image MAINTAINER
  • RUN: the command to RUN when the container is built
  • EXPOSE: Indicates the port that is exposed to the current container
  • WORKDIR: specifies the working directory that the terminal logs in to by default after the container is created
  • ENV: Used to set the environment variable ENV MY_PATH /usr/mytest during image building
  1. This environment variable can be used in any subsequent RUN directive, just as the environment variable prefix is specified before the command; You can also use these environment variables directly in other instructions,

Example: WORKDIR $MY_PATH

  • ADD: Copies files in the host directory to the image and the ADD command automatically processes the URL and decompresses the tar package
  • COPY: Similar to ADD, copies files and directories to an image. Copy files/directories from the < source path > directory in the build context directory to the < destination path > location within the image in a new layer
  • VOLUME: container data VOLUME used for data storage and persistence
  • CMD:
  1. Specifies the command to run when a container is started

  2. There can be multiple CMD directives in a Dockerfile, but only the last one takes effect and CMD is replaced by the argument after the Docker run

ENTRYPOINT: 3. Specify the command to run when a container is started. 4

  • ONBUILD: Run the command when building an inherited Dockerfile. The ONBUILD of the parent image is triggered when the quilt inherits the parent image



A small summary

case

Mirror Base (scratch)

99% of the images in Docker Hub are built by installing and configuring the required software in the base image

  • docker rm -f $(docker ps -q), query the ID of the current running container and delete it, similar to batch delete.

Custom mirror mycentos

  1. Copy the JDK and Tomcat installation packages to the previous directory
  • Hub default CentOS mirroring





    The original centos (pull centos from Aliyun) does not support the above two commands
Customize mycentos purpose to make our own image with the following: default path after login vim editor view network configuration ifconfig supportCopy the code
  • Prepare to write the DockerFile file



    I’m going to create a new Dockerfile2 file in my myDocker folder in my root directory

  • MyCentOS content DockerFile



    ENV is used to set environment variables. WORKDIR is the default path for login. Echo is used to print the contents of the successful build.
FROM centos
MAINTAINER xdr630<[email protected]>
 
ENV MYPATH /usr/local
WORKDIR $MYPATH
 
RUN yum -y install vim
RUN yum -y install net-tools
 
EXPOSE 80
 
CMD echo $MYPATH
CMD echo "success--------------ok"
CMD /bin/bash
Copy the code
  1. build
Docker build-t new image name :TAGCopy the code

You’ll see a. “at the end of the Docker build command to indicate the current directory

Docker build -f /mydocker/Dockerfile2 -t mycentos:1.3Copy the code



Docker Images:



  1. run
Docker runit new image name :TAGCopy the code

As you can see, our own new image already supports the vim/ifconfig command, and the extension is successful.



The default login path has been changed, vim and ifconfig commands are now available

  1. Lists the change history of the mirror
docker historyThe mirror ofCopy the code

Docker images image name, so that you can also blur query different versions of Mycentos



CMD/ENTRYPOINT Image example

  • Both specify commands to run when a container is started
  • CMD
  1. There can be multiple CMD directives in a Dockerfile, but only the last one takes effect and CMD is replaced by the argument after the Docker run
  2. Case

    Demonstration of Tomcat

docker run -it -p 8888:8080 tomcat ls -l
Copy the code

Ls -l /usr/local/tomcat will override the first line of CMD



Tomcat is also not started at this time:

  • ENTRYPOINT, which will not be overridden by subsequent commands, will be added
  1. Arguments after docker run are passed to ENTRYPOINT as arguments, and new command combinations are formed
  2. Case

  • Create a CMD version of a container for querying IP information
  • Crul command description:

Using the curl command, you can download, send various HTTP requests, and specify HTTP headers. You can install curl with yum install curl if you don’t have curl. Curl is to print the downloaded file to stdout

Use the following command: curlwww.baidu.com

Once executed, the HTML for www.baidu.com appears on the screen

FROM centos
RUN yum install -y curl
CMD [ "curl"."-s"."http://ip.cn" ]
Copy the code





This is the easiest way to use it. Use this command to get the page that http://curl.haxx.se points to, and again, if the URL here points to a file or an image it can be downloaded locally. If you download an HTML document, by default only the header of the file, the HEADER of the HTML document, will be displayed. To display them all, add the -i parameter

  • Problem: If we want to display HTTP headers, we need to add the -i parameter

  • WHY

Executable file not found As we said earlier, the image name is followed by command, which replaces the default value of CMD at runtime. So the -i replaces CMD instead of adding it to curl -s ip.cn. And -i is not a command at all, so of course you can’t find it.

So if we want to add the -i parameter, we have to re-type the command completely:

$ docker run myip curl -s http://ip.cn -i
Copy the code
  • Make ENTROYPOINT version of the container to query IP information
FROM centos
RUN yum install -y curl
ENTRYPOINT [ "curl"."-s"."http://ip.cn" ]
Copy the code



Custom mirror Tomcat9

  1. mkdir -p /zzyyuse/mydockerfile/tomcat9

  2. Touch C.txt in the above directory

  3. Copy the JDK and Tomcat installation packages to the previous directory

    1. Apache tomcat – 9.0.8. Tar. Gz

    2. jdk-8u171-linux-x64.tar.gz

  4. In/zzyyuse/mydockerfile/tomcat9 new Dockerfile file directory

FROM         centos
MAINTAINER    zzyy<[email protected]>
Copy the host's current context to /usr/local/
COPY c.txt /usr/local/cincontainer.txt
Add Java and Tomcat to the container
ADD jdk-8u171-linux-x64.tar.gz /usr/local/ ADD apache tomcat - 9.0.8. Tar. Gz/usr /local/
Install the Vim editor
RUN yum -y install vim
# set the path of WORKDIR for working access
ENV MYPATH /usr/local
WORKDIR $MYPATH
Configure Java and Tomcat environment variables
ENV JAVA_HOME /usr/local/ jdk1.8.0 _171 ENV CLASSPATH$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/ apache tomcat - 9.0.8 ENV CATALINA_BASE/usr /local/ apache tomcat - 9.0.8 ENV PATH$PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
The port on which the container is listening at runtime
EXPOSE  8080
Run Tomcat on startup
# ENTRYPOINT ["/usr/local/apache tomcat - 9.0.8 / bin/startup. Sh "]
# CMD ["/usr/local/apache tomcat - 9.0.8 / bin/catalina. Sh ", "run"]
CMD /usr/local/apache-tomcat-9.0.8/bin/startup.sh && tail -F /usr/local/ apache tomcat - 9.0.8 / bin/logs/catalina. OutCopy the code
  • Directory content

  1. build



    Build a complete

  2. 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
Copy the code



note

Cannot open directory.: Permission denied

How to do this: Add a — privileged=true parameter after the mount directory

  1. validation

  2. Publish the test Web service test with the container volume described above
  • 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>
Copy the code
  • 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>Copy the code
  • test





A small summary