Dockerfile Mirror Making

Keywords: Operation & Maintenance Docker Nginx

Dockerfile operation instructions

instructionsMeaning
FROM MirrorSpecifies the mirror on which the new image is based. The first instruction must be the FROM instruction, which is required for each image created
MAINTAINER NameMaintainer information describing the mirror
RUN commandExecute commands on the mirror on which they are based and submit them to a new mirror
CMD ["Program to run", ""Parameter 1","Parameter 2"]A command or script to run when an instruction starts a container, dockerfile can only have one CMD command, and only the last one can be executed if multiple commands are specified
EXPOSE port numberSpecify the port to open when a new image is loaded into the docker
ENV Environment Variable ValueSet the value of an environment variable, which the subsequent RUN s will use
ADD source/directory target/directoryCopy the source file to the target file, either in the same directory as the Dockerfile or in a URL
COPY source/directory target/directoryCopy the files/directories on the local host to the destination location. Source files/directories Rain Dockerfile s in the same directory
VOLUME ["Directory"]Create a mount point in a container
USER username/UIDSpecify the user when running the container
WORKDIR PathSpecify a working directory for subsequent RUN/CMD/ENTRYPOINT
ONBUILD commandSpecifies the command to run when the generated image is used as a base image
HEALTHCHECKHealth examination

Making nginx Mirrors

[root@docker2 ~]# mkdir nginx
[root@docker2 ~]# cd nginx
[root@docker2 nginx]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@docker2 nginx]# vim Dockerfile
FROM centos:7                   //Based on the underlying environment
MAINTAINER nginx image                   //Mirror Information
RUN yum -y update && yum -y install gcc gcc-c++ make pcre-devel zlib-devel      //Update Base Environment
RUN useradd -M -s /sbin/nologin nginx           //Create an administrative user
ADD nginx-1.12.2.tar.gz /usr/local/src                 //decompression
WORKDIR /usr/local/src/nginx-1.12.2                   //Enter Directory
RUN ./configure --prefix=/usr/local/nginx \           //assign work directory
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH                //Set variable environment
EXPOSE 80                                //Expose port 80
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf       //Close daemon
CMD nginx                          //Execute default command to open nginx

[root@docker2 nginx]# ls
Dockerfile  nginx-1.12.2.tar.gz

[root@localhost nginx]# Docker build-t nginx:new. //Build Mirror
.......................
Successfully built 8539f950fcb7
Successfully tagged nginx:new

[root@localhost nginx]# docker images //See if the image was made successfully
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        new       8539f950fcb7   7 seconds ago   544MB
centos       7         8652b9f0cb4c   10 months ago   204MB

[root@localhost nginx]# Docker run-d-P nginx:new//Open container
ce3b9cc6ce37a8e0c6e360e7c0002158edad03944383ee3efb962f990f12f3e5
[root@localhost nginx]# Docker PS-A //view port
CONTAINER ID   IMAGE       COMMAND              CREATED         STATUS         PORTS                                     NAMES
ce3b9cc6ce37   nginx:new   "/bin/sh -c nginx"   5 seconds ago   Up 4 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   heuristic_ritchie

Tomcat

[root@localhost tomcat]# ls
apache-tomcat-9.0.16.tar.gz  Dockerfile  jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# vim Dockerfile 
FROM centos:7
RUN yum -y update
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out

[root@localhost tomcat]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
tomcat       new       b8368375f32b   12 seconds ago   1.21GB
<none>       <none>    80a6d110b4f3   3 minutes ago    815MB
nginx        test      b8beee210c3a   9 minutes ago    544MB
nginx        new       8539f950fcb7   20 minutes ago   544MB
centos       7         8652b9f0cb4c   10 months ago    204MB
[root@localhost tomcat]# docker run -d -P tomcat:new
618ce8fc249eabf0568518e7d81b3e8deb7aa9e2117d8753f7134f985fd29df7
[root@localhost tomcat]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS                                         NAMES
618ce8fc249e   tomcat:new     "/bin/sh -c '/usr/lo..."   4 seconds ago    Up 3 seconds               0.0.0.0:49154->8080/tcp, :::49154->8080/tcp   adoring_germain
04d9a4656392   80a6d110b4f3   "/bin/sh -c 'mv jdk1..."   4 minutes ago    Exited (1) 4 minutes ago                                                 naughty_lichterman
ce3b9cc6ce37   nginx:new      "/bin/sh -c nginx"       21 minutes ago   Up 21 minutes              0.0.0.0:49153->80/tcp, :::49153->80/tcp       heuristic_ritchie

summary

Generally, the mirror you make will always be very large, so how can you optimize it?
1. Drop commands that do not require output into/dev/nulll
2. Reduce RUN build
3. Multi-phase build (use the FROM command to generate multiple mirrors, using the specified mirror as the underlying mirror environment for other mirrors)

Posted by bullchina on Sat, 11 Sep 2021 10:35:34 -0700