preface

  • The first blog: Docker introduction to the installation of Docker is the preliminary use of the related Docker commands and how to run some basic images, but running are others to build the image, if you can build some of your own images on the cool. So now we will learn how to build a Dockerfile image and build your own Tomcat image.

Dockerfile instruction summary

Instructions (case insensitive, preferably in uppercase, to better distinguish parameters) The rules describe The sample Matters needing attention
FROM The FROM of mirror Specify base image FROM < image >:[TAG] If the docker hub does not exist, you can configure your own Docker registry.
MAINTAINER MAINTAINER MAINTAINER information The maintainer of the mirror MAINTAINER ‘avengerEug’ This may be discarded in older versions of Docker and replaced with the LABEL directive
COPY COPY …

COPY [“”,…””]
Copy files from the host machine to the image COPY Source file destination file 1. It must be a path in the build context, not a file in the parent directory

2. If the directory is a directory, its internal files or subdirectories are recursively copied, but the directory itself is not copied

3. If multiple directories are specified or wildcard characters are used in, the directory must be one and must end with a slash (/)

4. If it does not exist, it will be created automatically
ADD ADD …

ADD [“”,…””]
Add the host file to the image ADD Source file (folder) Target file (folder) 1. Basically the same as the COPY command

2. Tar files and URL paths are supported

3. If there is a URL, the execution file will be downloaded, but will not be decompressed

4. If the file is a tar file in compressed format on the local system, it expands to a directory. However, tar files obtained through urls are not automatically expanded

5. If multiple resources are specified using wildcards, they must be directories and end with slash (/)
WORKDIR WORKDIR Specifies the working directory under which file operations are performed WORKDIR /usr/local/src If this command is followed by ADD or COPY, the destination files (folders) are prefixed with /usr/local/src

/usr/local/ SRC = /usr/local/ SRC = /usr/local/ SRC
ENV ENV

ENV =…
Specify the environment variable, ENV = or ENV key value ENV JAVA_HOME /usr/local/jdk

ENV PATH
P A T H : PATH:
JAVA_HOME/bin/
Call format:
v a r i a b l e N a m e or VariableName or
{variableName}
RUN RUN

RUN [“”,””,””]
The commands followed by shell scripts are parsed in the format of shell scripts.

Used to specify the command to run during the Docker build process
RUN echo ‘Hello docker’ There is no
CMD CMD

CMD [“< executable command >”,””,””]

CMD [“”,””]
This is followed by shell script commands, which, unlike RUN, are executed after the image is started There is no The CMD command can be used to write multiple commands, but only the last CMD command will take effect, so when we pull someone else to make an image, if someone else’s image uses CMD to start the program,

So when we use docker run command to add shell command, we will overwrite CMD command inside. Eg: Run docker run –name tomcat-it -d -p 8080:8080 tomcat:latest ls to start the command

During tomcat mirroring, the container is not started. When the container is started, run the ls command.

CMD [“”,””] adds parameters to the ENTRYPOINT directive
ENTRYPOINT ENTRYPOINT

ENTRYPOINT[“”,””,””]
Commands that need to be executed when the container starts ENTRYPOINT [“java”, “-jar”, “/root/test.jat”] Similar to CMD, but not overridden by commands in Docker Run. It and the CMD command, whoever comes last, takes effect
VOLUME VOLUME

VOLUME [“”]
To mount a volume, you can mount a directory in the container to a random directory on the host. For example, the mysql data store can use this command to mount persistent data stored in mysql to a random directory on the host.

Ensure that data can be accessed even after the container is restarted
The mountpoint in the VOLUME is a directory in the container, and directories mounted to the host are randomly generated. You can use the Docker inspact container ID command to view the data,

You can find the MOUNTS directory if you select a message whose key is “MOUNTS”
ARG ARG userName = defaultValue Like ENV, it defines environment variables, but it allows us to use the –build-arg = format to fill variables with values when building images docker build -t test:latest –build-arg userName = eugene .

Build the Tomcat image of the custom image instance

2.1 Creating a Tomcat Image folder

  • 	mkdir /root/tomcat-docker
    Copy the code

