Home > Operation and Maintenance > Linux Operation and Maintenance > How to use Docker for continuous integration and continuous deployment

How to use Docker for continuous integration and continuous deployment

WBOY
Release: 2023-11-08 13:28:55
Original
1566 people have browsed it

How to use Docker for continuous integration and continuous deployment

How to use Docker for continuous integration and continuous deployment

With the rapid development of software development, continuous integration and continuous deployment have become indispensable in the modern software development process a part of. As a containerization platform, Docker can greatly simplify the process of continuous integration and continuous deployment. This article will introduce how to use Docker for continuous integration and continuous deployment, and provide specific code examples.

1. Continuous Integration

Continuous integration refers to frequently merging developers’ code modifications into a shared repository, and frequently building and testing them. Using Docker for continuous integration can simplify the environment configuration and build process and improve development efficiency.

  1. Create a Dockerfile

Dockerfile is a script file used to build a Docker image. Create a file named Dockerfile in the project root directory and add the following code example:

# 使用官方的 Node.js 镜像作为基础镜像
FROM node:alpine

# 设置工作目录
WORKDIR /app

# 复制 package.json 和 package-lock.json 到工作目录
COPY package*.json ./

# 安装项目依赖
RUN npm install

# 将项目文件复制到工作目录
COPY . .

# 暴露端口
EXPOSE 3000

# 运行应用
CMD ["npm", "start"]
Copy after login

This Dockerfile file defines a Node.js-based image and installs the project's dependencies into the image, and Copy the application's files and code into the working directory. Finally, expose the port and run the application.

  1. Build Docker image

In the project root directory, use the following command to build the Docker image:

docker build -t my-app .
Copy after login

This command will be based on the definition of the Dockerfile file, Build an image named my-app.

  1. Containerized testing

Create a file named docker-compose.test.yml in the project root directory and add the following code example:

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - db
    command: npm run test
  db:
    image: mongo
Copy after login

This docker-compose.test.yml file defines two services, one is the app service, which is our application service, and the other is the db service, which is our database service. This file instructs Docker to run two services and run test commands in the app service.

In the project root directory, use the following command to run the test container:

docker-compose -f docker-compose.test.yml up
Copy after login

This command will start the app and db services and run the test command.

  1. Automated Continuous Integration

The purpose of continuous integration is to quickly and frequently merge developers' code changes into the main code and perform automated builds and tests. You can use tools such as Jenkins and GitLab CI to implement automated continuous integration.

Take Jenkins as an example, create a file named Jenkinsfile, and add the following code example:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'docker build -t my-app .'
            }
        }
        stage('Test') {
            steps {
                sh 'docker-compose -f docker-compose.test.yml up'
            }
        }
    }
}
Copy after login

This Jenkinsfile file defines a Jenkins pipeline that contains two stages: build and test. In the build phase, execute the docker build command to build the Docker image, and in the test phase execute the docker-compose command to start the test container.

Add the Jenkinsfile file to the root directory of the project and configure the Jenkins server for automated continuous integration.

2. Continuous deployment

Continuous deployment refers to automatically deploying code to the production environment after completing continuous integration. Using Docker for continuous deployment can greatly simplify the deployment process.

  1. Create a Docker Image

Using the Dockerfile created in the previous steps, build a Docker image that contains the application code.

  1. Write the docker-compose.yml file

Create a file named docker-compose.yml in the project root directory and add the following code example:

version: '3'
services:
  app:
    image: my-app:latest
    restart: always
    ports:
      - 80:3000
Copy after login

This docker-compose.yml file instructs Docker to run an app service and use the my-app image just built as its base image. In addition, port mapping and other services can be configured.

  1. Deploy the application

Use the following command to deploy the application in the production environment:

docker-compose up -d
Copy after login

This command will start the app service in the background and expose it On port 80 of the host.

The above are the specific steps and code examples on how to use Docker for continuous integration and continuous deployment. Through Docker, you can simplify the environment configuration and deployment process, improve development efficiency and application reliability.

The above is the detailed content of How to use Docker for continuous integration and continuous deployment. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template