After learning the Dockerfile command, you need to write a relatively complete Dockerfile to verify the learning content.
preface
First of all, we need to make it clear what to do, build an image through the Dockerfile file, and ensure that the built-in tomcat is self started when starting the image generation container.
Then let's start~
1, Preparatory work
In order to show the whole process more clearly, relevant information will be cleared away.
1. Delete all images in the environment
View mirror
docker images
delete mirror
# Delete the specified image ID can be separated by a space, and multiple images can be deleted at a time docker rmi -f image ID # If the image is deleted through the image name, you need to bring a tag docker rmi -f Image name:tag
Finally, verify whether the container is deleted. If there is a running container, pause the container first
2. Delete all containers
Viewing running containers
docker ps
Stop container in operation
docker stop container ID
Delete container
# Delete specified container docker rm -f container ID container ID ... # Delete all containers docker rm -f $(docker ps -qa)
3. Download tomcat compressed package
Create a clean directory / opt / soft / docker and upload the tomcat compressed package.
Download tomcat compressed package. If not, baidu is recommended.
Unzip it, mainly to view the unzipped name
tar -zxvf apache-tomcat-8.5.65.tar.gz
4. Download the jdk compressed package under linux environment
In the newly created directory / opt / soft / docker, upload the jdk package and download it from Baidu.
As a reminder, the compressed package downloaded by wget is not authorized by default and cannot be used normally.
Decompression:
tar -zxvf jdk-8u281-linux-x64.tar.gz
Get the fake name of the extracted file: jdk1 8.0_ two hundred and eighty-one
2, Dockerfile
1. Write Dockerfile file
The code is as follows:
# Download Image centos8 FROM centos:8 # Label author information MAINTAINER wang 1194966094@qq.com # Upload the compressed package to the specified directory in the container, and the ADD command will decompress it automatically ADD apache-tomcat-8.5.65.tar.gz /usr/local/tomcat/ ADD jdk-8u281-linux-x64.tar.gz /usr/local/java # Configure environment variables. Note that the folder name after jdk decompression is used here ENV JAVA_HOME /usr/local/java/jdk1.8.0_281 ENV PATH $JAVA_HOME/bin:$PATH # Directory mount VOLUME /usr/local/tomcat/apache-tomcat-8.5.65/conf /usr/local/tomcat/apache-tomcat-8.5.65/logs /usr/local/tomcat/apache-tomcat-8 .5.65/bin # Exposed port 8080 is the default port of tomcat EXPOSE 8080 # Why use Catalina What about sh? Because docker must have at least one front-end service, otherwise docker will have no service by default and will be closed automatically CMD /usr/local/tomcat/apache-tomcat-8.5.65/bin/catalina.sh run
2. Build image
Save Dockerfile file
Execute the build mirror command:
docker build -t centos-tomcat:1.0 .
At this time, we will see the image composition layer by layer. We know that the image of docker is divided according to levels, so when writing Dockerfile, we should also note that the command line should not be divided into too many levels.
Give me a diagram:
In fact, from the above figure, we can also see the concept of docker layering. I don't know if it is similar to dolls. We can study it when we have time.
3. Start the image
Let's first look at the generated image
Start mirroring
# -P refers to the mapping between the random port of the host and port 8080 in the container docker run -d -P --name tomcat01 centos-tomcat:1.0
Viewing container processes
If you want to view the details of the container
docker inspect container ID
There is a lot of information, so I won't show it first. I can try it myself.
Well, let's focus on mount and network settings.
Mounts:
"Mounts": [ { "Type": "volume", "Name": "d320041ccbe4a3c5f04a06230eebaff9e071c52703eea8b64d51927e533c23d6", "Source": "/var/lib/docker/volumes/d320041ccbe4a3c5f04a06230eebaff9e071c52703eea8b64d51927e533c23d6/_data", "Destination": "/usr/local/tomcat/apache-tomcat-8.5.65/bin", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "27765bfca23ac6f6285a2905544053e1e3e847779afc9d982003b073a26acd93", "Source": "/var/lib/docker/volumes/27765bfca23ac6f6285a2905544053e1e3e847779afc9d982003b073a26acd93/_data", "Destination": "/usr/local/tomcat/apache-tomcat-8.5.65/conf", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "54fcca8a0a62bbb0154cc4fcdf8cbfe486660fb75f32aeb7426e087f0347e429", "Source": "/var/lib/docker/volumes/54fcca8a0a62bbb0154cc4fcdf8cbfe486660fb75f32aeb7426e087f0347e429/_data", "Destination": "/usr/local/tomcat/apache-tomcat-8.5.65/logs", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],
Source refers to the host directory
Destination refers to the directory within the container
You can enter the Source directory to see whether the mount is successful. At the same time, you can also modify the configuration to see whether the file in the container is modified, and then restart to see whether it takes effect. Don't try one by one.
NetworkSettings:
"NetworkSettings": { "Bridge": "", "SandboxID": "f3da6df9e5acd806f308f43189f752c8e104963dd92cba23b6a31c403977520b", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "8080/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "49154" }, { "HostIp": "::", "HostPort": "49154" } ] }, "SandboxKey": "/var/run/docker/netns/f3da6df9e5ac", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "95d370f3059c9a26d5c244e8decab9a430da816678eeced88afad6d3acd4b9a6", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "29566123a9aec320125306dad6add56b6a0c502d2256df7a98a3880a19539f5e", "EndpointID": "95d370f3059c9a26d5c244e8decab9a430da816678eeced88afad6d3acd4b9a6", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } }
above:
Ports node, you can see the port mapping information between the container and the host
The network node can view the network configuration. The bridge represents the bridge, and the Gateway is the Gateway address. Generally, the default is the IP address assigned by docker to the host, that is, the address of docker0. IPAddress is the address assigned after the container establishes a connection with the host through the bridge. If we want to access the services inside the container, we can use IPAddress+Ports. However, because we have done port mapping, at this stage, we can directly access the host address to verify whether the service in the container is self started successfully.
# Test 49154 on the host indicates networksettings Ports. HostPort curl http://localhost:49154
You can also enter http: / / host address: port in the browser address bar to see the tomcat home page.
summary
- A completed Dockerfile is created successfully.
- The tomcat bootstrap inside the container was created successfully.
- The access test is successful by mapping the external port to the port in the container.
- The creation of Dockerfile still needs a lot of practice and is written according to your own business needs.
- Come on, come on, work hard. (it feels like MLM ~)