1 Prepare the host system
Prepare a CentOS 7 operating system with the following specific requirements:
Must be a 64-bit operating system. The
kernel is above 3.8 to
view your CentOS kernel with the following command:
# uname -r
2 Install Docker
# yum install docker
Use the following command to see if Docker is installed successfully:
# docker version
If the version number of Docker is output, the installation is successful. You can start the Docker service with the following command:
# systemctl start docker.service
Once the Docker service has started, you can start using Docker.
3 download image
Take CentOS as an example, download a CentOS image:
# docker pull centos After
downloading, use the command to view the local mirror list:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest e934aafc2206 2 weeks ago 199MB
4 The host creates the /root/software/ directory and places the installation package in this directory.
5 start the container
The container is run on a mirrored basis. Once the container is started, we can log into the container and install the software or applications we need.
Start the container with the following command:
# docker run -i -t -v /root/software/:/mnt/software/ The e934 /bin/bash
command contains the following three sections:
Docker run <related parameters> <mirror ID> <initial command>
Among them, related parameters include:
-i: indicates
that the container is run in “interactive mode” -t: indicates that the container will enter its command line after startup
-v: indicates which directory needs to be mounted locally to the container, format: -v <host directory>:<container Directory > In
this example, all installers are placed in the /root/software/ directory of the host, and now you need to mount them in the /mnt/software/ directory of the container.
# cd /mnt/software/
# pwd
/mnt/software
# ls
apache-tomcat-7.0.81.tar.gz jdk-8u121-linux-x64.tar.gz The
initial command means that once the container is started, you need to run the command. Use “/bin/bash” to indicate that you directly enter the bash shell after booting.
6 Installing the software
In order to build the Java Web runtime environment, you need to install JDK and Tomcat. The following procedures are performed inside the container. In this example, select the /opt/ directory as the installation directory. You must first enter the directory using the cd /opt/ command.
6.1 Installing the JDK
First, unzip the JDK package:
# tar -zxf /mnt/software/jdk-8u121-linux-x64.tar.gz -C .
Then, move the JDK directory:
# mv jdk1.8.0_121/ /opt/jdk/
6.2 Installing Tomcat
First, extract the Tomcat package:
# tar -zxf /mnt/software/apache-tomcat-7.0.81.tar.gz -C .
Then, move the Tomcat directory:
# mv apache-tomcat-7.0.81/ /opt/tomcat/
6.3 Writing a run script
Write a run script that, when the container is started, runs the script and starts Tomcat.
First, create a run script:
# touch /root/run.sh
# vi /root/run.sh
Then, edit the script as follows:
#!/bin/bash
export JAVA_HOME=/opt/jdk/
export PATH=$JAVA_HOME/bin:$PATH
sh /opt/tomcat/bin/catalina.sh run
Finally, add execute permission for running the script:
# chmod u+x /root/run.sh
7 Exit the container
When the above steps are all completed, you can use the exit command to exit the container.
You can then view the running container using the following command:
Docker ps
At this point, you should not see any running programs, because the container that you just exited with the exit command, the container is stopped, you can use the following command to view all containers:
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4e3a062ab05 e934 “/bin/bash” 38 minutes ago Exited (0) About a minute ago lucid_einstein
Remember the above CONTAINER ID (container ID), which will then be created through the container One can run a Tomcat image.
8 Create a Tomcat image
Use the following command to create a new “mirror” based on a “container ID”:
# Docker the commit d4e3 mytomcat: 1.0
SHA256: c5ef8dacbf3eead5ea2b9fc3c1050a395863c6db0abd0eb7d6dee8ed46a31ffd
# Docker Images
the REPOSITORY the TAG the IMAGE ID CREATED SIZE
mytomcat 1.0 c5ef8dacbf3e 18 is seconds The ago Member 583MB
CentOS Latest e934aafc2206 2 weeks ago Member 199MB
ID of this container is d4e3, image name created is “mytomcat: 1.0 “, then you can use the image to start the Tomcat container.
9 Start the Tomcat container
First, create a new /root/webapps/ROOT directory,
Cd # / root
# mkdir webapps
# cd webapps /
# mkdir ROOT
# cd ROOT /
# vi index.html
and create an index.html file in the directory, file reads as follows:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
As described above, the container can be started with the “mirror name” or “mirror ID”, and the last time it was started. The difference between the containers is that instead of entering the command line of the container, the Tomcat service inside the container is started directly. In this case, you need to use the following command:
# docker run -d -p 58080:8080 -v /root/webapps/:/opt/tomcat/webapps/ –name mytomcat_1 mytomcat:1.0 /root/run.sh
where related parameters include:
-d: Indicates that the /root/run.sh script is executed in “daemon mode”, at which point the Tomcat console does not appear on the output terminal.
-p: indicates the port mapping between the host and the container. At this time, the 8080 port inside the container is mapped to the port 58080 of the host. This exposes the port 58080 to the outside world. The Docker bridge can be used to access the port 8080 inside the container. .
-v: Indicates which local directory needs to be mounted to the container. Format: -v <host directory>: <container directory>
–name: indicates the container name, which can be named with a meaningful name.
In the browser, enter the host IP and port number to access Tomcat:
11 Stop the Tomcat container
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54215923125b mytomcat:1.0 “/root/run.sh” 3 minutes ago Up 3 minutes 0.0.0.0:58080->8080/tcp mytomcat_1
# docker stop 5421
12 Remove the container
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54215923125b mytomcat:1.0 “/root/run.sh” 3 minutes ago Exited (137) 2 seconds ago mytomcat_1
# docker rm 5421
5421
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13 Remove the image
# Docker Images
the REPOSITORY the TAG the IMAGE ID CREATED SIZE
mytomcat 1.0 c5ef8dacbf3e 20 is minutes ago Member 583MB
CentOS Latest e934aafc2206 2 weeks ago Member 199MB
# Docker RMI c5ef
the Untagged: mytomcat: 1.0
the Untagged: mytomcat @ SHA256: d949cbb93a58de27eec4c911f27b9f09edeb3d3ce57cf5ce77d4745211c947f6
Deleted: SHA256: c5ef8dacbf3e7ce916f57c52c16de3892c724996b5e30ca0d141c81897d9a06c
Deleted: SHA256: 7e62d1c2f904e8de3fadc6b01edea96bcb324634f0df514cc9297b1bf11d2f06
# Docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Centos latest e934aafc2206 2 weeks ago 199MB
Recent Comments