2.2 Building a DockerFile File

  • The Tomcat image folder is displayed
    	cd /root/tomcat-docker
    Copy the code
  • build
    	Based on centos mirroring
    	FROM centos
    	
    	Add parameters to build image
    	ARG author=default
    	
    	# Add tags, values obtained by parameters in ARG
    	LABEL maintainer=$author
    	
    	Use the ADD command to remotely download the Tomcat package
    	ADDHTTP: / / https://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.100/bin/apache-tomcat-7.0.100.tar.gz /
    	
    	# Since ADD does not extract remote resources, the tomcat package will be named apache-tomcat-7.0.100
    	RUNTar -zxvf apache-tomcat-7.0.100.tar.gz -c /usr/local/
    	
    	# ADD local tar package jdk1.8.0_221
    	ADD jdk-8u221-linux-x64.tar.gz /usr/local/
    	
    	Add environment variables for tomcat and JDK
    	ENV JAVA_HOME=/usr/local/jdk1.8.0_221
    	ENV TOMCAT_HOME=/usr/local/apache-tomcat-7.0.100
    	
    	Add tomcat and Java environment variables to the system environment variables
    	ENV PATH=$PATH:$JAVA_HOME/bin:$TOMCAT_HOME/bin
    	
    	# mount the directory where Tomcat logs are generated to a random directory on the host machine
    	VOLUME "/ usr/local/apache tomcat - 7.0.100 / logs"
    	
    	Expose port 8080
    	EXPOSE 8080
    	
    	# add CMD command -> will be replaced by ENTRYPOINT, and either CMD or ENTRYPOINT will be executed
    	CMD echo $JAVA_HOME
    	
    	# add the container context, when entering the container, will enter this folder by default
    	WORKDIR /usr/local/ apache tomcat -- 7.0.100
    	
    	# start tomcat
    	ENTRYPOINT ["catalina.sh"."run"]
    Copy the code

2.3 Placing the JDk1.8 tar package in the same directory as the Dockerfile

  • As is shown in

2.4 Building the Tomcat Image

  • Run the following script to build the image
    docker build -t mytomcat:v1 --build-arg author=eugene .
    Copy the code

2.5 Starting a Mirror

  • docker run --name mytomcat -it -d -p 8081:8080 mytomcat:v1
    Copy the code

2.6 Viewing the Image Running Log

  • Run the following command
    docker logs mytomcat
    Copy the code
  • The running result is shown below

You can see that the container ran successfully

