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 run a nginx image in docker

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "how to run a nginx image in docker". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to run a nginx image in docker"!

Since I have run images before, and there are too many images running on the server, we first use the following command to stop the container and delete the container. Because it is the test server opened to us by operation and maintenance, we can play as much as we like.

1docker stop $(docker ps-a-Q) / / stop stops all containers

2docker rm $(docker ps-a-Q) / / remove deletes all containers

Next, let's take a look at the images in the entire server, using the docker images command. As shown in the figure, we can see that there are many images, but there are no images named nginx. Mirroring and git version control principles are similar, mirroring uses tag to distinguish between different versions of the image.

Next, let's run a mirror image named nginx. Let's first take a look at the command docker run, and then write the port mapping-p. The next step is to map port 80 of nginx to port 8080 of the server, that is, I enter 8080 in the browser, and he will automatically access port 80 of nginx. Note here that the default port of nginx is 80. Let's remember here and talk about it later. -d starts in the background and returns the containerId of the container. Finally, there is the name of the mirror nginx. We can see a line of string below the line, which is the returned container ID.

Note: if it is the first time to run the image and we do not have the image locally, he will pull the image from the remote image repository.

Next, let's see if the image runs successfully, execute the command docker ps, look at the running containers, and by the way, docker ps-a, check all the containers. The red containerId is the container id. Note that the container Id here is shorter than the container ID returned directly after running. This is actually called "the short ID of the container". The long ID seen earlier is called "the long ID of the container", and the short ID is the first 12 characters of the long ID. They actually represent the same container. And Image is the mirror that the container is running.

Now that we have launched the container, let's take a look at the effect in the browser. The picture below is obviously the welcome page of nginx, which means that we have succeeded.

For example, if we want to change his prompt message and change it to "I am a cute girl", it should be divided into three steps.

one

Create a new index.html file, and note that the file name here must be index.html, because the index.html,index.html in the container we are replacing is its first page.

According to the previous we learned how to add a file, fill in it and view the file, do not understand the move to the first part, Docker series-3.Linux common commands.

The specific operation process is shown in the following figure.

two

Copy our new index.html page into the container. The following represents the location of the container ID and the original index.html in the container.

three

see. Let's refresh the page to see if the page shows "I am a cute girl". The result is yes, perfect.

Let's think about a question: if you stop the container now and then start it again, will the content in it still be "I am a cute girl"?

Without saying much, let's do it right away.

We see that the answer is no, it is still the original interface, which means that our previous modification in the interior of the container is temporary, while the external image image has not been changed, so when we stop the container and start the new container, the new container still uses the external image image, so there is still no interface.

So if we want to see the effect of the modification, we have to modify the external mirror image and let's do it all over again.

Delete a specified image, as long as a short imageID is added.

At this point, I believe you have a deeper understanding of "how to run a nginx image in docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report