The article is described in the following scenarios:
- Get the Nginx Image from Docker Registry (Container Registry)
- Execute the Container and add/change files
- Return to the background process and check the status
- Build the new Image, and
- Finally, upload to the Docker Registry
Use Nginx as a Web Server
-
Get nginx image from Docker Hub
docker pull nginx
-
Run nginx as a container with the name webserver
docker run -d -p 8080:80 --name webserver nginx
Options Description -d --detch, Run container in background -p Publish all exposed ports to the port,
format host-port:container-port--name Assign a name to the container nginx Docker image or ID After execution, enter localhost:8080 in your host browser and see the “Welcome to nginx!” web page.
-
Stop, start and restart the container
docker stop webserver docker start webserver docker restart webserver
-
Make sure that the container is in the execution, then type the following command to enter the shell environment
docker exec -it webserver sh
Options Description -i --interactive, Keep STDIN open even if not attached -t --tty, Allocate a pseudo-TTY The default home page of Nginx is located at /usr/share/nginx/html/index.html; you can use the following command to confirm it.
# cat /usr/share/nginx/html/index.html
Enter exit to leave the terminal and return to PowerShell.
-
Copy the file to replace the default web page with a simple Hello World.
<!DOCTYPE html> <html> <head> <title>Hello World Sample</title> </head> <body> Hello World! </body> </html>
Overwrite the container’s homepage with the cp command.
docker cp ./index.html \ webserver:/usr/share/nginx/html
Enter localhost:8080 in the browser, and you can see that the homepage has been updated.
-
Create a new image
Use docker commit to save the container as a new image.docker commit webserver \ <docker_hub_account> \ /hello_world_nginx:1.0
-
Stop the container and remove it.
docker stop webserver docker rm webserver
-
Run the newly docker image, and test in the browser.
docker run -d -p 8080:80 \ --name webserver \ <docker_hub_account>\ /hello_world_nginx:1.0
-
Login to docker hub and upload the new docker image.
docker login
docker push \ <docker_hub_account>\ /hello_world_nginx:1.0
After completion, you can see the new docker image on the docker hub web page.
No comments:
Post a Comment