📜  docker jenkins - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:31.700000             🧑  作者: Mango

Docker Jenkins - Shell/Bash

Introduction

Docker and Jenkins are two essential tools for modern-day software development. While Docker provides containerization and portability of applications, Jenkins offers continuous integration and delivery (CI/CD) capabilities. Together, they form a powerful combination that helps developers streamline their workflow and improve software quality.

In this article, we will explore how to use Docker and Jenkins with Shell/Bash scripting to automate your development process.

Using Docker with Shell/Bash

Shell/Bash is a powerful scripting language that can be used to automate various tasks in a Docker container. Here are some examples of how you can use Shell/Bash scripts with Docker:

  1. Building Docker images: You can use Shell/Bash scripts to automate the process of building Docker images. For instance, you can define the Dockerfile and any necessary configurations in a Shell/Bash script, which can then be executed to build the Docker image.
# Dockerfile
FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
# build.sh
#!/bin/bash

docker build -t myapp .
  1. Running Docker containers: Similarly, Shell/Bash scripts can be used to start, stop, and manage Docker containers.
# start.sh
#!/bin/bash

docker run -d --name myapp -p 3000:3000 myapp
# stop.sh
#!/bin/bash

docker stop myapp
docker rm myapp
Using Jenkins with Shell/Bash

Jenkins provides a user-friendly interface for configuring and running CI/CD pipelines. However, you can also use Shell/Bash scripts to define your workflows, giving you more flexibility and customization options. Here are some ways you can use Shell/Bash with Jenkins:

  1. Defining Jenkins jobs: You can define Jenkins jobs using Shell/Bash scripts, which specify the build process, testing steps, and deployment options. This is useful for complex pipelines that require multiple stages and environments.
# build.sh
#!/bin/bash

docker build -t myapp .
# test.sh
#!/bin/bash

docker run myapp npm test
# deploy.sh
#!/bin/bash

docker tag myapp myrepo/myapp
docker push myrepo/myapp
  1. Using Jenkins plugins: Jenkins offers a wide range of plugins that can be used to extend its functionality. Some of these plugins, such as the TravisCI plugin, allow you to define your pipeline as a YAML file, which can be executed using Shell/Bash scripts.
# Jenkinsfile
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                sh 'docker build -t myapp .'
            }
        }
        
        stage('Test') {
            steps {
                sh 'docker run myapp npm test'
            }
        }
        
        stage('Deploy') {
            steps {
                sh 'docker tag myapp myrepo/myapp'
                sh 'docker push myrepo/myapp'
            }
        }
    }
}
Conclusion

Docker and Jenkins with Shell/Bash scripting provide a powerful platform for automating your development workflow. By using these tools together, you can achieve greater efficiency, reliability, and scalability in your software development process.