DockerFile

DockerFile introduction

Dockerfile is used to build docker image file! Command parameter script!

Construction steps:

1. Create a dockerFile

2. Docker builds into an image

Docker run

4, Docker push release image (DockerHub Ali cloud image warehouse)

FROM scratch ADD centos-7-x86_64-docker.tar.xz/LABEL \ org. LABEL -schema.schema-version="1.0" \ org.label-schema.name="CentOS Base Image" \ org.label-schema.vendor="CentOS" \ org.label-schema.license="GPLv2" \ org.label-schema.build-date="20201113" \ org.opencontainers.image.title="CentOS Base Image" \ Org. Opencontainers. Image. Vendor = "CentOS" \ org. Opencontainers. Image. The licenses = "GPL - 2.0 - only \" org.opencontainers.image.created="2020-11-13 00:00:00+00:00" CMD ["/bin/bash"]Copy the code

The official image package is relatively simple, and sometimes you need to do the mirror yourself.

DockerFile build process

Basic knowledge:

1. Each reserved keyword must be uppercase

2. Execute from top to bottom

3. # indicates a comment

4. Each instruction belongs to the mirror layer

Dockerfiles are relatively simple and become the standard for enterprise delivery.

DockerFile instruction

instruction describe note
FROM Base image centos
MAINTAINER The author ljchengx
RUN The script that runs when the image is compiled
ADD Copy files to the image when compiling the image Similar to tomcat zip package
WORKDIR The working directory of the mirror
VOLUME Example Set the volume to be attached to a container
EXPOSE Set the port to be exposed by the mirror
CMD Sets the start command for the container Only the last one is valid
ENTRYPOINT Sets the start command for the container Can add
ONBUILD Set the ONBUILD directive for the image
COPY Copy files to the image
ENV Set environment variables at build time

Practical test

Most images in FROM Scratch # DockerHub are based on ScratchCopy the code

Create a centos

#Write fileFROM centos MAINTAINER ljchengx<[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 "--end--" CMD /bin/bash
#Build the mirrorDocker build -f myDockerfile -t mycentos:0.1
#A test run
#Start the[root@VM-0-11-centos dockerfile]# docker run -it mycentos:0.1Copy the code

View docker image build history

Actual combat: Creating a Tomcat image

1. Prepare the Image file Tomcat package JDK

#The preparatory work[root@VM-0-11-centos tomcat]# ls apache-tomcat-9.0.43.tar.gz JDK-8U281-linux-x64.tar. gzCopy the code

2, write dockerfile file official name dockerfile default use command directly find do not specify

FROM centos
MAINTAINER ljchengx  # the author
COPY readme.text /usr/local/readme.txt # Copy a file
ADD jdk-8u281-linux-x64.tar.gz /usr/local/  # ADD can do the decompression automaticallyThe ADD apache tomcat - 9.0.43. Tar. Gz/usr /local/ 
RUN yum -y install vim  # install vim
ENV MYPATH /usr/local Define the container path
WORK $MYPATH # specify the path defined above as the working path
ENV JAVA_HOME /usr/local/ jdk1.8.0 _281Set Java environment variables
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
ENV CATALINA_HOME /usr/local/ apache tomcat -- 9.0.43Set the Tomcat environment variables
ENV CATALINA_BASE /usr/local/ apache tomcat - 9.0.43 ENV PATH$PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080 Expose the internal port 8080
CMD /usr/local/apache-tomcat-9.0.43/bin/startup.sh && tail -F /url/local/ apache tomcat - 9.0.43 / bin/logs/catalina. Out# execute command
Copy the code

3. Build an image

[root@VM-0-11-centos tomcat]# docker build -t diytomcat .
Copy the code

Build complete View generated successfully!

4, run,

# docker run -d -p 9090:8080 --name ljchengxtomcat -v /home/tomcat/test:/usr/local/ apache tomcat -- 9.0.43 / webapps /test -v /home/tomcat/logs:/usr/local/ apache tomcat - 9.0.43 / logs diytomcat

#The startup succeeded and the mount succeeded[root@VM-0-11-centos tomcat]# docker run -d -p 9090:8080 --name ljchengxtomcat -v / home/tomcat/test: / usr/local/apache tomcat - 9.0.43 / home/tomcat/webapps/test - v/logs: / usr/local/apache tomcat - 9.0.43 / logs  diytomcat d97affb4d1d08fe19dd72de4b181e4c8340d5534c7aae349c44e6aeac1535576 [root@VM-0-11-centos tomcat]# ls Apache-tomcat-9.0.43.tar. gz Dockerfile JDK-8u281-linux-x64.tar. gz logs readme.txt test
#Write a simple page to find a normal site access
Copy the code

Release the mirror

Release DockerHub

1. Register a DockerHub account

2. Login account

3. Linux also requires login

[root@VM-0-11-centos tomcat]# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username 

Copy the code

4, publish,

# docker push diytomcat

#Modify the label[root@VM-0-11-centos tomcat]# docker tag 613b591c146d ljchengx/tomcat [root@VM-0-11-centos tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest 613b591c146d 53 minutes ago 640MB ljchengx/tomcat latest 613b591C146d 53 minutes ago 640MB mycentos 0.1 6e3f53d5b5d2 25 hours ago 640MB Ljchengx /centos 1.0 3e09855b2bc7 26 hours  ago 209MB nginx latest f6d0b4767a6c 8 weeks ago 133MB centos latest 300e315adb2f 3 months ago 209MB
# [root@VM-0-11-centos tomcat]# docker push ljchengx/tomcat
Copy the code

Publish to Aliyun

1. Create a namespace

2. Create a mirror repository

3. Login push

[root@VM-0-11-centos tomcat]613 # docker tag b591c146d registry.cn-hangzhou.aliyuncs.com/ljchengx/ljchengx:1.0
[root@VM-0-11-centos tomcat]# docker push registry.cn-hangzhou.aliyuncs.com/ljchengx/ljchengx:1.0
Copy the code

Refer to Aliyun: