Home > Operation and Maintenance > Linux Operation and Maintenance > How to deploy and manage containerized Linux systems

How to deploy and manage containerized Linux systems

王林
Release: 2023-11-07 16:12:30
Original
1333 people have browsed it

How to deploy and manage containerized Linux systems

How to deploy and manage containerized Linux systems

With the rise of cloud computing and microservice architecture, containerization technology has become an important part of software development and deployment tool. Containerized deployment and management solutions on Linux systems, such as Docker and Kubernetes, have been widely adopted. This article will introduce how to use Docker for container deployment and management, and give specific code examples.

  1. Installing Docker

First, you need to install Docker on the Linux system. You can install Docker through the following command:

$ sudo apt-get update
$ sudo apt-get install docker.io
Copy after login

After the installation is complete, you can use the following command to verify whether Docker is installed successfully:

$ docker --version
Copy after login
  1. Create a Docker image

Docker image is a template used to create Docker containers. The content and configuration of the image can be defined through Dockerfile. The following is a simple Dockerfile example:

# 使用基础镜像
FROM ubuntu:16.04

# 安装所需软件包
RUN apt-get update && apt-get install -y 
    nginx 
    php7.2 
    mysql-server

# 复制配置文件到镜像中
COPY nginx.conf /etc/nginx/nginx.conf
COPY index.php /var/www/html/index.php

# 容器启动时运行的命令
CMD ["nginx", "-g", "daemon off;"]
Copy after login

In the above example, a base image ubuntu:16.04 is first used, then several software packages are installed, and the configuration files and applications are copied. The program code finally sets the command to run when the container starts.

  1. Building and running containers

The command to build the image is docker build, which is done by specifying the directory and Dockerfile path. For example, run the following command in the current directory:

$ docker build -t myapp .
Copy after login

The above command will build an image named myapp.

Next, you can use the following command to run the image to create the container:

$ docker run -d -p 80:80 myapp
Copy after login

In the above command, the parameter -d means running in background mode, -p 80 :80 means mapping port 80 of the host to port 80 of the container.

  1. Container Management

Manage containers through Docker commands. Here are some commonly used command examples:

  • View all running containers:
$ docker ps
Copy after login
  • View all containers (including stopped ones):
$ docker ps -a
Copy after login
  • Stop a container:
$ docker stop <容器ID>
Copy after login
  • Start a container:
$ docker start <容器ID>
Copy after login
  • Enter the shell environment of a container :
$ docker exec -it <容器ID> /bin/bash
Copy after login
  • Delete a container:
$ docker rm <容器ID>
Copy after login
  • View container log:
$ docker logs <容器ID>
Copy after login

The above are just some common ones Command examples, Docker also provides many other functions and commands that can be learned and used according to specific needs.

Summary:

This article introduces how to use Docker for containerized deployment and management of Linux systems, and gives specific code examples. By learning and mastering this knowledge, software development and deployment can be carried out more effectively, and work efficiency and system stability can be improved. Hope this article is helpful to readers.

The above is the detailed content of How to deploy and manage containerized Linux systems. 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