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

A tutorial on how to deploy a static web page in a Docker container

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

Generally speaking, we need to access the container through the port of the container, so how to set the port mapping of the container?

We set it up with the following command:

Docker run-p ip:hostPort:containerPort [--name] [- I] [- t] Image name [COMMAND] [ARG...] ip: indicates host iphostPort: host port number containerPort: container port number

You can set it in the following ways:

ContainerPort, which specifies the container port number, and the host port is randomly generated.

[root@localhost] # docker run-p 80-- name web_test-I-t 80864d42dd23 hub.c.163.com/library/ubuntu / bin/bash

HostPort:containerPort maps host port and container port

[root@localhost] # docker run-p 8080 80864d42dd23 hub.c.163.com/library/ubuntu 80-- name web_test-I-t 80864d42dd23 hub.c.163.com/library/ubuntu / bin/bash

Ip::containerPort sets the random port to container port of the host

[root@localhost] # docker run-p 0.0.0.0 name web_test-I-t 80864d42dd23 hub.c.163.com/library/ubuntu / bin/bash

Ip:hostPort:containerPort maps the specified port of the specified address to the designated port of the container

[root@localhost] # docker run-p 0.0.0.0 80864d42dd23 hub.c.163.com/library/ubuntu 8080-- name web_test-I-t 80864d42dd23 hub.c.163.com/library/ubuntu / bin/bash

Let's deploy a static web page in the container through nginx, using the following steps

-create an interactive container with 80 mapped ports

-install nginx

-install the text editor vim

-create a static web page

-run nginx

-validate the web page

The example is as follows (if you cannot install nginx for apt-get update after installing ubuntu):

[root@localhost] # docker run-p 80-- name static_test-I-t hub.c.163.com/library/ubuntu / bin/bashroot@25fcbf6e953d:/# apt-get install-y nginxReading package lists... DoneBuilding dependency tree Reading state information... DoneE: Unable to locate package nginxroot@25fcbf6e953d:/# apt-get update Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247kB] Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102kB] * * intermediate logs are omitted. * * Fetched 24.9 MB in 9s (2717 kB/s) Reading package lists... Doneroot@25fcbf6e953d:/# apt-get install-y nginxReading package lists... DoneBuilding dependency tree Reading state information... The Done** installation log is omitted. **

Then install vim:

Root@25fcbf6e953d:/# apt-get install-y vimReading package lists... DoneBuilding dependency tree Reading state information... The DoneThe following additional packages will be installed:** installation log is omitted. **

Then create a directory where the static files are stored, and create a simple html file:

Root@25fcbf6e953d:/# mkdir-p / var/www/htmlroot@25fcbf6e953d:/# root@25fcbf6e953d:/# cd / var/www/html/root@25fcbf6e953d:/var/www/html# vim index.htmlroot@25fcbf6e953d:/var/www/html# ll total 8drwxr-xr-x. 2 root root 53 Mar 13 05:02. / drwxr-xr-x. 3 root root 17 Mar 13 04:50.. /-rw-r--r--. 1 root root 79 Mar 13 05:02 index.html-rw-r--r--. 1 root root 612 Mar 13 04:51 index.nginx-debian.htmlroot@25fcbf6e953d:/var/www/html# cat index.html this is the first docker static fileroot@25fcbf6e953d:/var/www/html#

Find the directory where nginx is installed and change the root value of default to the directory you just created:

Root@25fcbf6e953d:/var/www/html# whereis nginxnginx: / usr/sbin/nginx / etc/nginx/ usr/share/nginxroot@25fcbf6e953d:/var/www/html# cd / etc/nginx/sites-enabled/root@25fcbf6e953d:/etc/nginx/sites-enabled# lltotal 4drwxr-xr-x. 2 root root 20 Mar 13 04:51. / drwxr-xr-x. 6 root root 4096 Mar 13 04:51.. / lrwxrwxrwx. 1 root root 34 Mar 13 04:51 default-> / etc/nginx/sites-available/defaultroot@25fcbf6e953d:/etc/nginx/sites-enabled# vim default

Then start nginx to view the container process:

