Home  >  Article  >  Operation and Maintenance  >  How to deploy Docker in production

How to deploy Docker in production

PHPz
PHPzOriginal
2023-04-18 09:48:25994browse

As the complexity of software development and deployment continues to increase, lightweight containerization technology has become a new trend. Docker is one of the most popular container technologies currently. It can provide developers with a more efficient and faster way of working, while also providing enterprises with more flexible and reliable deployment solutions. This article will introduce how to deploy Docker in a production environment to help you better manage containerized applications.

  1. Installing Docker

Before installing Docker, we need to manage the resource allocation and security permissions of the server. It is recommended to use mainstream Linux operating systems such as Ubuntu and CentOS, and ensure that the latest version of Docker Engine is installed on the server.

Installing Docker is very simple and only requires a few steps:

1) Install dependent tools: apt-get update && apt-get install -y apt-transport-https ca-certificates curl software -properties-common.

2) Import Docker’s official GPG key: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -.

3) Add Docker repository: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable".

4) Install Docker: apt-get update && apt-get install -y docker-ce.

After the installation is complete, you can use the docker command to test whether Docker is running normally. For example, run the docker version command to check the Docker version information.

  1. Configuring Docker

After installing Docker, we need to do some basic configuration on it to ensure that it can work better for us.

The current mainstream Docker CLI uses Unix domain sockets for communication. By default, adding the user running the docker command to the docker user group will have the ability to interact with the Docker daemon, thereby avoiding the use of the sudo command.

When configuring Docker, you also need to pay attention to the following aspects:

1) Update the Docker configuration file

Docker’s daemon will read /etc/docker/ by default daemon.json file to obtain configuration information. Open the file and add the required configuration, such as:

{
"registry-mirrors": ["https://url-of-mirror"],
"max-concurrent-downloads" : 5,
"debug": true,
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://192.168 .0.0:111",
"tag": "prod"
}
}

In it, registry-mirrors are used to configure faster Docker mirror accelerator sources, max- concurrent-downloads is used to specify the maximum number of images to be downloaded at the same time, and debug and log-driver are used to enable Docker's debug log and output it to the syslog log file.

2) Set the Docker storage directory

Docker is saved in the /var/lib/docker directory by default. You can better manage the Docker file system storage and storage by modifying the Docker storage path. data volume. Open the daemon configuration file using redirection and add the following line:

{
"data-root": "/mnt/data/docker"
}

/data The /docker/ directory needs to be created manually first, and the appropriate storage path must be selected based on the actual deployment situation.

  1. Build a Docker image

In Docker, you can use a Dockerfile file to define the environment of the application and how it is deployed. A Dockerfile is a text file that consists of a series of instructions that specify how to build a Docker image of an application to be deployed.

In the process of writing the Dockerfile file, you can use FROM, RUN, COPY, EXPOSE, ENV, CMD and other instructions to build a complete Docker image:

FROM ubuntu:18.04
RUN mkdir /app
COPY . /app
WORKDIR /app
CMD python app.py

The above Dockerfile script uses Ubuntu 18.04 as the base image, creates the /app directory, and adds the local code Copy to the /app directory, finally set the working directory to /app, and then run the python app.py script.

Use the docker build command to build the Docker image, as follows:

docker build -t myapp:latest .

It is recommended to use the version tag to declare the version number of the Dockerfile, for example: FROM ubuntu :18.04 AS build.

  1. Publish the Docker image

After building the Docker image, you need to publish it to the image warehouse to prepare the application for deployment anywhere. Docker Hub is a public Docker image repository, while private Docker registries can be used to store private Docker images.

Pushing the Docker image to the image warehouse requires authentication. The specific method is as follows:

1) Initialize the Docker login console: docker login registry-name.

2) Enter the username and password used in Docker Hub or private docker registry.

3) Publish the image: docker push registry-name/myapp:latest.

Now, we have successfully pushed the Docker image to the Docker registry for use elsewhere.

  1. Deploy a Docker container

When using a Docker container to run a Docker image, you can use the following command:

docker run --name myapp -p 127.0. 0.1:80:80 -d myapp:latest

Among them, the --name parameter specifies the name of the Docker container, the -p parameter specifies the host port to which the container will be bound, the -d parameter indicates that the container is running in the background, and myapp:latest is the Docker image just pushed to the Docker registry.

  1. Manage Docker Containers

After an application is deployed into a Docker container, it needs to be managed. You can use docker ps, docker logs, docker stop, docker rm and other commands to manage Docker containers.

The specific operation method is as follows:

1) View the currently running Docker container: docker ps -a.

2) View the logs of the specified Docker container: docker logs myapp.

3) Stop the specified Docker container: docker stop myapp.

4) Delete the specified Docker container: docker rm myapp.

  1. Conclusion

Docker deployment is a critical task in a production environment. After installing Docker and configuring its basic operation, you need to build the Docker image and publish it to the Docker registry. Finally, use Docker containers to run the application, manage and monitor it. The above is a detailed introduction in this article on how to deploy Docker in a production environment. I hope it will be helpful to you.

The above is the detailed content of How to deploy Docker in production. 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