Home  >  Article  >  Operation and Maintenance  >  How to use Docker for continuous integration and continuous deployment of containers on Linux?

How to use Docker for continuous integration and continuous deployment of containers on Linux?

WBOY
WBOYOriginal
2023-07-30 23:30:281133browse

How to use Docker for continuous integration and continuous deployment of containers on Linux?

With the continuous development of the software development industry, continuous integration and continuous deployment have become important links in the modern software development process. As a lightweight virtualization technology, Docker provides us with a convenient way to implement containerized application deployment. On Linux systems, we can use Docker to perform continuous integration and continuous deployment of containers. This article will introduce how to use Docker to implement this process.

First, we need to install Docker. The following takes the Ubuntu system as an example:

sudo apt-get update
sudo apt-get install docker-ce

After the installation is completed, you can use the following command to verify whether the Docker installation is successful:

docker version

Next, we need to create a Docker image that will be used to build our application. We can use Dockerfile to define image building rules. The following is a sample Dockerfile:

FROM ubuntu:latest

# 安装所需的软件包
RUN apt-get update && 
    apt-get install -y software-properties-common && 
    add-apt-repository ppa:ondrej/php && 
    apt-get update && 
    apt-get install -y php7.2-cli

# 复制应用程序的代码到镜像中
COPY . /app

# 在容器中设置工作目录
WORKDIR /app

# 安装依赖
RUN composer install

# 定义容器启动时执行的命令
CMD ["php", "index.php"]

Create a file named Dockerfile in a directory containing the application code and copy the above code into it. Then, run the following command in this directory to build the Docker image:

docker build -t myapp .

The above command will package the code in this directory and build it into a Docker image named myapp.

Next, we need to create a script for continuous integration and continuous deployment. Here is an example script:

#!/bin/bash

# 拉取最新的代码
git pull origin master

# 停止并移除现有的容器
docker stop myapp
docker rm myapp

# 构建并运行新的容器
docker build -t myapp .
docker run -d --name myapp myapp

The script first pulls the latest code through Git, then stops and removes the existing container. Next, it rebuilds the image and runs a container named myapp.

Save the above script to a file named deploy.sh, and run the following command to give the script execution permissions:

chmod +x deploy.sh

Finally, we can use a continuous integration tool (such as Jenkins ) to execute the script regularly to achieve the goals of continuous integration and continuous deployment. Using such a tool, we can set up scheduled tasks or automatically execute scripts based on triggers of code submission to achieve automated container deployment processes.

To sum up, we can use Docker to achieve continuous integration and continuous deployment of containers. By creating Docker images and using related scripts, we can easily build and deploy containerized applications. This method is not only convenient and fast, but also improves the reliability and testability of the application. Therefore, using Docker for continuous integration and continuous deployment of containers has become an important practice in the current software development industry.

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

Statement:
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