Tutorial on how to deploy static Web pages in a Docker container

Source: Internet
Author: User
Tags docker ps docker run

Steps:
1. Creating an interactive container for mapped ports
docker run -p 80 --name web -i -t daocloud.io/ubuntu /bin/bash
2. Installing Nginx
apt-get install -y nginx
3. Install the Text editor vim
apt-get install -y vim
4. Create a static page

        mkdir -p /var/www/html          cd /var/www/html          vim index.html  

Use i to switch to insert mode

Write the following in index.html:

Save exit

5. Modify Nginx configuration file
vim /etc/nginx/sites-enabled/default
This opens the Nginx configuration file and you will see:
server {
Listen default_server;
Listen [::]:80 default_server Ipv6only=on;

 root /var/www/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } 

This time to modify the root content, modified to the location of our HTML files. Save exit.

To switch to the root directory:

CD/

6. Running Nginx

[Plain] View plain copy
Nginx

You can use PS-EF to see if the Nginx is running.

Using Ctrl+p+q, you can put containers in the background to run.

Use Docker PS to see how the container works.

You can also use the Docker port Web to view port mappings for containers:

80/TCP-0.0.0.0:32768

7. Verifying website Access

[Plain] View plain copy
Curl http://127.0.0.1:32768

You can also access this page in your browser:

[CSharp] View plain copy
http://127.0.0.1:32768

To view the IP address of the container using the Docker inspect Web:

[Plain] View plain copy
"Networksettings": {
"Bridge": "Docker0",
"Gateway": "172.17.42.1",
"Globalipv6address": "",
"Globalipv6prefixlen": 0,
"IPAddress": "172.17.0.1",
"Ipprefixlen": 16,
"Ipv6gateway": "",
"Linklocalipv6address": "Fe80::42:acff:fe11:1",
"Linklocalipv6prefixlen": 64,
"MacAddress": "02:42:ac:11:00:01",
"portmapping": null,
"Ports": {
"80/tcp": [
{
"HostIP": "0.0.0.0",
"Hostport": "32768"
}
]
}
},

You can see "IPAddress": "172.17.0.1", which is the IP address of the container.

Use

[Plain] View plain copy
Curl http://127.17.0.1

You can view it directly.

You can also use the IP address of this container in your browser.

Finally, it is important to note that if you use a command to stop a container:

[Plain] View plain copy
Docker Stop web

Then open the container:

[Plain] View plain copy
Docker Start-i Web

Use this time:

[Plain] View plain copy
Ps-ef

Found Nginx is not started.

Use Curl+p+q to put containers in the background.

Use:

[Plain] View plain copy
Docker exec Web Nginx

Start the Nginx service.

Use:

[Plain] View plain copy
Curl http://172.17.0.1

Found out.

This time we use:

[Plain] View plain copy
Docker Inspect web

The following output results are seen:

[CSharp] View plain copy
"Networksettings": {
"Bridge": "Docker0",
"Gateway": "172.17.42.1",
"Globalipv6address": "",
"Globalipv6prefixlen": 0,
"IPAddress": "172.17.0.2",
"Ipprefixlen": 16,
"Ipv6gateway": "",
"Linklocalipv6address": "Fe80::42:acff:fe11:2",
"Linklocalipv6prefixlen": 64,
"MacAddress": "02:42:ac:11:00:02",
"portmapping": null,
"Ports": {
"80/tcp": [
{
"HostIP": "0.0.0.0",
"Hostport": "32769"
}
]
}
},

Notice that the IP address and port number of the container have changed, and we know that the IP addresses and port numbers of the container after the reboot will change.

Tutorial on how to deploy static Web pages in a Docker container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.