How to change user config in Docker tomcat 8?


Introduction

In a Docker Tomcat 8 container, you may want to change the user configuration to add or modify users and roles. This modification can be useful for tasks like securing access to the Tomcat 8 instance or setting up different levels of permissions for different users.

This article explains how to change the user configuration in a Docker Tomcat 8 container. We cover the steps involved in modifying the tomcat-users.xml file, building a new Docker image with the modified configuration, and running a Docker container from the new image.

Prerequisites

Before changing the user configuration in a Docker Tomcat 8 container, you must install Docker on your machine. Docker can be downloaded from the Docker website − https://www.docker.com/get-started

Once you have installed Docker, you should pull the Tomcat 8 Docker image from the Docker Hub. To do this, open a terminal window and run the following command −

$ docker pull tomcat:8 

This command downloads the Tomcat 8 Docker image and saves it locally on your machine. The Docker image includes the Tomcat 8 application server and its dependencies.

Modifying the Tomcat 8 user configuration

To change the user configuration in a Docker Tomcat 8 container, you must modify the tomcat-users.xml file. This file is located in the conf directory of the Tomcat 8 installation and defines the users and roles that are allowed to access the Tomcat 8 instance.

Example 

An example of a tomcat-users.xml file is shown below.

<tomcat-users>
   <user username="admin" password="password" roles="admin-gui,manager-gui"/>
   <user username="deployer" password="password" roles="manager-script"/>
   <user username="tomcat" password="password" roles="manager-gui"/>
</tomcat-users>

Add a user to this file.

<tomcat-users>
   <user username="admin" password="password" roles="admin-gui,manager-gui"/>
   <user username="deployer" password="password" roles="manager-script"/>
   <user username="tomcat" password="password" roles="manager-gui"/>
   <user username="newuser" password="newpassword" roles="manager-gui"/>
</tomcat-users>

To modify the tomcat-users.xml file, you can either edit the file directly on your host machine or copy the file into a new Docker image and modify the copy. Here's an example of how you might do the latter &moinus;

  • Please create a new directory on your host machine and copy the tomcat-users.xml file into it.

  • Edit the tomcat-users.xml file to add or modify the users and roles that you want.

  • Create a Dockerfile in the same directory with the following content −

#use tomcat:8 as the base image.
FROM tomcat:8

#copy the user configuration file into the container.
COPY tomcat-users.xml /usr/local/tomcat/conf/

This Dockerfile creates a new Docker image based on the Tomcat 8 image. Now copy the modified tomcat-users.xml file into the conf directory of the Tomcat 8 installation.

$ docker build -t my-tomcat-image . 

This command builds a new Docker image with the name my-tomcat-image based on the Dockerfile in the current directory (indicated by the.). The new image includes the modified tomcat-users.xml file.

$ docker run -d -p 8080:8080 my-tomcat-image 

This command starts a Docker container in detached mode (-d) and maps the host's port 8080 to the container's port 8080 (-p 8080:8080). The container is based on the my-tomcat-image image that you built earlier.

Verifying the changes to the user configuration

To verify that the changes to the user configuration have been applied, you can connect to the Tomcat 8 instance using a web browser. To do this, visit the URL http://localhost:8080 (or the port number you specified when you started the container).

You should see the Tomcat 8 login page. If you added or modified a user in the tomcat-users.xml file, you should be able to log in using that user's credentials.

Example 

To stop the Docker container, you can use the docker stop command and specify the container ID or name. For example −

$ docker stop my-tomcat-container 

To restart the container, you can use the docker start command in the same way −

$ docker start my-tomcat-container 

Some other ways

  • Using environment variables to set the user configuration. You can use the -e flag when running the docker run command to set environment variables in the container. For example, you could set an environment variable called TOMCAT_USER with the value of a user to add to the tomcat-users.xml file. You could then use a script to read the environment variables and update the tomcat-users.xml file in the container.

  • Using a configuration management tool like Ansible, chef, or puppet to automate the process of changing the user configuration. These tools allow you to define the desired configuration in a declarative way, and apply the configuration to the container as part of the container's lifecycle.

  • Using a continuous integration and deployment tool like Jenkins, Travis, or CircleCI to automate the process of building and deploying Docker images with modified user configurations. These tools allow you to define a pipeline for building and deploying Docker images, and can be triggered by code changes or other events.

Conclusion

This article explained how to change the user configuration in a Docker Tomcat 8 container. We covered the steps involved in modifying the tomcat-users.xml file, building a new Docker image with the modified configuration, and running a Docker container from the new image. We also discussed advanced topics, including mounting volumes and Docker Compose.

Changing the user configuration in a Docker Tomcat 8 container is a useful way to secure access to the Tomcat 8 instance and manage different levels of permissions for different users. Following this article's steps, you can easily customize the user configuration to suit your needs.

Updated on: 30-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements