Home Operation and Maintenance Linux Operation and Maintenance How to use Docker for automated deployment and rollback of containers

How to use Docker for automated deployment and rollback of containers

Nov 07, 2023 pm 01:40 PM
docker rollback Automated deployment

How to use Docker for automated deployment and rollback of containers

With the popularity of cloud computing and microservices, Docker, as a lightweight virtualization technology, is widely used in container deployment. Docker has the advantages of rapid deployment, resource isolation, and easy management, which can greatly simplify the application maintenance and release process. This article will introduce how to use Docker for automated deployment and rollback of containers, and provide some specific code examples.

1. Docker automated deployment

Docker automated deployment refers to packaging the application environment and related dependencies into Docker by writing Dockerfile files and Docker Compose files during the application development and testing process. Mirror and automatically deploy to local or remote servers.

1. Write a Dockerfile

Dockerfile is a text file that contains all the instructions and commands required by the application to build the image. You can write a Dockerfile file to define the environment, dependencies, and startup commands required for the application. You only need to execute a single command to automatically build the Docker image required for the application.

The following is an example of using a Dockerfile file to build a Node.js application:

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Copy after login

The above Dockerfile file specifies the base image as the alpine base image of Node.js 14 version, and defines the working directory as /app, copy package.json and package-lock.json to the working directory, execute npm install to install dependencies, copy the application code to the working directory, define the port to be mapped by the container as 3000, and finally execute npm start to start the application.

2. Write Docker Compose files

Docker Compose is a tool officially provided by Docker for defining and running applications composed of multiple containers. By writing a Docker Compose file, you can define the services required by the application, the image and startup method of the container, the network and dependencies between containers, and other information, as well as the resource limits and expansion of the container.

The following is an example of using Docker Compose to deploy a Node.js application:

version: '3'

services:
  app:
    build: .
    ports:
      - '8080:3000'
    environment:
      NODE_ENV: production
    restart: always
Copy after login

A service named app is defined in the above Docker Compose file, use the build command to build the image, and set the container port Map to host port 8080, define the environment variable NODE_ENV as production, and specify that the container will automatically restart after it goes down.

3. Automated deployment scripts

In order to make the application deployment process more automated, you can write some deployment scripts to realize the entire process of automatically building, pushing and deploying Docker images. The following is an example of using Docker Compose to automatically deploy a Node.js application:

#!/bin/bash

# 定义应用名称和版本号
APP_NAME='nodejs-app'
TAG=$(git rev-parse --short HEAD)

# 构建Docker镜像
docker build -t $APP_NAME:$TAG .

# 推送Docker镜像到仓库
docker push $APP_NAME:$TAG

# 使用Docker Compose部署应用
docker-compose up -d
Copy after login

In the above script, use the git rev-parse command to obtain the version number of the latest code, build the image and push it to the Docker warehouse, and finally use Docker Compose starts the application (the -d parameter indicates running in the background).

2. Docker container rollback

During the application development and testing process, some code errors or incompatibility issues will inevitably occur. At this time, we need to roll back the application version and restore the application to its previous state.

Docker containers provide a simple rollback method, which is to specify the version number through tags. You can select different image versions when the container is started to implement application version rollback.

1. Use tag (Tag)

When specifying the version number in the Dockerfile file, you can use the tag (Tag) to identify the application version. For example:

FROM node:14-alpine

LABEL version="1.0.0"

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Copy after login

The LABEL instruction is used in the above Dockerfile to identify the application version number as 1.0.0.

After building the image, you can use the docker tag command to create a new tag, for example:

docker tag nodejs-app:latest nodejs-app:1.0.0
Copy after login

When defining the service in the Docker Compose file, you can specify the tag (Tag) that needs to be used, For example:

version: '3'

services:
  app:
    image: nodejs-app:1.0.0
    ports:
      - '8080:3000'
    environment:
      NODE_ENV: production
    restart: always
Copy after login

2. Rollback script

In order to facilitate version rollback, you can write a rollback script to implement the rollback of the application version by specifying the image version that needs to be used. The following is an example of using Docker Compose to roll back a Node.js application:

#!/bin/bash

# 定义应用名称和版本号
APP_NAME='nodejs-app'
TAG='1.0.0'

# 更新Docker Compose文件中使用的镜像版本
sed -i "s|$APP_NAME:.*|$APP_NAME:$TAG|g" docker-compose.yml

# 回滚应用版本
docker-compose up -d
Copy after login

In the above script, directly modify the image version number used in the Docker Compose file and start the container.

3. Summary

This article introduces how to use Docker for automated deployment and rollback of containers, and provides some specific code examples. Through automated deployment, we can quickly deploy and update applications, and roll back versions when necessary, improving application stability and reliability. However, in actual applications, deployment strategies need to be optimized based on specific business needs and environments to ensure the normal operation of the application.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

See all articles