Automated Nginx Proxy and Docker Container Management with Jenkins
In this article, we will explore the automated deployment of Nginx proxy and Docker containers using Jenkins. We will cover the creation of a dynamic Dockerfile, building a Docker image, and starting the container with a specified name and port. Additionally, we will discuss the management of container port conflicts and the configuration of Nginx proxy.
Step 1: Construction of Dynamically Generated Dockerfile
The first step is to create a dynamic Dockerfile that will be used to build the Docker image. This file will be created using the cat command and will include the necessary instructions to build the image.
# Create a dynamic Dockerfile
rm -f Dockerfile
cat > Dockerfile << EOF
FROM tomcat:8-jre8
MAINTAINER xiaochangwei <changw.xiao@qq.com>
LABEL app="testApp" version="0.0.1" by="xiaochangwei"
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY ./target/*.war /usr/local/tomcat/webapps/$NAME.war
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
EOF
Step 2: Building Docker Image
The next step is to build the Docker image using the dynamic Dockerfile. This is done using the docker build command.
# Build the Docker image
docker build -t $IMAGE --rm=true
Step 3: Starting the Container
After building the Docker image, we need to start the container with a specified name and port. We can do this using the docker run command.
# Start the container
docker run -d --privileged=true --env JAVA_OPTS=-Duser.timezone=GMT+08 -p $PORT:8080 --name $NAME $IMAGE
Step 4: Managing Container Port Conflicts
If the port specified is already in use, the container will not start. To manage this, we can check if the port is occupied and stop the corresponding container if it is.
# Check if the port is occupied
if [-n "$PORT"]; then
containers=$(docker ps -a --filter status=running | grep ':' $PORT | awk '{print $1}')
echo 'containers:' $containers
if [ ${#containers[@]} -gt 0 ]; then
echo 'stop container:' $containers
docker stop $containers
for container in $containers; do
docker stop $container
done
fi
fi
Step 5: Configuring Nginx Proxy
To configure the Nginx proxy, we need to update the configuration file and restart the Nginx service. This is done using the docker exec command.
# Update the Nginx configuration file
docker exec -i nginx nginx -s reload
Automating the Process with Jenkins
To automate this process, we can use Jenkins to create a pipeline that will perform the steps outlined above. This pipeline can be triggered manually or automatically based on changes to the code.
# Jenkins pipeline
pipeline {
agent any
stages {
stage('Build Docker Image') {
steps {
sh 'docker build -t $IMAGE --rm=true'
}
}
stage('Start Container') {
steps {
sh 'docker run -d --privileged=true --env JAVA_OPTS=-Duser.timezone=GMT+08 -p $PORT:8080 --name $NAME $IMAGE'
}
}
stage('Manage Container Port Conflicts') {
steps {
sh 'docker ps -a --filter status=running | grep ':' $PORT | awk {print $1}'
if [ ${#containers[@]} -gt 0 ]; then
sh 'docker stop $containers'
for container in $containers; do
sh 'docker stop $container'
done
fi
}
}
stage('Configure Nginx Proxy') {
steps {
sh 'docker exec -i nginx nginx -s reload'
}
}
}
}
By automating the deployment of Nginx proxy and Docker containers using Jenkins, we can improve the efficiency and reliability of our development process.