Using Docker in development for Java EE applications

后端 未结 7 1084
北荒
北荒 2021-02-05 13:59

I will add 300 points as bounty

I have recently started to take a closer look at Docker and how I can use it for faster getting new member of t

7条回答
  •  醉梦人生
    2021-02-05 14:51

    With the latest version of Docker, you can achieve that easily with Docker Links, Docker Volume and Docker Compose. More information about these tools from Docker site.

    Back to your workflow as you have mentioned: for any typical Java EE application, an application server and a database server are required. Since you do not mention in your post how the database is set up, I would assume that your development environment will have separated database server for each developer.

    Taking all these into assumption, I could suggest the following workflow:

    • Build the base Wildfly application server from the official image. You can achieve that by: "docker pull" command
    • Run the base application server with:

    docker run -d -it -p 8080:8080 -p 9990:9990 --name baseWildfly jboss/wildfly

    The application server is running now, you need to configure it to connect to your database server and also configure the datasource settings and other configuration if neccessary in order to start your Java EE application. For this, you need to log into bash terminal of the Jboss container:

    docker exec -i -t baseWildfly /bin/bash/

    You are now in the terminal of container. You can configure the application server as you do for any linux environment.

    You can test the configuration by manually deploying the WAR file to Wildfly. This can be done easily with the admin console, or maven plugin, or ADD command as you said. I usually do that with admin console, just for testing quickly. When you verify that the configuration works, you can remove the WAR file and create a snapshot of your container:

    docker commit --change "add base settings and configurations" baseWildfly yourRepository:tag

    You can now push the created image to your private repository and share that with your developer team. They can now pull the image and run the application server to deploy right away.

    We don't want to deploy the WAR file for every Maven build using admin console as that is too cumbersome, so next task is to automate it with Docker Volume.

    Assuming that you have configured Maven to build the WAR file to "../your_project/deployments/", you can link that to deployment directory of Jboss container as following:

    docker run -d -p 8080:8080 -v ../your_project/deployments:/opt/jboss/wildfly/standalone/deployments

    Now, every time you rebuild the application with Maven, the application server will scan for changes and redeploy your WAR file.

    It is also quite problematic to have separated database server for each developer, as they have to configure it by themselves in the container because they might have different settings (e.g. db's url, username, password, etc...). So, it's good to dockerize that eventually.

    Assuming you use Postgres as your db server, you can pull it from postgres official repository. When you have the image ready, you can run the db server:

    docker run -d -p 5432:5432 -t --name postgresDB postgres

    or run the database server with the linked "data" directory:

    docker run -d -p 5432:5432 -v ../your_postgres/data:/var/lib/postgresql -t --name postgresDB postgres

    The first command will keep your data in the container, while the latter one will keep your data in the host env.

    Now you can link your database container with the Wildfly:

    docker run -d -p 8080:8080 --link postgresDB:database -t baseWildfly

    Following is the output of linking:

    Now you can have the same environment for all members in developer's team and they can start coding with minimal set up.

    The same base images can be used for Production environment, so that whenever you want to release new version, you just need to copy the WAR file to "your_deployment" folder of the host.

    The good thing of dockerizing application server and db server is that you can cluster it easily in the future to scale it or to apply the High Availability.

提交回复
热议问题