❤️Dockerfile部署springboot+vue项目

小明的学习圈子2024-03-25环境

一、docker中部署mysql

二、docker中部署redis

三、docker中部署nginx

环境部署地址参照:Linux环境下的部署(基于Docker容器)open in new window

四、CentOS7中使用Dockerfile部署后台jar包

1、修改application.yml 文件中:文件上传地址 profile、redis信息 2、修改application-druid.yml文件中的 数据库信息 3、修改logback.xml文件种的日志存放路径 4、打包后在 stucoding-admintarget下生成了一个 jar 包

1、application.yml

修改的主要部分

 profile: /mydata/uploadPath
 host:  redis

完整

# 项目相关配置
stucoding:
  # 名称
  name: stucoding
  # 版本
  version: 3.8.7
  # 版权年份
  copyrightYear: 2024
  # 文件路径 示例( Windows配置D:/stucoding/uploadPath,Linux配置 /home/stucoding/uploadPath)
  profile: /mydata/uploadPath
  # 获取ip地址开关
  addressEnabled: false
  # 验证码类型 math 数字计算 char 字符验证
  captchaType: math
 
  # redis 配置
  redis:
    # 地址
    host:  redis
    # 端口,默认为6379
    port: 6379
    # 数据库索引
    database: 0
    # 密码
    password:
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms

2、application-druid.yml

注意修改用户和密码,以及访问url改为db

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        druid:
            # 主库数据源
            master:
                url: jdbc:mysql://db:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                username: root
                password: stucoding

3、logback.xml

修改为服务器的地址,增加编码格式

  <property name="log.path" value="/mydata/stuCoding/logs" />
  <pattern>${log.pattern}</pattern>
  <charset>UTF-8</charset>

完整文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 日志存放路径 -->
	<property name="log.path" value="/mydata/stuCoding/logs" />
    <!-- 日志输出格式 -->
	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

	<!-- 控制台输出 -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${log.pattern}</pattern>
            <charset>UTF-8</charset>
		</encoder>
	</appender>
	
	<!-- 系统日志输出 -->
	<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${log.path}/sys-info.log</file>
        <!-- 循环政策:基于时间创建日志文件 -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志文件名格式 -->
			<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
			<!-- 日志最大的历史 60天 -->
			<maxHistory>60</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>${log.pattern}</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 过滤的级别 -->
            <level>INFO</level>
            <!-- 匹配时的操作:接收(记录) -->
            <onMatch>ACCEPT</onMatch>
            <!-- 不匹配时的操作:拒绝(不记录) -->
            <onMismatch>DENY</onMismatch>
        </filter>
	</appender>
	
	<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${log.path}/sys-error.log</file>
        <!-- 循环政策:基于时间创建日志文件 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志文件名格式 -->
            <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
			<!-- 日志最大的历史 60天 -->
			<maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!-- 过滤的级别 -->
            <level>ERROR</level>
			<!-- 匹配时的操作:接收(记录) -->
            <onMatch>ACCEPT</onMatch>
			<!-- 不匹配时的操作:拒绝(不记录) -->
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
	
	<!-- 用户访问日志输出  -->
    <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/sys-user.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 按天回滚 daily -->
            <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- 日志最大的历史 60天 -->
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${log.pattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
	
	<!-- 系统模块日志级别控制  -->
	<logger name="com.stucoding" level="info" />
	<!-- Spring日志级别控制  -->
	<logger name="org.springframework" level="warn" />

	<root level="info">
		<appender-ref ref="console" />
	</root>
	
	<!--系统操作日志-->
    <root level="info">
        <appender-ref ref="file_info" />
        <appender-ref ref="file_error" />
    </root>
	
	<!--系统用户操作日志-->
    <logger name="sys-user" level="info">
        <appender-ref ref="sys-user"/>
    </logger>
</configuration> 

4、pom文件

这里没用到docker-maven-plugin插件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
    <groupId>com.stucoding</groupId>
    <artifactId>stucoding</artifactId>
    <version>3.8.7</version>

    <name>stucoding</name>
    <url>https://www.stucoding.com/</url>
    <description>StuCodingVue</description>
    
    <properties>
        <docker.host>http://192.168.1.71:2375</docker.host>
        <docker.maven.plugin.version>0.40.2</docker.maven.plugin.version>

        <stucoding.version>3.8.7</stucoding.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <druid.version>1.2.20</druid.version>
        <bitwalker.version>1.21</bitwalker.version>
        <swagger.version>3.0.0</swagger.version>
        <kaptcha.version>2.3.3</kaptcha.version>
        <pagehelper.boot.version>1.4.7</pagehelper.boot.version>
        <fastjson.version>2.0.43</fastjson.version>
        <oshi.version>6.4.11</oshi.version>
        <commons.io.version>2.13.0</commons.io.version>
        <commons.collections.version>3.2.2</commons.collections.version>
        <poi.version>4.1.2</poi.version>
        <velocity.version>2.3</velocity.version>
        <jwt.version>0.9.1</jwt.version>
        <mybatis-plus.version>3.5.2</mybatis-plus.version>
    </properties>

    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringBoot的依赖配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.15</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 阿里数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!-- 解析客户端操作系统、浏览器等 -->
            <dependency>
                <groupId>eu.bitwalker</groupId>
                <artifactId>UserAgentUtils</artifactId>
                <version>${bitwalker.version}</version>
            </dependency>

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

            <!-- 获取系统信息 -->
            <dependency>
                <groupId>com.github.oshi</groupId>
                <artifactId>oshi-core</artifactId>
                <version>${oshi.version}</version>
            </dependency>

            <!-- Swagger3依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!-- io常用工具类 -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>

            <!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- velocity代码生成使用模板 -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>

            <!-- collections工具类 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons.collections.version}</version>
            </dependency>

            <!-- 阿里JSON解析器 -->
            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- Token生成与解析-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <!-- 验证码 -->
            <dependency>
                <groupId>pro.fessional</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- 定时任务-->
            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-quartz</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <!-- 代码生成-->
            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-generator</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <!-- 核心模块-->
            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-framework</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <!-- 系统模块-->
            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-system</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <!-- 通用工具-->
            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-common</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <dependency>
                <groupId>com.stucoding</groupId>
                <artifactId>stucoding-business</artifactId>
                <version>${stucoding.version}</version>
            </dependency>

            <!-- mybatis-plus start-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-extension</artifactId>
                <version>${mybatis-plus.version}</version>
                <scope>compile</scope>
            </dependency>
            <!-- mybatis-plus end-->
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>stucoding-admin</module>
        <module>stucoding-framework</module>
        <module>stucoding-system</module>
        <module>stucoding-quartz</module>
        <module>stucoding-generator</module>
        <module>stucoding-common</module>
        <module>stucoding-business</module>
    </modules>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

