使用docker compose部署tomcat应用程序

    科技2026-08-24  2

    For more article, check out my blog https://devops4solutions.com/

    有关更多文章,请查看我的博客https://devops4solutions.com/

    In this blog, we will learn what is docker-compose and how we can deploy a tomcat application which uses mysql database. We will learn how we can setup a development environment using docker-compose in a single command

    在此博客中,我们将学习什么是docker-compose以及如何部署使用mysql数据库的tomcat应用程序。 我们将学习如何在单个命令中使用docker-compose设置开发环境

    Prerequisite:

    先决条件:

    Docker and Docker-compose installed

    安装了Docker和Docker-compose

    INTRODUCTION

    介绍

    Docker-compose is a tool which is used to deploy multi-container application.

    Docker-compose是用于部署多容器应用程序的工具。One single yaml file to deploy your application on the server.

    一个单独的yaml文件,用于在服务器上部署您的应用程序。 Best suited for the developers to setup their workstation in a single command without installing any kind of dependencies for the application

    最适合开发人员在单个命令中设置工作站而无需为应用程序安装任何类型的依赖项

    docker-compose up to start your application

    docker-compose up启动您的应用程序

    docker-compose down to clean up all the docker containers

    docker-compose down清理所有docker容器

    Let’s take an example here:

    让我们在这里举个例子:

    We have a project called user registration which uses mysql for storing the data . In terms of microservices, we can say that we have two services as shown below:

    我们有一个名为用户注册的项目,该项目使用mysql来存储数据。 在微服务方面,可以说我们有两个服务,如下所示:

    Web Service

    网络服务 Database Service

    数据库服务

    You can clone this git repo and try the below example

    您可以克隆此git repo并尝试以下示例

    Explanation of docker-compose

    docker-compose的说明

    version : This is the version as per the docker engine you have installed on your machine

    版本:这是根据您在计算机上安装的docker引擎的版本

    services: This is the main tag which is used to configure multiple services and under that we have details of all the services

    服务:这是用于配置多个服务的主要标签,在该标签下,我们具有所有服务的详细信息

    3. web: This is our service name -> using image, ports and volumes

    3. web :这是我们的服务名称->使用映像,端口和卷

    4. volumes: To store the database files

    4.卷:用于存储数据库文件

    Now we will create main docker-compose file which will together launch a tomcat, mysql and phpmyadmin container

    现在我们将创建主docker-compose文件,该文件将一起启动tomcat,mysql和phpmyadmin容器

    Tomcat container — To run your application

    Tomcat容器-运行您的应用程序

    Database container — To store the data

    数据库容器—存储数据

    PhpMyAdmin — Access the database through GUI

    PhpMyAdmin-通过GUI访问数据库

    So we will have three services

    因此,我们将提供三项服务

    db — we are using local path to store the data so that when you run docker-compose down all your data will retain. If you use the volume then all data will get lost if you run the docker-compose down

    db —我们正在使用本地路径来存储数据,以便当您运行docker-compose down所有数据都将保留。 如果使用该卷,则在运行docker-compose down会丢失所有数据

    Also, we are importing sql file so that our database is ready with the sample data. It is good so that each developer will always have the base or the actual data to run their application on the local machine

    另外,我们正在导入sql文件,以便我们的数据库已准备好样本数据。 很好,这样每个开发人员都将始终具有基础或实际数据,以在本地计算机上运行他们的应用程序

    2. phpmyadmin — which is used to access the database through GUI and it depends on service db

    2. phpmyadmin-用于通过GUI访问数据库,它取决于服务db

    3. web — which is used to access your web application and it also depends on service db

    3. web-用于访问您的Web应用程序,它还取决于服务db

    version: '3.3'services: db: image: mysql:5.7 volumes: - /opt/test:/var/lib/mysql - ./mysql-dump:/docker-entrypoint-initdb.d environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: testdb1 MYSQL_USER: testuser MYSQL_PASSWORD: root ports: - 3306:3306phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin ports: - '8081:80' environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: rootweb: depends_on: - db image: tomcat volumes: - ./target/LoginWebApp-1.war:/usr/local/tomcat/webapps/LoginWebApp-1.war ports: - '8082:8080' environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: testdb1 MYSQL_USER: testuser MYSQL_PASSWORD: root

    Run the command

    运行命令

    docker-compose up -d

    All the environment variables which we are passing like mysql database details for the tomcat container can be used in the code.

    我们正在传递的所有环境变量(例如tomcat容器的mysql数据库详细信息)都可以在代码中使用。

    You can now browse the url

    您现在可以浏览网址

    http://yourip:8082/LoginWebApp-1/

    http:// yourip:8082 / LoginWebApp-1 /

    Click on register and fill the below details and then check the database it has inserted all the values in USER table. This shows that tomcat application is able to make a database connection.

    单击注册并填写以下详细信息,然后检查数据库是否已将所有值插入USER表中。 这表明tomcat应用程序能够建立数据库连接。

    Now I will be running those as a separate container and will show how exactly it works

    现在,我将它们作为单独的容器运行,并显示其工作原理

    Now, lets start creating a tomcat container using sample index.html

    现在,让我们开始使用示例index.html创建一个tomcat容器

    version: '3'services: web: image: tomcat ports: - "8081:8080" volumes: - ./index.html:/usr/local/tomcat/webapps/test/index.html

    Now you can run this compose file in detached mode and can see the output “Server startup” at the end:

    现在,您可以在分离模式下运行此撰写文件,并可以在末尾看到“ Server startup ”的输出:

    docker-compose -f docker-compose_tomcat.yml up -d

    Now if you try to access it using http://yourip:8081, you will see the below error. You are getting this error because webapps folder is empty . There is no sample example folder is present.

    现在,如果您尝试使用http:// yourip:8081访问它,则会看到以下错误。 由于webapps文件夹为空,因此出现此错误。 没有示例文件夹的示例存在。

    But as we have put the index.html we can try to access that

    但是,当我们放置index.html时,我们可以尝试访问它

    http://yourip:8081/test

    http:// yourip:8081 / test

    You should be able to see the content of your index.html.

    您应该能够看到index.html的内容。

    This shows that we have successfully launched tomcat inside a docker container using docker-compose.

    这表明我们已经使用docker-compose在docker容器内成功启动了tomcat。

    Clean the docker container

    清洁Docker容器

    docker-compose -f docker-compose_tomcat.yml down

    Install Mysql

    安装Mysql

    Now we will use the mysql image and launch a container

    现在我们将使用mysql映像并启动一个容器 This will create a volume on your docker machine and all data will be removed if you delete the container .

    这将在您的docker机器上创建一个卷,并且如果您删除容器,所有数据将被删除。You need to declare db_data1 in volume section at the bottom.

    您需要在底部的卷部分中声明db_data1。 version: '3.3'services: db: image: mysql:5.7 volumes: - db_data1:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: testdb1 MYSQL_USER: testuser MYSQL_PASSWORD: root ports: - 3306:3306volumes: db_data1: name: db_data1

    Now run this

    现在运行这个

    docker-compose -f docker-compose_mysql.yml up -d

    As you can see all mysql db details are created inside a container if you use the volume approach

    如您所见,如果使用卷方法,则会在容器内创建所有mysql db详细信息

    How to connect using command prompt

    如何使用命令提示符进行连接

    mysql -utestuser -proot1 -h 127.0.0.1

    Tear down container and volumes

    拆下容器和体积

    # To Tear Down$

    Install Mysql with phpadmin so that we can access the database through GUI

    使用phpadmin安装Mysql,以便我们可以通过GUI访问数据库

    We are using the local path for the database so that data will retain even if container get deleted

    我们正在使用数据库的本地路径,以便即使删除容器也将保留数据

    We are creating a new user testuser for our database. If you want to use same root user then you do not need to provide the user parameter ,only password parameter is required to set the password

    我们正在为数据库创建一个新的用户testuser 。 如果要使用相同的root用户,则无需提供user参数,只需密码参数即可设置密码

    version: '3.3'services: db: image: mysql:5.7 volumes: - /opt/test:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: testdb1 MYSQL_USER: testuser MYSQL_PASSWORD: root ports: - 3306:3306phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin ports: - '8081:80' environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: root

    Run the docker-compose

    运行docker-compose

    docker-compose -f docker-compose_mysql_phpMyAdmin.yml up -d

    Logged on phpmyadmin and create some table to test if it persist or not

    登录phpmyadmin并创建一些表以测试其是否持久

    http://yourip:8081/

    http:// yourip:8081 /

    Now clean your container

    现在清洁您的容器

    docker-compose -f docker-compose_mysql_phpMyAdmin.yml down

    Rerun the container

    重新运行容器

    docker-compose -f docker-compose_mysql_phpMyAdmin.yml up -d

    You should be able to see the table which you have created

    您应该能够看到已创建的表

    How to import .sql file in docker-compose file

    如何在docker-compose文件中导入.sql文件

    This is required if you need to import the existing sql file so that every developer who runs the container can have all the database tables and some sample data created

    如果需要导入现有的sql文件,则这是必需的,以便每个运行容器的开发人员都可以创建所有数据库表和一些示例数据

    mysql-dump must be a directory. All the .sql’s in the directory will be imported.

    mysql-dump必须是目录。 目录中的所有.sql文件都将被导入。

    version: '3.3'services: db: image: mysql:5.7 volumes: - /opt/test:/var/lib/mysql - ./mysql-dump:/docker-entrypoint-initdb.d environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: testdb1 #MYSQL_USER: testuser MYSQL_PASSWORD: root ports: - 3306:3306phpmyadmin: depends_on: - db image: phpmyadmin/phpmyadmin ports: - '8081:80' environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: root

    Congratulations, you have successfully installed the docker-compose and ran the tomcat application with mysql database using docker-compose.

    恭喜,您已成功安装docker-compose并使用docker-compose在mysql数据库中运行了tomcat应用程序。

    翻译自: https://medium.com/swlh/deploy-a-tomcat-application-using-docker-compose-d34db50edddd

    Processed: 0.010, SQL: 9