Docker 詳細(xì)部署不香嗎?
鏈接:toutiao.com/i6843391272229536267
目錄
docker介紹 安裝docker Ubuntu安裝docker CentOS安裝docker 通過(guò)腳本安裝 拉取java環(huán)境 創(chuàng)建springboot項(xiàng)目 打包springboot到docker docker查看容器的日志 查看log4j2輸出問(wèn)文件日志
Ubuntu安裝docker
CentOS安裝docker
yum update

yum install?-y yum-utils device-mapper-persistent-data?lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum update

yum install?-y docker-ce

systemctl start?dockersystemctl?restart dockersystemctl stop?dockersystemctl enable?dockersystemctl?status docker通過(guò)腳本安裝
curl -fsSL https://get.docker.com/ | shwget -qO- https://get.docker.com/ | shdocker pull java:8docker?images



<properties>
???????<docker.image.prefix>registry.aliyuncs.com/linhuatestdocker.image.prefix>
????properties>
?
????????????<plugin>
????????????????<groupId>com.spotifygroupId>
????????????????<artifactId>docker-maven-pluginartifactId>
????????????????<version>1.0.0version>
????????????????<configuration>
????????????????????<imageName>${docker.image.prefix}/${project.artifactId}imageName>
????????????????????
????????????????????<dockerDirectory>src/main/dockerdockerDirectory>
????????????????????<resources>
????????????????????????<resource>
????????????????????????????<targetPath>/targetPath>
????????????????????????????<directory>${project.build.directory}directory>
????????????????????????????<include>${project.build.finalName}.jarinclude>
????????????????????????resource>
????????????????????resources>
????????????????configuration>
????????????plugin>

FROM java:8
VOLUME /tmp/tomcat
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar springboot-docker.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker.jar"]FROM:指定存在的鏡像,java:8是我剛剛拉取的鏡像,運(yùn)行的基礎(chǔ)。VOLUME:指向的一個(gè)臨時(shí)文件,用于存儲(chǔ)tomcat工作。ADD:復(fù)制文件并且重命名文件。ENTRYPOINT:初始化配置或者自定義配置。

package?com.ymy.controller;
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RequestMethod;
import?org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
????@RequestMapping(value?= "/test",method = RequestMethod.GET)
????public String test(){
????????System.out.println("這是控制臺(tái)日志!");
????????log.info("這是輸出到文件的日志");
????????return???"HELLO-BUG!!!!!!!!!!";
????}
}
server:
??port: 9999. ____ _ __ _ _
?/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
?\\/ ___)| |_)| | | | | || (_| | ) ) ) )
??' |____| .__|_| |_|_| |_\__, | / / / /
?=========|_|==============|___/=/_/_/_/
?:: Spring Boot :: (v2.2.5.RELEASE)
15:29:19.386 [main] INFO com.ymy.SpringBootDockerApplication - Starting SpringBootDockerApplication on LAPTOP-3GLHJRE9 with PID 20652 (D:\springboot\spring-boot-docker\target\classes started by admin in D:\springboot)
15:29:19.395 [main] INFO com.ymy.SpringBootDockerApplication - No active profile set, falling back to?default?profiles: default
15:29:20.183?[main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized?with?port(s): 9999?(http)
15:29:20.200?[main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9999"]
15:29:20.201?[main] INFO org.apache.catalina.core.StandardService - Starting?service [Tomcat]
15:29:20.201?[main] INFO org.apache.catalina.core.StandardEngine - Starting?Servlet engine: [Apache Tomcat/9.0.31]
15:29:20.309?[main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
15:29:20.309?[main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in?881?ms
15:29:20.452?[main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
15:29:20.568?[main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting?ProtocolHandler ["http-nio-9999"]
15:29:20.596?[main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on?port(s): 9999?(http) with?context?path?''
15:29:20.599?[main] INFO com.ymy.SpringBootDockerApplication - Started SpringBootDockerApplication in?1.664?seconds (JVM running for?4.04)![]()

mvn clean package?docker:build
docker?images
docker run --name springbooot-docker -p 9999:9999 -d 4a2
run:運(yùn)行的意思–name:指定鏡像啟動(dòng)的之后的名稱-p:容器和外部的端口映射 第一個(gè)端口:外部 第二個(gè)端口:內(nèi)部-d:后臺(tái)運(yùn)行 -t:實(shí)時(shí)運(yùn)行,窗口關(guān)閉,程序結(jié)束。4a2:表示鏡像的id(IMAGE ID)前3位,這里的id并不需要輸入全稱,只需要輸入前幾個(gè)就行,有一個(gè)前提:當(dāng)有很多鏡像的時(shí)候,前面幾個(gè)字符就有可能會(huì)相同,這個(gè)時(shí)候就需要多輸入幾位,直到不相同位置。
docker ps

docker ps
docker logs -f



tail?-100f info.log
docker exec?-it ca2cd59fff9b /bin/bashca2cd59fff9b:容器id
cd?work/spring-boot-docker
tail?-100f info.log
exit評(píng)論
圖片
表情
