Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use docker to install web services provided by nginx

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use docker to install web services provided by nginx, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will get something after reading this article on how to use docker to install web services provided by nginx. Let's take a look.

1. Pull the image

The docker pull command is used to pull the application image, and the docker pull nginx command is used to pull the latest version of nginx image. The following is the response result of the pull image process:

# docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxc229119241af: Pull complete 2215908dc0a2: Pull complete 08c3cb2073f1: Pull complete 18f38162c0ce: Pull complete 10e2168f148a: Pull complete c4ffe9532b5f: Pull complete Digest: sha256:2275af0f20d71b293916f1958f8497f987b8d8fd8113df54635f2a5915002bf1Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latest

From the above, we can see that the nginx image is pulled from the URL docker.io.

Use the docker images command to see which image files are downloaded in the current operating system.

# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 12766a6745ee 33 hours ago 142MBhello-world latest feb5d9fea6a5 6 months ago 13.3kB

REPOSITORY image repository and image name. If the image repository is not displayed, the default is docker.io.

The version of the TAG image or the milestone label. Latest indicates the latest version.

Unique identification of the IMAGE ID mirror

The time when the CREATED image was created

SIZE indicates the size of the image file

2. Run the image startup container

Start a container with the name nginx-zimug through the docker run command.

# docker run-d-name nginx-zimug-p 80:80 nginx81bb1211144bc0991a789f860458548643c60b5459466c14d0604be9a4ccbfd7

-d indicates that the container is running in the background

-- name gives the container a name

-p port mapping, in the format of host port: container port, which means to map port 80 in the container to port 80 of the host to provide external access services. The last field is the mirror name

The browser HTTP protocol accesses port 80 of the host, which can be omitted if it is port 80. The access results obtained are as follows, indicating that our nginx service has been started successfully.

You can view the running containers through docker ps, as shown below:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES81bb1211144b nginx "/ docker-entrypoint." 11 minutes ago Up 11 minutes 0.0.0.0 80/tcp-> 80/tcp,: 80-> 80/tcp nginx-zimug 3. File mapping

First of all, it is clear that the contents of the files in the container can be modified, but once the container is restarted, all changes written to the container, data files and configuration files will be lost. So in order to save the running state of the container and execute the results, we need to map some important data files, log files, and configuration files in the container to the host.

Taking nginx as an example, nginx has three important file directories:

The directory / usr/share/nginx/html/root/nginx/htmlnginx configuration file directory / etc/nginx/nginx.conf/root/nginx/conf/nginx.conf log directory / var/log/nginx/root/nginx/logs where the custom mapping path in the host in the container stores web pages

Create a new file directory in the host

Mkdir-p / root/nginx/logs / root/nginx/html / root/nginx/conf

Copy the files in the container to the host

Copy the nginx configuration file to the host

Docker cp nginx-zimug:/etc/nginx/nginx.conf / root/nginx/conf

Put a simulated html file into the html directory

Save the following file as index.html to the host's / root/nginx/html directory, which is actually placed in the container's / usr/share/nginx/html directory because of the mapping relationship.

Use docker to build nginx web service access successfully

Great!

Start the container service again

The-v parameter expresses the mapping relationship between the host file and the file in the container. The format is-v host directory: container file system directory. Start a new container with the name nginx-prod

Docker run-d-p 80:80\-- name nginx-prod\-v / root/nginx/html:/usr/share/nginx/html\-v / root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf\-v / root/nginx/logs:/var/log/nginx nginx

Delete the old nginx-zimug container before starting the new container. If you do not delete the old container, the port of the new container conflicts with the port of the old container. Delete the container using the following command:

Docker stop nginx-zimug;docker rm nginx-zimug

Execute the docker run command above to start the new container. After startup, access port 80 of the host through a browser, and the response result shows that nginx provides web services normally.

At the same time, you can modify the nginx configuration on the host machine, or you can view the runtime log files. The modification result will affect the operation of the container nginx service, because there is a mapping relationship between the configuration file of the host and the configuration file in the container.

This is the end of the article on "how to use docker to install web services provided by nginx". Thank you for reading! I believe that everyone has a certain understanding of "how to use docker to install web services provided by nginx". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report