How to package the project into docker through idea

How to package the project into docker through idea

Many friends have always wanted to know how to run a project in docker. Today, I will show you how to publish your own project to docker (Centos)

Install the virtual machine Download Docker

Directly visit the official website link: Docker official documentation.

1. Open the official homepage and select download and install

front page

2. Choose Docker for Linux

Choose Docker for Linux

3. Select the centos version (choose the appropriate version according to your system)

Choose centos

4. Follow the operation on the official website directly (choose the necessary steps for yourself)

Just follow the steps

1) Delete the previously installed docker (required)

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2) Install the yum tool package (mandatory)

sudo yum install -y yum-utils

3) Add docker repository (required)

 sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

4) Optional parameters, add according to your choice (optional)

Optional parameters

5) Install Docker Engine (Required. If you install other versions, you can skip this step)

sudo yum install docker-ce docker-ce-cli containerd.io

6) View the optional versions of Docker (skip this step if you have completed step 5)

yum list docker-ce --showduplicates | sort -r

7) Install a specific version of Docker as needed (skip this step if you have completed step 5)

 sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

8) Start Docker (required)

sudo systemctl start docker

9) Set Docker to start automatically at boot (optional)

systemctl enable docker

10) Set up Docker image acceleration (optional, can increase the image download speed)

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://sfgi0c9b.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

5. Since Docker has been installed, we will test the status of Docker by manually deploying a web project to the image. If you want to know how to publish a project to Docker, you can skip this step.

Manually deploy web projects to docker

1. Create a web project and package it (war)
Link: Download the project.
2. Download the tomcat image. All images can be searched on docker hub.

Link: docker hub.

The error message when searching on docker hub is tomcat
Choose the appropriate image version

Install tomcat8 Select the tags tab and select the version

Select the appropriate version to download

docker pull tomcat:8.5-jdk8-corretto

Download image

Download image

3. View the downloaded image

docker images 

View Mirror

4. Run an instance of the image to create a folder

mkdir -p /mydata/tomcat/webapps

Upload our web project to the /mydata/tomcat/webapps directory

Install the rz command (if you have a file transfer tool, you can use your own)

yum install lrzsz
cd /mydata/tomcat/webapps

Execute the rz command to upload the file

rz

Transferring Files

Check if the file exists

ls

View File

Start the Tomcat instance

docker run --name tomcat -itd -v /mydata/tomcat/webapps:/usr/local/tomcat/webapps -p 8080:8080 d2b

d2b is the first three letters of the image id. You can write the image name plus the tag. –name gives the instance a name.
-v mounts the directory mydata/tomcat/webapps outside the container and the /usr/local/tomcat/webapps directory inside the container together, so that we can upload projects directly to the outside without entering the container.
-p binds the internal port 8080 to the external port 8080. We can access the internal port 8080 of the container through the external port 8080. These three parameters are the most important parameters. For other parameters, please refer to the docker run parameter table. Learn link: docker run parameters.

View File

We can see that the current project package has been unpacked and the example is running.

docker ps (to view the running instance) 

docker ps -a (view all instances, including those started and unstarted) 

View instance launch

We can see that the instance has been started and we can try the effect

http://192.168.240.3:8080/hello/

5. How to change the port number to 80?

1) Stop and delete the original instance

docker stop tomcat
docker rm <instance id> 

Instance ID

2) Re-run an instance to map port 80

docker run --name tomcat -itd -v /mydata/tomcat/webapps:/usr/local/tomcat/webapps -p 80:80 d2b

3) Enter the container

docker exec -it tomcat /bin/bash   
tomcat is the instance name just created

4) Find /usr/local/tomcat/conf/server.xml and make changes. Entering the container is the same as the external operation.

vi /usr/local/tomcat/conf/server.xml 

Change to port 80

5) Exit the container

exit

6) Restart the container

docker restart tomcat
//Set the container to start automatically when it boots docker update --restart=always <container name>

