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-pluginto build an executable "fat JAR" that includes all dependencies. Double-check your pom.xml has this configured correctly (it should include therepackagegoal):
<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.jaron 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 packageto build your JAR —mvn clean jaronly 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
localhostin your Spring Boot config won't point to your host machine. Usedocker-machine ip defaultto get the VM's IP address, and update your database URL to use this IP instead oflocalhost.
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 -pis invalid — usedocker container ps -pto list running containers with their port mappings, ordocker port <mysql-container-id>to get just the port info. - Verify MySQL is running:
- Check if the MySQL image exists locally:
docker images | grep mysql - Check if the container is running:
docker container ls -a(look for your MySQL container's name/ID) - 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 - Check if the MySQL image exists locally:
- 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
mysqlinstead 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
相关产品推荐
相关产品推荐

