Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Docker learning by Dockerfile

Dockerfile is used to build a docker image file, command parameter script

Build steps

  • Write adockerfilefile
  • docker buildBuild as a mirror image
  • docker runRun the mirror
  • docker pushRelease image (DockerHub, Ali Cloud image warehouse)

Basic knowledge of

  • Each reserved keyword (directive) must be in uppercase

  • Commands are executed from top to bottom

  • # comment

  • Each instruction creates a commit to a new mirror layer and commits

Dockerfile is development-oriented, and we will need to write a Dockerfile file if we want to publish deployment projects later

Docker image has gradually become a standard and mainstream technology for enterprise delivery

steps

Development > Deployment > O&M

  • Dockerfile: the build file that defines all the steps, source code

  • DockerImages: The images generated by building a Dockerfile, and the final product released and running

  • Docker containers: Containers are images that run to provide services

Dockerfile instruction

FROM         # base image, everything starts from here (specify base image, who is the father of this image?) Such as: centos
MAINTAINER   # Tell someone who will take care of him? (Specify the maintainer's name + email address)
RUN          # what do you want him to do?
ADD          # Give him some start-up capital (COPY file, will automatically decompress), for example, I want to add a Tomcat
WORKDIR      # mirror working target (set current working directory)
VOLUME       # Give him a place to put his luggage (set up volume, mount host directory)
EXPOSE       # what door does he want to open?
RUN          # Let's Go (specify what to do when the container starts)

# extension
CMD          # specifies the command to run when the container is started. Only the last command is valid and can be replaced
ENTRYPOINT   # is similar to CMD, but it can append commands

ONBUILD      The ONBUILD command is executed when building an inherited Dockerfile
COPY         Copy our files to the image
ENV          Set environment variables at build time
Copy the code

Real test: Build your own Centos

Write a Dockerfile file
FROM centos                                
MAINTAINER xujiantong<[email protected]>  

ENV MYPATH /usr/local
WORKDIR $MYPATH

# add command
RUN yum -y install vim 
RUN YUM -y install net-tools

Expose the port number
EXPOSE 80

# output information
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash

Copy the code
Build the image from the file above
# command docker build -f 
      
        -t image name :[tag]
      Docker build -f Dockerfile -t mycentos:1.0# Building 2.9s (8/8) FINISHED 

# test runDocker run-it mycentos:1.0 xujiantong@xujiantongdeMac-mini docker % docker run-it mycentos:1.0 [root@2cd65476cc5clocal]# pwd
/usr/local
[root@2cd65476cc5c local]# ifconfigeth0: Flags = 4163 < UP, BROADCAST, RUNNING, MULTICAST > mtu 1500 inet 172.17.0.5 netmask 255.255.0.0 BROADCAST 172.17.255.255 Mr 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)# Run successfully
Copy the code

View the build process/history

docker history <IMAGE ID>

IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
400239e8be02   4 minutes ago   CMD ["/bin/sh" "-c" "/bin/bash"]                0B        buildkit.dockerfile.v0
<missing>      4 minutes ago   CMD ["/bin/sh" "-c" "echo \"----end----\""]     0B        buildkit.dockerfile.v0
<missing>      4 minutes ago   CMD ["/bin/sh" "-c" "echo $MYPATH"]             0B        buildkit.dockerfile.v0
<missing>      4 minutes ago   EXPOSE map[80/tcp:{}]                           0B        buildkit.dockerfile.v0
<missing>4 minutes ago RUN /bin/sh -c yum -y install net-tools # bu... 14.7 MB buildkit. Dockerfile. Where v0<missing>      4 minutes ago   RUN /bin/sh -c yum -y install vim # buildkit    71.8MB    buildkit.dockerfile.v0
<missing>      6 minutes ago   WORKDIR /usr/local                              0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   ENV MYPATH=/usr/local                           0B        buildkit.dockerfile.v0
<missing>      6 minutes ago   MAINTAINER xujiantong<[email protected]>     0B        buildkit.dockerfile.v0
<missing>      7 days ago      /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>7 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc... 0B<missing>7 days ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0... 231MBCopy the code