tomcat + mysql + war使用docker-compose.yml

时间:2018-12-10 08:03:41

标签: mysql docker tomcat docker-compose

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

docker-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

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

Tomcat和Mysql已成功安装。但是我的warinit sql script不好。 我的docker-compose.yml

中是否缺少任何配置

1 个答案:

答案 0 :(得分:0)

对于数据库:

也许您遇到的问题与此处所述的相同: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。容器启动时,您应该在标准输出中看到以下内容:

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

对于Tomcat:

也许您的应用程序由于数据库问题而无法运行。但是,如果没有,您可以尝试以下操作:

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

docker exec -it tomcat bash

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

 /usr/local/tomcat/webapps/