You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Toolbox中无法运行Spring Boot容器问题咨询

Hey there, let's work through your Spring Boot + Docker issues one by one to get things up and running smoothly:

1. Jar包压缩与Spring Boot打包问题排查

First off, let's rule out the most common culprit: a mispackaged Spring Boot JAR.

  • Verify your Maven plugin setup: Spring Boot requires the spring-boot-maven-plugin to build an executable "fat JAR" that includes all dependencies. Double-check your pom.xml has this configured correctly (it should include the repackage goal):
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Test the JAR locally first: Run java -jar target/your-app-name.jar on your host machine. If this fails, the issue is with your Spring Boot build, not Docker. Fix any startup errors here before moving to containers.
  • Avoid incorrect Maven commands: Use mvn clean package to build your JAR — mvn clean jar only packages your project code without dependencies, which will fail to run.

2. Docker Toolbox vs. Spring: Where's the Issue?

Docker Toolbox runs Docker inside a virtual machine (VM), which adds a few quirks to watch for:

  • Check your Dockerfile: Make sure it uses a valid JRE base image and correctly executes the JAR. Example of a working Dockerfile:
# Use a lightweight JRE image matching your Spring Boot version's Java requirement
FROM openjdk:17-jre-slim
# Copy your built JAR into the container
COPY target/your-app-name.jar app.jar
# Define the command to run the app
ENTRYPOINT ["java", "-jar", "/app.jar"]
  • Check container logs (critical!): Don't guess at exceptions — run docker logs <your-container-id> to see exactly what's failing. Logs will tell you if it's a database connection error, missing dependencies, or a corrupted JAR.
  • Docker Toolbox network note: The Docker daemon runs in a VM, so localhost in your Spring Boot config won't point to your host machine. Use docker-machine ip default to get the VM's IP address, and update your database URL to use this IP instead of localhost.

3. MySQL Container Visibility & Connection Issues

It sounds like you're having trouble locating your MySQL container or connecting to it. Let's fix that:

  • Correct container commands: The command docker container -p is invalid — use docker container ps -p to list running containers with their port mappings, or docker port <mysql-container-id> to get just the port info.
  • Verify MySQL is running:
    1. Check if the MySQL image exists locally: docker images | grep mysql
    2. Check if the container is running: docker container ls -a (look for your MySQL container's name/ID)
    3. If it's not running, start it with a valid command (replace placeholders):
    docker run -d --name mysql-db \
      -e MYSQL_ROOT_PASSWORD=your-root-pass \
      -e MYSQL_DATABASE=your-app-db \
      -p 3306:3306 \
      mysql:8.0
    
  • Simplify with Docker Compose: Managing multiple containers (Spring Boot + MySQL) manually is error-prone. Use Docker Compose to handle networking automatically — your Spring Boot app can connect to MySQL using the service name mysql instead of an IP:
version: '3.8'
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root123
      MYSQL_DATABASE: demo_db
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
  spring-app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - mysql
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/demo_db?useSSL=false&serverTimezone=UTC
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root123
volumes:
  mysql-data:

Run docker-compose up --build to start both containers together.


内容的提问来源于stack exchange,提问作者Arun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:09:16