docker-compose.yaml:
nginx:
image: nginx
links:
- tomcat
ports:
- "80:80"
volumes:
- /root/conf:/etc/nginx/conf.d:ro
tomcat:
image: tomcat:8.5
links:
- mysql
volumes:
- /root/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
- /root/manager.xml:/usr/local/tomcat/conf/Catalina/localhost/manager.xml
mysql:
image: mysql
volumes:
- /opt/data:/var/lib/mysql
- /root/codepad.sql:/docker-entrypoint-initdb.d/codepad.sql
environment:
- MYSQL_ROOT_PASSWORD=xxxxxx
注意tomcat的manager默认只有本机能访问,如果需要非本机访问的,需要配置manager.xml,不然访问tomcat控制台会报403,位置在:$CATALINA_BASE/conf/[enginename]/[hostname]
,内容如下:
<Context privileged="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow=".*"/>
</Context>
启动docker container:
docker-compose up -d
java代码中连接数据库,字符串写法为:
url=jdbc:mysql://${MYSQL_PORT_3306_TCP_ADDR}:3306/codepad?useUnicode=true&characterEncoding=UTF-8
MYSQL_PORT_3306_TCP_ADDR是mysql container的ip地址,intellij idea 本地运行时得在run/debug窗口事先配置好.
参考文档见: