Using Docker to create static website applications (multiple ways)

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websites. This article uses nginx, apache, and tomcat servers to demonstrate the docker static website application settings

First, create a docker file,

The docker files for different servers are different. The following are the docker files for nginx, apache, and tomcat servers. In fact, the main difference is the FROM tag in the project file directory.

Directory structure:

1. Docker file for nginx

FROM nginx
COPY ./www /usr/share/nginx/html/
WORKDIR /usr/share/nginx/html/RUN chown -R daemon:daemon * && chmod -R 755 *
EXPOSE 80

2. Apache's docker file

FROM httpd
COPY ./www/ /usr/local/apache2/htdocs/
WORKDIR /usr/local/apache2/htdocs/RUN chown -R daemon:daemon * && chmod -R 755 *
EXPOSE 80

3. Docker file of tomcat

FROM tomcat
COPY ./www/ /usr/local/tomcat/webapps/ROOT/webapp
WORKDIR /usr/local/tomcat/webapps/ROOT/webapp #Switch to the project directory RUN chown -R daemon:daemon * && chmod -R 755 * #Set permissions

Here we create a project named webapp. When accessing it, add /webapp after the URL address. Also note that the default port of Tomcat is 8080.

The three files are used to deploy your website in different environments. You can set permissions according to the situation or not.

Second, build the project

 docker build -t imageName . (Note the dot at the end. It is best if imageName is the "hub account name/project name" to facilitate pushing to the public library)

The following shows the build process

3. Push to public warehouse

You need to execute docker login first

docker push ejiyuan/webapp

Fourth, pull the project on the server

You need to execute docker login first

docker pull ejiyuan/webapp

Execute docker images to check whether the image exists

5. Execution of the Project

docker run -d -p 8081:80 ejiyuan/webapp

VI. Verification

Directly access the host IP address plus the port number 8081 given at startup or use curl

curl http://192.168.99.100:8081

7. Questions

Here we take nginx as an example. If your project's default page is not index.html, 401 or the following page will appear:

The main reason is that the default page is not specified. Use the following command to log in to the image

docker exec -it containerId /bin/bash

The containerId can be obtained using docker ps, modify /etc/nginx/conf.d/default.cnf, if there is no vim or vi in ​​the container, install it with the following two commands

apt update
apt install vim

Or use sed command

sed -i '10c index default.html;' /etc/nginx/conf.d/default.conf

Modifications require reloading

nginx -t # Check if the configuration file is correct service nginx reload # Newly load the configuration file

If the service does not exist, exit the container using exit and restart the container using docker restart containerId

8. Contrast

Run docker images and see the results yourself. Nginx is the smallest, Apache is the second, and tomcat is the largest.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation of static website layout in docker container

<<:  mysql5.7.17.msi installation graphic tutorial

>>:  Example of automatic import method of vue3.0 common components

Recommend

Detailed explanation of using grep command in Linux

Linux grep command The Linux grep command is used...

The process of building and configuring the Git environment in Docker

Configure Git environment in Docker At work, I en...

TortoiseSvn Little Turtle Installation Latest Detailed Graphics Tutorial

There were always problems when installing tortoi...

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

Change the MySQL database engine to InnoDB

PS: I use PHPStudy2016 here 1. Stop MySQL during ...

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...

js to implement add and delete table operations

This article example shares the specific code of ...

Detailed explanation of Vite's new experience

What is Vite? (It’s a new toy on the front end) V...

Install Python virtual environment in Ubuntu 18.04

For reference only for Python developers using Ub...

Use CSS to implement special logos or graphics

1. Introduction Since pictures take up a lot of s...

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...

Analysis and solution of a MySQL slow log monitoring false alarm problem

Previously, for various reasons, some alarms were...