Docker builds java web service

Install Docker

The Docker software can be installed simply by the following command:

>> rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
>> yum -y install docker-io

You can use the following command to check whether Docker is installed successfully:

docker version

If the version number of Docker is output, it means that the installation is successful, and we can start using Docker below.

The Docker service can be started with the following command:

service docker start

download mirror

docker Chinese website

  • Download the centOS image
    The warehouse pulled here is centos7.9.2009 version
docker pull centos:7.9.2009

insert image description here

  • View all local mirrors
docker images

insert image description here

Start the container

docker run -i -t -v /www/server/box/docker/repository/boo/:/mnt/software/ eeb6ee3f44bd /bin/bash
  • docker run <related parameters> <mirror ID> <initial command>
    The initial command indicates the command that needs to be run once the container is started. At this time, "/bin/bash" is used to indicate that nothing should be done, just enter the command line.
parameter explain
-i Indicates to run the container in "interactive mode"
-t Indicates that the container will enter its command line after it starts
-v Indicates which local directory needs to be mounted into the container, format: -v <host directory>:<container directory>

insert image description here
At this point the container has started and entered the command line of the container

Install related software

以下操作均在容器内进行

install tomcat

You can put the tomcat file in the */www/server/box/docker/repository/boo/ directory of the host :
insert image description here
correspondingly,
there will be this file in the /mnt/software/tomcat/* directory in the container (mounting relationship ):

insert image description here
Then unzip the tomcat file... no more details

install jdk


Download the decompressed version of version 8 from the official website here : https://www.oracle.com/java/technologies/downloads/

Configure environment variables after downloading and decompressing

Configure environment variables
  • Modify the system configuration file:
vi /etc/profile

Add the following to the end of the profile file:

# jdk环境变量路径
export JAVA_HOME=/data/read_business_world/jdk/jdk1.8.0_151
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}
  • After saving the configuration successfully, make the file take effect:
source /etc/profile
  • Check jdk version
    insert image description here

Write a running script

Write a running script. When the container is started, run the script and start Tomcat. The specific process is as follows:

  • First, create the run script:
vi /root/run.sh

The script content is as follows:

sh /home/server/tomcat/bin/catalina.sh run

Note: Use Tomcat's run script to start the Tomcat service.

  • Finally, add execute permissions for running scripts:
chmod u+x /root/run.sh

exit container

Exit the container with exitthe command
insert image description here

  • View running programs
docker ps

Because the exit command has been used to exit the container just now, the container is in a stopped state at this time, so you cannot see

  • view all containers
docker ps -a

insert image description here
Remember the above CONTAINER ID (container ID), and then we will use this container to create a mirror that can run Java Web.

Create a javaWeb image

docker commit 05fcf4ef548b boo/javaweb:0.1

The format is docker commit boo/javaweb:0.1
insert image description here
After the creation is complete, use the docker images command to view all images in the current system
insert image description here

Start the Java web container

Same as the startup container above

  • View all mirrors:
[root@localhost docker]# docker images
REPOSITORY       TAG    IMAGE ID       CREATED             SIZE
boo/javaweb      0.1    7fd8bf88f779   About an hour ago   594MB
  • Start the container
    This time we do not enter the command line of the container, but directly start the Tomcat service inside the container (run the /root/run.sh script):
docker run -d -p 58080:8080 --name booWeb boo/javaweb:0.1 /root/run.sh
parameter explain
-d Indicates that the /root/run.sh script is executed in "guardian mode", and the Tomcat console will not appear on the output terminal at this time
-p Indicates the port mapping between the host and the container. At this time, port 8080 inside the container is mapped to port 58080 of the host. This exposes port 58080 to the outside world, and the port 8080 inside the container can be accessed through the Docker bridge.
–name Indicates the container name, just customize a meaningful name

Operation error
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/root/run.sh": permission denied: unknown.
insert image description here
The current running user of the host machine is inconsistent with the running user in the docker container, resulting in an access permission problem.
Solution:
add --privileged=true

docker run --privileged=true -d -p 58080:8080 --name booWeb boo/javaweb:0.1 /root/run.sh
  • View running containers
docker ps

run in privileged mode

docker run --name boo-web --privileged=true -d boo/javaweb:0.1 /usr/sbin/init

insert image description here

into the container:
docker exec -it boo-web /bin/bash

insert image description here

Common commands

Operating images (images)

docker images : view local mirror
docker rmi : delete mirror

operating container

docket ps : view running containers
docket ps -a : view all containers

docker run xxx : create container
docker start <NAMES / CONTAINER ID> : run container
docker stop <NAMES / CONTAINER ID> : stop container
docker rm <NAMES / CONTAINER ID> : delete container

docker inspect <NAMES / CONTAINER ID> | grep Mounts -A 50 : View container file mounts

In-vessel operation

exit : Exit and close the container
ctrl + P + Q : Exit the container The container continues to run

Guess you like

Origin blog.csdn.net/HELLOMRP/article/details/128640232