Root@25fcbf6e953d:/etc/nginx/sites-enabled# nginxroot@25fcbf6e953d:/etc/nginx/sites-enabled# ps-ef UID PID PPID C STIME TTY TIME CMDroot 1 00 04:48? 00:00:00 / bin/bashroot 827 10 05:06? 00:00:00 nginx: master process nginxwww-data 828827 0 05:06? 00:00:00 nginx: worker processwww-data 829827 0 05:06? 00:00:00 nginx: worker processwww-data 830827 0 05:06 ? 00:00:00 nginx: worker processwww-data 831 827 0 05:06? 00:00:00 nginx: worker processwww-data 832 827 0 05:06? 00:00:00 nginx: worker processwww-data 833 827 0 05:06? 00:00:00 nginx: worker processwww-data 834 827 0 05:06? 00:00:00 nginx: worker processwww-data 835 827 0 05:06? 00:00:00 nginx: worker processroot 836 1 0 05:07? 00:00:00 ps -efroot@25fcbf6e953d:/etc/nginx/sites-enabled#

Then press ctrl+p,ctrl+q to exit the interactive container.

Use Docker ps to check the port and operation of the container. You can also view it through the dcoker port image name:

Root@localhost ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES25fcbf6e953d hub.c.163.com/library/ubuntu "/ bin/bash" 18 minutes ago Up 18 minutes 0.0.0.0 minutes 32772-> 80/tcp static_ Testt [root @ localhost ~] # docker port static_test80/tcp-> 0.0.0.0 bin/bash 32772 [root@localhost ~] #

You can see that port 80 of the container is mapped to port 32772 of the annotation.

Access the index.html file created through curl:

[root@localhost ~] # curl http://127.0.0.1:32772/index.html this is the first docker static file [root@localhost ~] #

You can also view container details and ip through the docker inspect container name

Access through the container ip:

[root@localhost ~] # docker inspect static_test other information omits "IPAddress": "172.17.0.5", [root@localhost ~] # curl http://172.17.0.5/index.html this is the first docker static file [root@localhost ~] #

When we stop the container with the docker stop container name, and then start the container with the docker start container name, the nginx inside does not start. So we use the docker exec container name nginx to launch:

[root@localhost ~] # docker stop static_teststatic_ test [root @ localhost ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb4f32bbe4a34 hub.c.163.com/library/ubuntu "/ bin/bash" 41 hours ago Up 41 hours loving_brattaind75a2d8c7822 xingguo/df_test1 "nginx-g'daemon off" 2 days ago Up 2 days 0.0.0.0 days 32770-> 80/tcp df_nginx_web959c0fc5d903 xingguo/commit_test1 " Nginx-g'daemon off "2 days ago Up 2 days 0.0.0.0 days 32769-> 80/tcp nginx_ test [root @ localhost ~] # docker start static_teststatic_ test [root @ localhost ~] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES25fcbf6e953d hub.c.163.com/library/ubuntu" / bin/bash "29 minutes ago Up 2 seconds 0.0.0.0 days ago Up 32773-> 80/tcp static_testb4f32bbe4a34 hub.c.163.com/ Library/ubuntu "/ bin/bash" 41 hours ago Up 41 hours loving_brattaind75a2d8c7822 xingguo/df_test1 "nginx-g'daemon off" 2 days ago Up 2 days 0.0.0.0 daemon off 32770-> 80/tcp df_nginx_web959c0fc5d903 xingguo/commit_test1 "nginx-g'daemon off" 2 days ago Up 2 days 0.0.0.0 days ago Up 32769-> 80/tcp nginx_ test [root @ localhost ~] # docker top static_testUID PID PPID C STIME TTY TIME CMDroot 4719 4702 0 13:17 pts/2 00:00:00 / bin/bash [root@localhost ~] # docker exec static_test nginx [root@localhost ~] # docker top static_testUID PID PPID C STIME TTY TIME CMDroot 4719 4702 0 13:17 pts/2 00:00:00 / bin/bashroot 4800 1 0 13:18 ? 00:00:00 nginx: master process nginxamandab+ 4801 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4802 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4803 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4804 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4805 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4806 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4807 4800 0 13:18? 00:00:00 nginx: worker processamandab+ 4808 4800 0 13:18? 00:00:00 nginx: worker process [root@localhost ~] #

Summary

The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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

Servers

Wechat

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

12
Report