Home  >  Article  >  Operation and Maintenance  >  How to use Linux for container deployment

How to use Linux for container deployment

WBOY
WBOYOriginal
2023-06-18 14:00:311624browse

With the rise of cloud computing and microservices, containerization has become a very important link in modern software development. As a representative of open source systems, Linux has also become one of the preferred systems for containerized deployment. This article will introduce how to use Linux for container deployment.

1. Install Docker

Docker is one of the most popular containerization solutions and can run on the Linux operating system. Before installing Docker, you need to uninstall the existing Docker version and execute the following command:

sudo apt-get remove docker docker-engine docker.io containerd runc

Then, install Docker:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

After the installation is complete, execute the following command to check whether the installation is successful:

sudo docker run hello-world

If "Hello from Docker!" is output, the installation is successful.

2. Create a Docker image

To use Docker for container deployment, you need to create an image first. An image is a snapshot of the files and configuration a container needs to run. If you need to deploy an application, you need to first write a Dockerfile file, which contains instructions for building the image.

The following is a simple Dockerfile example that can be used to build an image running Apache:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]

Among them, the "FROM" instruction specifies the base image, and the latest version of Ubuntu is used here. Then use the "RUN" command to install Apache, and use the "EXPOSE" command to specify the port the container listens on. Finally, use the "CMD" command to specify the startup command of the image.

After you have the Dockerfile, execute the following command to build the image:

sudo docker build -t my-apache .

Among them, the "-t" parameter specifies the image Name and version number, "." indicates the directory where the Dockerfile file is located.

3. Run the Docker container

After creating the image, you can use Docker to run the container. Execute the following command:

sudo docker run -d -p 8080:80 my-apache

Among them, the "-d" parameter indicates running the container in background mode, and the "-p" parameter specifies Mapping between the host port and the container port, "my-apache" is the name of the previously created image.

After running successfully, you can enter "http://localhost:8080" in the browser to access Apache.

4. Using Docker Compose

Docker Compose is a tool for defining and running multiple Docker containers. You can use it to quickly build multiple containers and set up communication and dependencies between them.

The following is a simple docker-compose.yml example:

version: "3"
services:
db:

image: mysql
environment:
  MYSQL_ROOT_PASSWORD: password

web:

build: .
ports:
  - "8080:80"
depends_on:
  - db

Among them, the "web" service refers to the previously created image and listens to port 8080. The "db" service uses the official image of MySQL and sets the root password. The two services have a dependency specified via the "depends_on" parameter.

Execute the following command to start the service:

sudo docker-compose up -d

Now, you can enter "http://localhost:8080" in the browser to access Apache and MySQL services are also up and running.

Summary

This article introduces how to use Linux for container deployment, including installing Docker, creating images, running containers, and using Docker Compose to build multi-container applications. These technologies are very important for modern software development, and I hope this article can help readers better apply them in practical work.

The above is the detailed content of How to use Linux for container deployment. 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