<!--            <plugin>-->
<!--                <groupId>io.fabric8</groupId>-->
<!--                <artifactId>docker-maven-plugin</artifactId>-->
<!--                <version>${docker.maven.plugin.version}</version>-->
<!--&lt;!&ndash;                <executions>&ndash;&gt;-->
<!--&lt;!&ndash;                    &lt;!&ndash;如果想在项目打包时构建镜像添加&ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;                    <execution>&ndash;&gt;-->
<!--&lt;!&ndash;                        <id>build-image</id>&ndash;&gt;-->
<!--&lt;!&ndash;                        <phase>package</phase>&ndash;&gt;-->
<!--&lt;!&ndash;                        <goals>&ndash;&gt;-->
<!--&lt;!&ndash;                            <goal>build</goal>&ndash;&gt;-->
<!--&lt;!&ndash;                        </goals>&ndash;&gt;-->
<!--&lt;!&ndash;                    </execution>&ndash;&gt;-->
<!--&lt;!&ndash;                </executions>&ndash;&gt;-->
<!--                <configuration>-->
<!--                    &lt;!&ndash; Docker 远程管理地址&ndash;&gt;-->
<!--                    <dockerHost>${docker.host}</dockerHost>-->
<!--                    <images>-->
<!--                        <image>-->
<!--                            &lt;!&ndash;定义镜像名称&ndash;&gt;-->
<!--                            <name>stucodingvue/${project.name}:${project.version}</name>-->
<!--                            &lt;!&ndash;定义镜像构建行为&ndash;&gt;-->
<!--                            <build>-->
<!--                                &lt;!&ndash;定义基础镜像&ndash;&gt;-->
<!--                                <from>openjdk:8</from>-->
<!--                                <args>-->
<!--                                    <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>-->
<!--                                </args>-->
<!--                                &lt;!&ndash;定义哪些文件拷贝到容器中&ndash;&gt;-->
<!--                                <assembly>-->
<!--                                    &lt;!&ndash;定义拷贝到容器的目录&ndash;&gt;-->
<!--                                    <targetDir>/</targetDir>-->
<!--                                    &lt;!&ndash;只拷贝生成的jar包&ndash;&gt;-->
<!--                                    <descriptorRef>artifact</descriptorRef>-->
<!--                                </assembly>-->
<!--                                &lt;!&ndash;定义容器启动命令&ndash;&gt;-->
<!--                                <entryPoint>["java", "-jar","-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>-->
<!--                                &lt;!&ndash;定义维护者&ndash;&gt;-->
<!--                                <maintainer>stucoding</maintainer>-->
<!--                            </build>-->
<!--                        </image>-->
<!--                    </images>-->
<!--                </configuration>-->
<!--            </plugin>-->



        </plugins>
    </build>

    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

5、将此服务打成一个jar包

设置linux下的编码格式LF

image-20240419193716322

6、将jar包上传到服务器的/mydata/stucoding/路径下

image-20240419194155488

7、新建Dockerfile文件

vi Dockerfile

内容

# 该镜像需要依赖的基础镜像
FROM openjdk:8
# 将当前目录下的jar包复制到docker容器的/目录下
ADD stucoding-admin.jar /stucoding-admin.jar
# 声明服务运行在8080端口
EXPOSE 8080
# 指定docker容器启动时运行jar包
ENTRYPOINT ["java", "-jar","/stucoding-admin.jar"]
# 指定维护者的名字
MAINTAINER stucoding

8、在Linux上构建docker镜像

  • 如果之前有创建过stucoding的容器和镜像,需要先进行删除;
# 删除容器
docker rm stucoding
# 删除镜像
docker rmi stucoding
  • 在Dockerfile所在目录执行以下命令:
  • 注意这里的镜像名 不能有大写,后面还有一个点
docker build -t stucoding .
# -t 表示指定镜像仓库名称/镜像名称:镜像标签 .表示使用当前目录下的Dockerfile
docker build -t stucoding/stucoding:1.0-SNAPSHOT .

9、步骤

mysql
[root@linux-local stucoding]# docker start redis
redis
[root@linux-local stucoding]# docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED       STATUS             PORTS                                                                                                      NAMES
4cf6c6f02a9b   nacos/nacos-server:2.0.2   "bin/docker-startup.…"   7 days ago    Up About an hour   0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848-9849->9848-9849/tcp, :::9848-9849->9848-9849/tcp   nacos
3a6c18413fa4   redis:7                    "docker-entrypoint.s…"   2 weeks ago   Up 5 seconds       0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                                                  redis
7b180e4e04c0   mysql:5.7                  "docker-entrypoint.s…"   2 weeks ago   Up 25 seconds      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                                       mysql
[root@linux-local stucoding]# vi Dockerfile
[root@linux-local stucoding]# ll
total 86000
-rw-r--r--. 1 root root      350 Apr 17 20:07 Dockerfile
-rw-r--r--. 1 root root 88056673 Apr 17 19:57 stucoding-admin.jar
[root@linux-local stucoding]# docker build -t stucoding .
[+] Building 8.3s (7/7) FINISHED                                                                                           docker:default
 => [internal] load build definition from Dockerfile                                                                                 0.0s
 => => transferring dockerfile: 446B                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/openjdk:8                                                                         0.0s
 => [internal] load .dockerignore                                                                                                    0.0s
 => => transferring context: 2B                                                                                                      0.0s
 => [internal] load build context                                                                                                    0.6s
 => => transferring context: 88.07MB                                                                                                 0.6s
 => [1/2] FROM docker.io/library/openjdk:8                                                                                           0.2s
 => [2/2] ADD stucoding-admin.jar /stucoding-admin.jar                                                                               7.1s
 => exporting to image                                                                                                               0.3s
 => => exporting layers                                                                                                              0.3s
 => => writing image sha256:4488b97552b713e9df7167fb59749354812ae20918c9555a71dd1fe21adc4dd0                                         0.0s
 => => naming to docker.io/library/stucoding                                                                                         0.0s
[root@linux-local stucoding]#

10、查看docker镜像


[root@linux-local stucoding]# docker images
REPOSITORY                 TAG                 IMAGE ID       CREATED         SIZE
stucoding                  latest              4488b97552b7   2 days ago      614MB
stucoding/student_server   1.0-SNAPSHOT        71530864ac83   4 days ago      611MB
stucoding/student_server   0.0.1-SNAPSHOT      258e2fda3ac0   5 days ago      549MB
minio/minio                latest              a66f62e448f2   2 weeks ago     159MB
mongo                      4                   3593bb45b7ce   7 weeks ago     427MB
redis                      7                   170a1e90f843   3 months ago    138MB
mysql                      5.7                 5107333e08a8   4 months ago    501MB
nginx                      1.22                0f8498f13f3a   12 months ago   142MB
openjdk                    8                   b273004037cc   20 months ago   526MB
elasticsearch              7.17.3              3c91aa69ae06   2 years ago     613MB
kibana                     7.17.3              4897f4b8b6ee   2 years ago     797MB
logstash                   7.17.3              dd4291c803f4   2 years ago     774MB
rabbitmq                   3.9.11-management   6c3c2a225947   2 years ago     253MB
nacos/nacos-server         2.0.2               c8cd99bb135c   2 years ago     1.05GB
[root@linux-local stucoding]#

11、运行应用

docker run -p 8080:8080 --name stucoding \
--link mysql:db \
--link redis:redis \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/stucoding/logs:/var/logs \
-d stucoding

12、查看项目启动日志

 docker logs -f stucoding

四、Dockerfile常用指令

指令语法说明
FROMFROM :指明构建的新镜像是来自于哪个基础镜像,如果没有选择tag,那么默认值为latest
MAINTAINERMAINTAINER 指明镜像维护者及其联系方式(一般是邮箱地址)。官方说明已过时,推荐使用LABEL
LABELLABEL = ...功能是为镜像指定标签。也可以使用LABEL来指定镜像作者
RUNRUN 构建镜像时运行的Shell命令,比如构建的新镜像中我们想在/usr/local目录下创建一个java目录
ADDADD ... 拷贝文件或目录到镜像中。src 可以是一个本地文件,还可以是一个url。然后自动下载和解压
COPYCOPY ... 拷贝文件或目录到镜像中。用法同 ADD,只是不支持自动下载和解压
EXPOSEEXPOSE [/...]暴露容器运行时的监听端口给外部,可以指定端口是监听 TCP 还是 UDP,如果未指定协议,则默认为 TCP
ENVENV = ...设置容器内环境变量
CMDCMD ["executable","param1","param2"]启动容器时执行的Shell命令。在Dockerfile中只能有一条CMD指令。如果设置了多条CMD,只有最后一条会生效
ENTRYPOINTENTRYPOINT ["executable", "param1", "param2"]启动容器时执行的 Shell 命令,同 CMD 类似,不会被 docker run 命令行指定的参数所覆盖,如果设置了多条ENTRYPOINT,只有最后一条会生效
WORKDIRWORKDIR param为 RUN、CMD、ENTRYPOINT 以及 COPY 和 AND 设置工作目录
VOLUMEVOLUME ["param"]指定容器挂载点到宿主机自动生成的目录或其他容器。一般的使用场景为需要持久化存储数据时

五、前端在Linux环境下的部署

1、nginx安装参照open in new window

2、请求地址修改为虚拟机的ip

将所有的url地址http://localhost/ 修改为http://192.168.1.71/

axios.get('http://192.168.1.71:10086/course/deleteById/' + row.cid).then(function (resp) 

3、然后使用如下命令进行打包

npm run build

4、打包后的代码将保存在dist目录下

5、可以使用压缩软件将dist目录打包为dist.tar.gz文件;

Windows如何压缩tar.gz格式open in new window

6、将dist.tar.gz上传到linux服务器上,上传目录为Nginx的html目录

tar -zxvf dist.tar.gz

7、移动dist文件夹到admin文件夹;

mv dist stucoding-student
[root@linux-local html]# ll
total 1332
drwxrwxrwx. 6 root root      88 Apr 13 22:48 dist
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
[root@linux-local html]# mv dist stucoding-student
[root@linux-local html]# ll
total 1332
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
drwxrwxrwx. 6 root root      88 Apr 13 22:48 stucoding-student

8、需要修改nginx的默认配置

由于我们将使用服务器IP进行访问,所以我们还需要修改nginx的默认配置文件default.conf

vim /mydata/nginx/conf/conf.d/default.conf

默认

drwxrwxrwx. 6 root root      88 Apr 13 22:48 dist
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
[root@linux-local html]# mv dist stucoding-student
[root@linux-local html]# ll
total 1332
-rw-r--r--. 1 root root 1362618 Apr 13 22:54 dist.tar.gz
drwxrwxrwx. 6 root root      88 Apr 13 22:48 stucoding-student
[root@linux-local html]# cd /mydata/nginx/conf/conf.d/
[root@linux-local conf.d]# ll
total 4
-rw-r--r--. 1 root root 1093 Apr  4 11:54 default.conf
[root@linux-local conf.d]# ^C
[root@linux-local conf.d]# vim /mydata/nginx/conf/conf.d/default.conf
[root@linux-local conf.d]# cat /mydata/nginx/conf/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改后

    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

[root@linux-local conf.d]# ^C
[root@linux-local conf.d]# vim /mydata/nginx/conf/conf.d/default.conf
[root@linux-local conf.d]# cat /mydata/nginx/conf/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  192.168.1.71;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

将默认的server_namelocalhost改为192.168.1.71

9、修改完配置后重启nginx

docker restart nginx

10、访问首页并登录:http://192.168.1.71/stucoding-student/

docker build -t stucoding .

Last Updated 2024/7/8 20:48:47