6. How to package the container and deploy it elsewhere?
We can package the finished container into an image
1) Container packaging into images
docker commit -a -m <container id> <your own container name>

docker commit -a "zhnagdong" -m "test" d98 tomcattest

2) View the packaged image

docker images

View the packaged image

Then export the image in two ways: the first is to directly export the file and move it; the second is to upload it to Docker Hub and then pull it remotely.
3) Run the command directly by exporting the file

docker save -o tomcat.tar 814
//-o represents the id of the image saved as file 814 

View the exported image

Import the container where appropriate

docker load --input tomcat.tar or docker load < tomcat.tar

4) By uploading to the docker repository (you need to have a docker hub account to register first)
First you need to type a tag

docker tag tomcattest:latest dwyerdocker/tomcattest:v1

//tomcattest:latest format is REPOSITORY:TAG
// dwyerdocker/tomcattest:v1 The format is username/name:tag name and tag are created by yourself. Username must be the username on docker hub, otherwise it cannot be pushed to the warehouse.

Register a Docker Hub account

Log in to your docker hub account

docker login

Push the image to the remote repository

docker push dwyerdocker/tomcattest:v1

Just come to our warehouse and pull our image from the appropriate place

insert image description here

The above is the basic use of our docker. The following will explain how to use idea to build a docker image and deploy a project.

Deploy the project to docker through idea

1. First, we modify the docker configuration file to make it accessible remotely. Since our docker does not support our tcp protocol by default, we need to open the docker configuration file to make the tcp protocol effective.

vi /lib/systemd/system/docker.service 

Docker configuration file

Configure the TCP protocol at the end of Execstart -H tcp://0.0.0.0:2375

Configure TCP protocol

Restart Docker

systemctl daemon-reload
systemctl restart docker

Looking at the process information, we found that docker already supports the tcp protocol

ps -ef | grep docker 

Production viewing information

2. Make sure idea has the docker plugin installed

Docker plugin

3. Create a DockerFile file in the root path

Create Dockerfile

FROM java:8
VOLUME /tmp
ADD /target/spring-demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

4. Test whether docker is connected

insert image description here

If the access timeout occurs, check whether the firewall is closed.

systemctl stop firewalld

5. Add configuration

Add Configuration

Configure the exposed port number

insert image description here
insert image description here

6. Package the project

Package Project

7. Right-click to deploy

It is best to restart Docker before deployment. Pay attention to port number conflicts.

insert image description here

8. Check the effect

Final result

This is the end of this article about how to package projects into docker through idea. For more related idea packaging into docker content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • idea combines docker to realize image packaging and one-click deployment
  • A brief analysis of SpringBoot packaging and uploading to docker and implementing multi-instance deployment (IDEA version)
  • How to integrate Docker into IDEA to achieve packaging

<<:  The image element img has extra blank space in IE6

>>:  Summary of basic usage of CSS3 @media

Recommend

Nodejs uses readline to prompt for content input example code

Table of contents Preface 1. bat executes js 2. T...

In-depth analysis of Linux NFS mechanism through cases

Continuing from the previous article, we will cre...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

WeChat Mini Program Lottery Number Generator

This article shares the specific code of the WeCh...

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

A brief discussion on the lazy loading attribute pattern in JavaScript

Table of contents 1. Introduction 2. On-demand at...

Three Ways to Lock and Unlock User Accounts in Linux

If you already have some kind of password policy ...

How to redirect to other pages in html page within two seconds

Copy code The code is as follows: <!DOCTYPE ht...

HTML table markup tutorial (5): light border color attribute BORDERCOLORLIGHT

In a table, you can define the color of the upper...

Example of how to modify styles via CSS variables

question How to modify CSS pseudo-class style wit...

Some properties in CSS are preceded by "*" or "_".

Some properties in CSS are preceded by "*&qu...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...

The difference and use of json.stringify() and json.parse()

1. Differences between JSON.stringify() and JSON....