2.7 Viewing Container Meta Information

  • Run the following command
    docker inspect mytomcat
    Copy the code
  • The running results are as follows:
    [{"Id": "a68f7f0efde04b248e0bdb279f051d2f4dd4520ee5d43b0858ddb5ecc9b14dbb"."Created": "The 2020-03-15 T11:24:35. 374661463 z"."Path": "catalina.sh"."Args": [
                "run"."/bin/sh"."-c"."echo $JAVA_HOME"]."State": {
                "Status": "running"."Running": true."Paused": false."Restarting": false."OOMKilled": false."Dead": false."Pid": 3700."ExitCode": 0."Error": ""."StartedAt": ": the 2020-03-15 T11 24:36. 388929347 z"."FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:b3668180dbfb595f08d6e8457ce1f54f05e3aa128bf97cd707246cfb48b2eec7"."ResolvConfPath": "/var/lib/docker/containers/a68f7f0efde04b248e0bdb279f051d2f4dd4520ee5d43b0858ddb5ecc9b14dbb/resolv.conf"."HostnamePath": "/var/lib/docker/containers/a68f7f0efde04b248e0bdb279f051d2f4dd4520ee5d43b0858ddb5ecc9b14dbb/hostname"."HostsPath": "/var/lib/docker/containers/a68f7f0efde04b248e0bdb279f051d2f4dd4520ee5d43b0858ddb5ecc9b14dbb/hosts"."LogPath": "/var/lib/docker/containers/a68f7f0efde04b248e0bdb279f051d2f4dd4520ee5d43b0858ddb5ecc9b14dbb/a68f7f0efde04b248e0bdb279f0 51d2f4dd4520ee5d43b0858ddb5ecc9b14dbb-json.log"."Name": "/mytomcat"."RestartCount": 0."Driver": "overlay2"."Platform": "linux"."MountLabel": ""."ProcessLabel": ""."AppArmorProfile": ""."ExecIDs": null."HostConfig": {
                "Binds": null."ContainerIDFile": ""."LogConfig": {
                    "Type": "json-file"."Config": {}},"NetworkMode": "default"."PortBindings": {
                    "8080/tcp": [{"HostIp": ""."HostPort": "8081"}},"RestartPolicy": {
                    "Name": "no"."MaximumRetryCount": 0
                },
                "AutoRemove": false."VolumeDriver": ""."VolumesFrom": null."CapAdd": null."CapDrop": null."Capabilities": null."Dns": []."DnsOptions": []."DnsSearch": []."ExtraHosts": null."GroupAdd": null."IpcMode": "private"."Cgroup": ""."Links": null."OomScoreAdj": 0."PidMode": ""."Privileged": false."PublishAllPorts": false."ReadonlyRootfs": false."SecurityOpt": null."UTSMode": ""."UsernsMode": ""."ShmSize": 67108864."Runtime": "runc"."ConsoleSize": [
                    0.0]."Isolation": ""."CpuShares": 0."Memory": 0."NanoCpus": 0."CgroupParent": ""."BlkioWeight": 0."BlkioWeightDevice": []."BlkioDeviceReadBps": null."BlkioDeviceWriteBps": null."BlkioDeviceReadIOps": null."BlkioDeviceWriteIOps": null."CpuPeriod": 0."CpuQuota": 0."CpuRealtimePeriod": 0."CpuRealtimeRuntime": 0."CpusetCpus": ""."CpusetMems": ""."Devices": []."DeviceCgroupRules": null."DeviceRequests": null."KernelMemory": 0."KernelMemoryTCP": 0."MemoryReservation": 0."MemorySwap": 0."MemorySwappiness": null."OomKillDisable": false."PidsLimit": null."Ulimits": null."CpuCount": 0."CpuPercent": 0."IOMaximumIOps": 0."IOMaximumBandwidth": 0."MaskedPaths": [
                    "/proc/asound"."/proc/acpi"."/proc/kcore"."/proc/keys"."/proc/latency_stats"."/proc/timer_list"."/proc/timer_stats"."/proc/sched_debug"."/proc/scsi"."/sys/firmware"]."ReadonlyPaths": [
                    "/proc/bus"."/proc/fs"."/proc/irq"."/proc/sys"."/proc/sysrq-trigger"]},"GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/a0867cbc4ae00a5de11fbfcb5e1d3547a63c284de32556ecaf98bd21c91b5b5c-init/diff:/var/lib/docker/ove rlay2/bc4f600a2ed70eee7b5da2e7ec190dfd5531c8d4e7cd80f8cce0ccc7cd929247/diff:/var/lib/docker/overlay2/9f84cb4e4ef1d2c30ff 0809786d2065577576d2bdfb467131c199e86c2ac00ea/diff:/var/lib/docker/overlay2/811a02af3d6518642276925f82c98deab538e745a1a5 bab64d23088c816945a6/diff:/var/lib/docker/overlay2/9d22f9d8c3d9d31a374834e3f4f5ff3d246e0c5f2148f46d696e8d8d548305fd/diff "."MergedDir": "/var/lib/docker/overlay2/a0867cbc4ae00a5de11fbfcb5e1d3547a63c284de32556ecaf98bd21c91b5b5c/merged"."UpperDir": "/var/lib/docker/overlay2/a0867cbc4ae00a5de11fbfcb5e1d3547a63c284de32556ecaf98bd21c91b5b5c/diff"."WorkDir": "/var/lib/docker/overlay2/a0867cbc4ae00a5de11fbfcb5e1d3547a63c284de32556ecaf98bd21c91b5b5c/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [{"Type": "volume"."Name": "a62dade48437f200874a5ba18b60681b91fc3968d14982d82eeea30e59a8699c"."Source": "/var/lib/docker/volumes/a62dade48437f200874a5ba18b60681b91fc3968d14982d82eeea30e59a8699c/_data"."Destination": "/ usr/local/apache tomcat - 7.0.100 / logs"."Driver": "local"."Mode": ""."RW": true."Propagation": ""}]."Config": {
                "Hostname": "a68f7f0efde0"."Domainname": ""."User": ""."AttachStdin": false."AttachStdout": false."AttachStderr": false."ExposedPorts": {
                    "8080/tcp": {}},"Tty": true."OpenStdin": true."StdinOnce": false."Env": [
                    "PATH = / usr/local/sbin, / usr/local/bin: / usr/sbin, / usr/bin, / sbin, / bin: / usr/local/jdk1.8.0 _221 / bin: / usr/local/apache tomcat - 7.0.100 / bin"."JAVA_HOME = / usr/local/jdk1.8.0 _221"."TOMCAT_HOME = / usr/local/apache tomcat - 7.0.100"]."Cmd": [
                    "/bin/sh"."-c"."echo $JAVA_HOME"]."Image": "mytomcat:v1"."Volumes": {
                    "/ usr/local/apache tomcat - 7.0.100 / logs": {}},"WorkingDir": "/ usr/local/apache tomcat - 7.0.100"."Entrypoint": [
                    "catalina.sh"."run"]."OnBuild": null."Labels": {
                    "maintainer": "eugene"."org.label-schema.build-date": "20200114"."org.label-schema.license": "GPLv2"."org.label-schema.name": "CentOS Base Image"."org.label-schema.schema-version": "1.0"."org.label-schema.vendor": "CentOS"."org.opencontainers.image.created": "The 2020-01-14 00:00:00-08:00"."org.opencontainers.image.licenses": "The GPL - 2.0 - the only"."org.opencontainers.image.title": "CentOS Base Image"."org.opencontainers.image.vendor": "CentOS"}},"NetworkSettings": {
                "Bridge": ""."SandboxID": "e27935f70ffebb9a888d7e5e6f3f965d18792e5856f65ae0c6ddb15341dce805"."HairpinMode": false."LinkLocalIPv6Address": ""."LinkLocalIPv6PrefixLen": 0."Ports": {
                    "8080/tcp": [{"HostIp": "0.0.0.0"."HostPort": "8081"}},"SandboxKey": "/var/run/docker/netns/e27935f70ffe"."SecondaryIPAddresses": null."SecondaryIPv6Addresses": null."EndpointID": "c077b34f7b89df2e6a591009d031c258626890e80ea17c9fdb7328d29981067f"."Gateway": "172.18.0.1"."GlobalIPv6Address": ""."GlobalIPv6PrefixLen": 0."IPAddress": "172.18.0.6"."IPPrefixLen": 16."IPv6Gateway": ""."MacAddress": "02:42:ac:12:00:06"."Networks": {
                    "bridge": {
                        "IPAMConfig": null."Links": null."Aliases": null."NetworkID": "c39de0a86ef7d1e6d1e5f69a5e45fdbcc035aa47aa442f360d96ba807590e086"."EndpointID": "c077b34f7b89df2e6a591009d031c258626890e80ea17c9fdb7328d29981067f"."Gateway": "172.18.0.1"."IPAddress": "172.18.0.6"."IPPrefixLen": 16."IPv6Gateway": ""."GlobalIPv6Address": ""."GlobalIPv6PrefixLen": 0."MacAddress": "02:42:ac:12:00:06"."DriverOpts": null}}}}]Copy the code
  • You can view mounting information under the Mounts node, and the Tomcat file is displayed/ usr/local/apache tomcat - 7.0.100 / logsDirectory mounted to the host/var/lib/docker/volumes/a62dade48437f200874a5ba18b60681b91fc3968d14982d82eeea30e59a8699c/_dataThen we look at the directory in the host and find that the log file does exist, as shown in the figure below:

At this point,VOLUMEThe directive is also in effect

2.8 Visiting the Tomcat home page

  • http://49.235.135.230:8081-> The IP address is my cloud server, which can be changed based on your own server IP address
  • Familiar with tomcat home page

  • At this point, the Tomcat image is successfully built and running

Third, summary

  • Dockerfile is mainly a summary of the use of various instructions in Dockerfile, and added an example to practice
  • I am a slow walker, but I never walk backwards.