Automate WebLogic in Docker Containers with a Shell Script


GIT Repository "LINK"

In this blog post, we'll walk through a shell script designed to automate the management of Oracle WebLogic in Docker containers. The script performs various tasks, such as setting up WebLogic, configuring the server, and managing the Node Manager and Admin Server within a Docker container.

Note: If you don't care for a detailed explanation, scroll to the bottom of the post for the complete combined script.

1. Setting Up the domain.properties File

The script first checks if a domain.properties file exists at the specified location. This file contains important credentials (admin-username and admin-password) for WebLogic, which are required for WebLogic's management console.


properties_file="/home/ubuntu/domain.properties" if [ ! -f "$properties_file" ]; then echo "$properties_file not found. Creating it with admin-username and admin-password..." echo "admin-username=weblogic" > "$properties_file" echo "admin-password=weblogic@123" >> "$properties_file" else echo "$properties_file already exists. No changes made." fi chmod 664 "$properties_file"

The script ensures that the file exists, and if not, creates it with default credentials. Then, it sets the appropriate file permissions (664), ensuring that the owner and group can read and write to the file.

2. Removing Any Existing Containers

Before launching a new WebLogic container, the script checks if any containers named weblogic-container already exist. If so, it stops and removes them to avoid conflicts.


existing_container=$(docker ps -a -q -f "name=weblogic-container") if [ ! -z "$existing_container" ]; then echo "Found existing WebLogic container(s). Removing them..." docker rm -f $existing_container fi

This ensures that a fresh WebLogic container is always started.

3. Launching a New WebLogic Docker Container

The script uses Docker to launch a new container based on the container-registry.oracle.com/middleware/weblogic:12.2.1.4 image. It maps ports, mounts the domain.properties file, and runs the container in the background.


docker run --privileged -d \ -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ -v $properties_file:/u01/oracle/properties/domain.properties \ --name weblogic-container \ -p 7001:7001 -p 7004:7004 -p 5556:5556 \ container-registry.oracle.com/middleware/weblogic:12.2.1.4

This command starts the WebLogic container with the necessary configurations, making it accessible on ports 7001, 7004, and 5556.

4. Installing Required Packages

Inside the container, the script installs essential packages (procps and net-tools) that are required for process management and networking.

docker exec -u root -it weblogic-container yum install -y procps net-tools

These tools help monitor and manage processes within the container.

5. Modifying config.xml to Disable Administration Port

The script then modifies the WebLogic config.xml to disable the administration port. This is done by searching for the <administration-port-enabled> tag and changing its value to false.

docker exec $container_id bash -c "sed -i 's/<administration-port-enabled>true<\/administration-port-enabled>/<administration-port-enabled>false<\/administration-port-enabled>/' $config_path" docker exec $container_id bash -c "grep '<administration-port-enabled>false</administration-port-enabled>' $config_path"

This ensures that WebLogic's administration port is not exposed.

6. Creating Log Directories for NodeManager and AdminServer

The script creates a logs directory inside the WebLogic domain directory if it doesn't already exist. This directory is used to store log files for NodeManager and AdminServer.


docker exec $container_id bash -c "mkdir -p /u01/oracle/user_projects/domains/base_domain/logs"

This ensures that logs are properly stored and managed within the container.

7. Starting NodeManager

The script starts the NodeManager process, which is used for managing WebLogic Server instances remotely.

start_nodemanager="/u01/oracle/user_projects/domains/base_domain/bin/startNodeManager.sh" docker exec $container_id bash -c "nohup $start_nodemanager > /u01/oracle/user_projects/domains/base_domain/logs/nodemanager.out 2>&1 &"

NodeManager is started in the background, and its output is redirected to a log file.

8. Stopping the AdminServer

Before restarting the AdminServer, the script stops it by killing its process. This is achieved by identifying the WebLogic server process and terminating it.


docker exec $container_id bash -c "ps aux | grep '[w]eblogic.Server' | awk '{print \$2}' | xargs -I {} kill -9 {}"

The ps aux command lists all running processes, and kill -9 terminates the AdminServer process.

9. Restarting the AdminServer

After stopping the AdminServer, the script restarts it using the startWebLogic.sh script.

start_script="/u01/oracle/user_projects/domains/base_domain/bin/startWebLogic.sh" docker exec $container_id bash -c "nohup $start_script > /u01/oracle/user_projects/domains/base_domain/logs/adminserver.out 2>&1 &"

The AdminServer is started in the background, and its output is directed to a log file for monitoring.

10. Checking the Status of the WebLogic Container

The script checks the status of the WebLogic Docker container to ensure it is running correctly.

docker ps -a -f "name=weblogic-container"

It then lists all running processes inside the container to confirm that the AdminServer and NodeManager are functioning.

docker exec $container_id ps -ef

11. Completion

Finally, the script prints a completion message to indicate that all tasks have been completed successfully.

echo "WebLogic container management completed."


Complete Script:

#####################################