Skip to content

Instantly share code, notes, and snippets.

@badgeek
Created July 23, 2024 07:29
Show Gist options
  • Save badgeek/b21678b2bc09349a79bf3011759b1b35 to your computer and use it in GitHub Desktop.
Save badgeek/b21678b2bc09349a79bf3011759b1b35 to your computer and use it in GitHub Desktop.
FROM cypress/base
COPY . /opt/app
WORKDIR /opt/app
RUN npx cypress install # Install Cypress binary into image
COPY . .
ENTRYPOINT ["npx", "cypress", "run"]
@badgeek
Copy link
Author

badgeek commented Jul 23, 2024

To execute Docker commands inside a Jenkins Docker container, you need to set up Docker-in-Docker (DinD) or Docker-outside-of-Docker (DooD). Here's a concise guide for the DooD approach, which is generally recommended:

  1. Modify your Jenkins Docker run command:
docker run -d --name jenkins \
  -p 8080:8080 -p 50000:50000 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts
  1. Install Docker CLI in Jenkins container:
# Access the Jenkins container
docker exec -it jenkins bash

# Install Docker CLI
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

# Add jenkins user to docker group
usermod -aG docker jenkins

# Exit the container
exit

# Restart Jenkins container
docker restart jenkins
  1. In your Jenkins pipeline, you can now use Docker commands:
pipeline {
    agent any
    stages {
        stage('Docker Test') {
            steps {
                sh 'docker --version'
                sh 'docker ps'
            }
        }
    }
}

Remember to grant necessary permissions to the /var/run/docker.sock file on the host system if you encounter permission issues.

@badgeek
Copy link
Author

badgeek commented Jul 23, 2024

install-jenkins:
	@echo "Running Jenkins Docker container..."
	docker run -d --name jenkins \
	  -p 8080:8080 -p 50000:50000 \
	  -v /var/run/docker.sock:/var/run/docker.sock \
	  -v jenkins_home:/var/jenkins_home \
	  jenkins/jenkins:lts

install-docker-cli:
	@echo "Installing Docker CLI in Jenkins container..."
	# Access the Jenkins container
	docker exec -it -u root jenkins bash -c "\
	  curl -fsSL https://get.docker.com -o get-docker.sh && \
	  sh get-docker.sh && \
	  usermod -aG docker jenkins && \
	  exit"

restart-jenkins:
	@echo "Restarting Jenkins container..."
	docker restart jenkins

delete-jenkins:
	@echo "Deleting Jenkins container..."
	docker rm -f jenkins

@badgeek
Copy link
Author

badgeek commented Jul 23, 2024

fix-permission:
	@echo "Fixing permissions for Docker socket..."
	docker exec -it -u root jenkins bash -c "\
	  chmod 666 /var/run/docker.sock && \
	  exit"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment