Home > Operation and Maintenance > Linux Operation and Maintenance > How to deploy containerized applications on Linux

How to deploy containerized applications on Linux

WBOY
Release: 2023-07-07 22:53:05
Original
1532 people have browsed it

How to deploy containerized applications on Linux

Introduction:
With the rapid development of cloud computing and container technology, containerized applications have become the first choice for many development teams. Containerized applications are lightweight, quick to deploy, and easy to manage, allowing developers to better utilize system resources and improve application portability and scalability. In this article, we will introduce how to deploy containerized applications on Linux.

1. Install Docker
Before we begin, we need to install Docker first. Docker is an open source container engine that helps us create and manage containers. The following are the steps to install Docker on an Ubuntu system:

  1. Update the operating system’s package list:

    sudo apt-get update
    Copy after login
  2. Install Docker’s dependency packages:

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
    Copy after login
  3. Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    Copy after login
  4. Add Docker’s software source:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    Copy after login
  5. Update the software package list and install Docker:

    sudo apt-get update
    sudo apt-get install docker-ce
    Copy after login
  6. Verify whether Docker is successfully installed:

    sudo docker run hello-world
    Copy after login

    If the run is successful, a prompt message will be output.

2. Writing Dockerfile
Dockerfile is a plain text file that contains a series of instructions for automatically building a Docker image. The following is a simple Dockerfile example:

# 基于Ubuntu镜像
FROM ubuntu:latest

# 安装应用的依赖包
RUN apt-get update && apt-get install -y 
    python3 
    python3-pip

# 复制应用代码到镜像中
COPY app /app

# 设置工作目录
WORKDIR /app

# 安装应用的Python依赖包
RUN pip3 install -r requirements.txt

# 暴露应用的端口
EXPOSE 5000

# 启动应用
CMD [ "python3", "app.py" ]
Copy after login

In the above example, we specify the required operating system base image, install dependency packages, copy application code, set the working directory, and install application dependency packages through Dockerfile. Steps such as exposing the application's port and starting the application.

3. Build the Docker image
After writing the Dockerfile, we need to use the Docker command to build the Docker image. Execute the following command on the command line:

sudo docker build -t myapp:latest .
Copy after login

Among them, the -t parameter is used to specify the name and version number of the image, and . represents the current directory where the Dockerfile is located. The build process may take some time, if all goes well we will get a Docker image named myapp:latest.

4. Run the Docker container
After the Docker image is built, we can use the Docker command to run the Docker container. Execute the following command in the command line:

sudo docker run -d -p 5000:5000 myapp:latest
Copy after login

Among them, the -d parameter indicates running the container in the background, and the -p parameter is used to specify the communication between the container and the host. Port Mapping. The above command will start a container named myapp on the host's port 5000.

5. Manage Docker containers
When deploying containerized applications, we usually need to perform some management operations on the containers. Here are some commonly used Docker commands:

  • View running containers:

    sudo docker ps
    Copy after login
  • Stop a container:

    sudo docker stop <container ID>
    Copy after login
  • Start container:

    sudo docker start <container ID>
    Copy after login
  • Delete container:

    sudo docker rm <container ID>
    Copy after login
  • View container log:

    sudo docker logs <container ID>
    Copy after login
  • 6. Summary
    This article introduces how to deploy containerized applications on Linux. First, we installed Docker and learned its basic concepts and installation steps. Then, we wrote a simple Dockerfile to build the Docker image. Next, we built the Docker image using the Docker command and started a container by running the command. Finally, we introduce some commonly used Docker commands for container management and maintenance.

    Containerized applications have become one of the standard methods for modern application development and deployment. With the help of container technology, developers can develop, test and release applications more flexibly. I hope this article will help you understand and master the deployment of containerized applications on Linux.

    The above is the detailed content of How to deploy containerized applications on Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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