使用docker-compose.yml进行tomcat mysql war

我正在使用docker-compose.yml来构建我的环境 .

泊坞窗,compose.yml

version: '3'

    services:
      mysql:
        image: mysql:5.5
        container_name: mysql
        restart: always
        environment:
          MYSQL_DATABASE: my-test-app
          MYSQL_ROOT_PASSWORD: root
        volumes:
          - ./docker/db:/docker-entrypoint-initdb.d
        ports:
           - "3306:3306"

      tomcat:
        image: tomcat:8.5.35
        container_name: tomcat
        volumes:
          - ./docker/myapp.war:/usr/local/tomcat/webapps/myapp.war
        ports:
          - "8080:8080"

我的文件夹结构

my-env            
├── docker-compose.yml
├── docker
    ├── myapp.war
    ├── db
         ├── DB_INIT.SQL

DB_INIT.SQL

CREATE TABLE EMPLOYEE (
  EMPLOYEE_ID BIGINT(20) NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(65) NOT NULL,
  DOB DATETIME DEFAULT NULL,
  GENDER VARCHAR(10) NOT NULL,
  MOBILE VARCHAR(65) DEFAULT NULL,
  EMAIL VARCHAR(60) DEFAULT NULL,
  PRIMARY KEY (EMPLOYEE_ID)
) ENGINE=INNODB DEFAULT CHARSET=UTF8MB4;


INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Zaw Than Oo", '1986-12-02', 'MALE', '09420000773', 'zawthanoo@cycdemo.com');
INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Myo Thiha Kyaw", '1990-10-25', 'MALE', '0942568971', 'mthk@cycdemo.com');
INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Htet Wai Yan Kyaw", '1991-12-15', 'MALE', '0942025023', 'hwyk@cycdemo.com');

运行此表达后 docker-compose up

我的docker-machine ip是 192.168.99.100 . 我可以访问 http://192.168.99.100:8080 的tomcat,但我无法访问我的应用程序 http://192.168.99.100:8080/myapp . 这是 404 .

我可以使用 MySQL Work Branch 从我的主机访问 docker's mysql . 在 MySQL Work Branch 中,我看到 my-test-app 数据库 . 但是没有 EMPLOYEE 表 .

Tomcat和Mysql是成功安装的 . 但我的 warinit sql script 不行 . 我的 docker-compose.yml 中是否有任何丢失的配置

回答(1)

2 years ago

For the Database:

也许你遇到的问题与这里描述的相同:https://stackoverflow.com/a/36348572/8221303

1. 首先运行此命令以删除当前容器:

docker-compose rm -vf

2. 从撰写文件中删除数据库名称 .

3. 修改数据库创建文件,如下所示:

CREATE DATABASE `my-test-app`;
USE my-test-app;

CREATE TABLE EMPLOYEE (
  EMPLOYEE_ID BIGINT(20) NOT NULL AUTO_INCREMENT,
  NAME VARCHAR(65) NOT NULL,
  DOB DATETIME DEFAULT NULL,
  GENDER VARCHAR(10) NOT NULL,
  MOBILE VARCHAR(65) DEFAULT NULL,
  EMAIL VARCHAR(60) DEFAULT NULL,
  PRIMARY KEY (EMPLOYEE_ID)
) ENGINE=INNODB DEFAULT CHARSET=UTF8MB4;


INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Zaw Than Oo", '1986-12-02', 'MALE', '09420000773', 'zawthanoo@cycdemo.com');
INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Myo Thiha Kyaw", '1990-10-25', 'MALE', '0942568971', 'mthk@cycdemo.com');
INSERT INTO EMPLOYEE (NAME, DOB, GENDER, MOBILE, EMAIL) VALUES ("Htet Wai Yan Kyaw", '1991-12-15', 'MALE', '0942025023', 'hwyk@cycdemo.com');

4. 再次运行Docker-Compose . 当容器启动时,您应该在stdout中看到类似的内容:

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/DB_INIT.sql

For the Tomcat:

也许你的应用程序因DB问题而无法运行 . 但如果没有,你可以尝试以下方法:

首先检查是否正确部署了WAR文件 . 通过以下方式连接到容器bash:

docker exec -it tomcat bash

检查webapps目录中是否存在带有已部署war文件的war文件:

/usr/local/tomcat/webapps/