This is the 7th day of my participation in the August More Text Challenge
In the previous article we have a preliminary understanding of dockerfile, know that dockerfile is used to build a docker image file, that is, command parameter script, now we try to learn more about dockerfile
Dockerfile:
-
Write a dockerFile file
-
Docker builds into an image
-
Docker run runs the image
-
Docker Push image release (DokcerHub, Ali Cloud image warehouse)
What did the authorities do
1. Search for a random image on DockerHub
2. Selecting a version will directly jump to Github, and you can see the command script of the image
Many of the official mirrors are very pure packs, and we can make our own mirrors according to our own needs
The construction of Dockerfile
Dockerfile construction is not difficult, mainly we want to write the command script
Basic knowledge of
1. Each keyword (directive) must be in uppercase
2. Execution is performed from top to bottom
3. # indicates a comment
4. Each directive creates and commits a new mirror layer
Dockerfile is development oriented, we will later release the project, do the image, need to write dockerfile this file (Dockerfile gradually become the standard of enterprise delivery)
Let’s review a couple of concepts
- Dockerfile: build file that defines all steps, source code
- DockerImages: A product built from a Dockerfile that generates images and is eventually published and run
- Docker container: The container is the server on which the image is run
DockerFile instructions
FROM # base mirror, MAINTAINER # who writes the RUN # Image build is required to RUN the command ADD # step WORKDIR # Image working directory VOLUME # Mount directory EXPOSE # Reserved port configuration CMD # ENTRYPOINT # specifies the command to be run when the container is started. You can append the command COPY # like ADD to COPY files to the image. ENV # specifies the environment variable set when the container is builtCopy the code
Practical test
You can see FROM the above centos command script that starts FROM Scratch, 99% of the images in DockerHub are FROM the base image of FROM Scratch, then configure the required software and configuration to build.
Some basic commands are not available in the official centos, so try creating your own centos
$ docker run -it centos
[root@8fbe85e4a93b /]# vim
bash: vim: command not found
[root@8fbe85e4a93b /]# ifconfig
bash: ifconfig: command not found
[root@8fbe85e4a93b /]#
Copy the code
Step 1: Write the document
Writes a configuration file for dockerfile in the specified path
FROM centos 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/bashCopy the code
Step 2: Build the image
Mydockerfile is the file name
Docker build -f myDockerfile -t mycentos:0.1Copy the code
You can see that it builds step by step according to the instructions we wrote
View the generated image
Step 3: Test run
Docker run - it mycentos: 0.1Copy the code
After running into the container, you can see that you are entering the configured working directory, and the ifconfig command is also available, and it is OK to try using vim command, so the image written by ourselves through dockerfile can be used normally
We can list the change history of the local image to see how it was made. So we can look at how mirror images are made, right
docker history
Copy the code
Actual Tomcat Image
Preparing an image File
Prepare the image file Tomcat package and JDK package
Write dockerfile
Dockerfile file, official name dockerfile, build time can automatically find this file, do not need to specify -f!
FROM centos MAINTAINER ajajaj COPY readme.txt /usr/local/readme.txt ADD jdk-8u301-linux-x64.tar /usr/local/ ADD Apache-tomcat-8.5.70.tar. gz /usr/local/run yum -y install vim ENV MYPATH /usr/localworkdir $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_301 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.70 ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.70 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD / usr/local/apache tomcat - 8.5.70 / bin/startup. Sh && tail -f/usr/local/apache tomcat - 8.5.70 / bin/logs/catalina. OutCopy the code
Build the mirror
The Dockerfile is ready to be built
docker build -t diytomcat .
Copy the code
At this point, the Customized Tomcat image is created successfully
Boot image
We then start the image and do something along the way, as you can see in the following commands
- Port mapping to 9090
- Mount the project directory and log directory
docker run -d -p 9090:8080 --name ajtomcat -v / Users/cb/test/tomcat - out/test: / usr/local/apache tomcat - 8.5.70 / webapps/test - v / Users/cb/test/tomcat - out/tomcatlogs: / usr/local/apache tomcat - 8.5.70 / logs diytomcatCopy the code
Into the container
$ docker exec -it 9f3f7ed2b184 /bin/bash
[root@9f3f7ed2b184 local]#
Copy the code
Access to the test
Tomcat (curl localhost:8080
Host machine to access the 9090 look also OK oh
Publish the project
Since volume mount is done, we can write projects locally between us
Mount relationships of the commands above
/ Users/cb/test/tomcat – out/test: / usr/local/apache tomcat – 8.5.70 / webapps/test
We create the simplest Web project under the external path
- Create a web-INF folder and create a web. XML file in the folder
- Create an index.jsp file
Write random things in the file according to the format
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
</web-app>
Copy the code
index.jsp
<html> <head><title>Hello World</title></head> <body> Hello World! <br/> <% out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
Copy the code
So when we’re done, let’s go to the Web project
Finally, let’s take a look at the output of the log. It can be seen from the figure below that the log is also printed stably
OK, we’re done here
Now we can complete the real-time release of the project locally, and the project is deployed in Docker. The most important thing is that the Docker image allows us to create it purely by hand. Oh, this set of completed processes can be settled.
Docker learned here is a more comprehensive understanding of Docker, and will continue to further study the Docker network, image publishing and other things, I hope this article is a little help to you!
Sometimes our eyes can see the universe, but do not see the most miserable world at the bottom of society.