xxxxxxxxxx
# Get the Jenkins Docker Image and Jenkins is now running on port 8080
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --restart=on-failure jenkins/jenkins:lts-jdk11
# You'll need the admin password
docker ps -a # copy the container_id
docker exec -it {container_id} /bin/bash
cat /var/jenkins_home/secrets/initialAdminPassword
xxxxxxxxxx
docker run \
--name jenkins \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins_home:/var/jenkins_home \
-d \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
jenkins/jenkins:jdk17-preview
xxxxxxxxxx
docker run -p 8080:8080 -p 50000:50000 -v ~/jenkins_home:/var/jenkins_home jenkins/jenkins:lts
xxxxxxxxxx
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts-jdk11
xxxxxxxxxx
docker run --name jenkins-docker --rm --detach \
--privileged --network jenkins --network-alias docker \
--env DOCKER_TLS_CERTDIR=/certs \
--volume jenkins-docker-certs:/certs/client \
--volume jenkins-data:/var/jenkins_home \
--publish 2376:2376 \
docker:dind --storage-driver overlay2
xxxxxxxxxx
docker network create jenkins \ &&
docker run \
--name jenkins-docker \ # ( Optional ) Specifies the Docker container name to use for running the image. By default, Docker generates a unique name for the container.
--rm \ # ( Optional ) Automatically removes the Docker container (the instance of the Docker image) when it is shut down.
--detach \ # ( Optional ) Runs the Docker container in the background. You can stop this instance by running docker stop jenkins-docker.
--privileged \ # Running Docker in Docker currently requires privileged access to function properly. This requirement may be relaxed with newer Linux kernel versions.
--network jenkins \ # This corresponds with the network created in the earlier step.
--network-alias docker \ # Makes the Docker in Docker container available as the hostname docker within the jenkins network.
--env DOCKER_TLS_CERTDIR=/certs \ # Enables the use of TLS in the Docker server. Due to the use of a privileged container, this is recommended, though it requires the use of the shared volume described below. This environment variable controls the root directory where Docker TLS certificates are managed.
--volume jenkins-docker-certs:/certs/client \ # Maps the /certs/client directory inside the container to a Docker volume named jenkins-docker-certs as created above.
--volume jenkins-data:/var/jenkins_home \ # Maps the /var/jenkins_home directory inside the container to the Docker volume named jenkins-data. This allows for other Docker containers controlled by this Docker container’s Docker daemon to mount data from Jenkins.
--publish 2376:2376 \ # ( Optional ) Exposes the Docker daemon port on the host machine. This is useful for executing docker commands on the host machine to control this inner Docker daemon.
docker:dind \ # The docker:dind image itself. Download this image before running, by using the command: docker image pull docker:dind.
--storage-driver overlay2 # The storage driver for the Docker volume. Refer to the Docker storage drivers documentation for supported options.
xxxxxxxxxx
pipeline {
agent { label 'master' }
stages {
stage('build') {
steps {
script {
def disk_size = sh(script: "df / --output=avail | tail -1", returnStdout: true).trim() as Integer
println("disk_size = ${disk_size}")
}
}
}
}
}
xxxxxxxxxx
pipeline {
agent any
environment {
GITLAB_REGISTRY_URL = 'registry.gitlab.com'
GITLAB_REPOSITORY_NAME = 'admin/node-helloworld-api'
DOCKER_COMPOSE_ORG_FILE = './node-helloworld-api/docker-compose.yml'
DOCKER_COMPOSE_PROD_FILE = './node-helloworld-api/docker-compose.prod.yml'
SSH_HOST = '192.168.1.1'
DIRECTORY = './node-helloworld-api'
}
options {
timestamps()
quietPeriod(5)
timeout(time: 30, unit: 'MINUTES')
durabilityHint('PERFORMANCE_OPTIMIZED')
}
stages {
stage('Clone Repository') {
steps {
git(branch: 'main', url: "https://gitlab.com/${GITLAB_REPOSITORY_NAME}", credentialsId: 'gitlab_credentials_id')
}
}
stage('Build image') {
steps {
withCredentials([usernamePassword(usernameVariable: 'GITLAB_USERNAME', passwordVariable: 'GITLAB_PASSWORD', credentialsId: 'gitlab_credentials_id')]) {
sh '''
docker login --username ${GITLAB_USERNAME} --password ${GITLAB_PASSWORD} ${GITLAB_REGISTRY_URL}
docker build -t ${GITLAB_REGISTRY_URL}/${GITLAB_REPOSITORY_NAME}:v${BUILD_NUMBER} --compress .
docker push ${GITLAB_REGISTRY_URL}/${GITLAB_REPOSITORY_NAME}:v${BUILD_NUMBER}
docker logout
'''
}
}
}
stage('Deploy App') {
steps {
withCredentials([
usernamePassword(usernameVariable: 'SSH_USERNAME', passwordVariable: 'SSH_PASSWORD', credentialsId: 'ssh_credentials_id'),
usernamePassword(usernameVariable: 'GITLAB_USERNAME', passwordVariable: 'GITLAB_PASSWORD', credentialsId: 'gitlab_credentials_token_id')
]) {
script {
def remoteServer = [:]
remoteServer.name = env.SSH_USERNAME
remoteServer.host = env.SSH_HOST
remoteServer.user = env.SSH_USERNAME
remoteServer.password = env.SSH_PASSWORD
remoteServer.allowAnyHosts = true
def commandServer = """
docker login --username ${GITLAB_USERNAME} --password ${GITLAB_PASSWORD} ${GITLAB_REGISTRY_URL}
if [ -f "${DOCKER_COMPOSE_PROD_FILE}" ]
then
cat ${DOCKER_COMPOSE_PROD_FILE}
else
git clone https://${GITLAB_USERNAME}:${GITLAB_PASSWORD}@gitlab.com/admin/node-helloworld-api.git
touch ${DOCKER_COMPOSE_PROD_FILE}
fi
awk '{gsub(/:v1/, ":v${BUILD_NUMBER}"); print}' ${DOCKER_COMPOSE_ORG_FILE} > ${DOCKER_COMPOSE_PROD_FILE}
docker-compose -f ${DOCKER_COMPOSE_PROD_FILE} up --pull missing --remove-orphans --force-recreate --wait -d
docker logout
rm -rf ${DIRECTORY}
"""
sshCommand(remote: remoteServer, command: commandServer)
}
}
}
}
}
}
xxxxxxxxxx
docker run \
--name jenkins-docker \(1)
--rm \(2)
--detach \(3)
--privileged \(4)
--network jenkins \(5)
--network-alias docker \(6)
--env DOCKER_TLS_CERTDIR=/certs \(7)
--volume jenkins-docker-certs:/certs/client \(8)
--volume jenkins-data:/var/jenkins_home \(9)
--publish 2376:2376 \(10)
docker:dind \(11)
--storage-driver overlay2(12)