Docker nginx deployment front-end project

 3 minutes to read

docker install nginx

Execute the following commands:

$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Image is up to date for nginx:latest
docker.io/library/nginx:latest

Pull the latest nginx image file

Run nginx container

$ docker run -d -p 8080:80 --name nginx nginx
949d037cddea664d342a5a138d64dd964e2708d328f8a406b1c7f355d7423320

docker run -d means running in the background

-p 80:80 The first 80 is the port of the host, and the second is the port of the container. The default listening port in the nginx container is port 80

–name nginx represents the name of the container

The last nginx represents mirroring

Then enter http://localhost:8080/ in the browser to access the welcome page of nginx

Enter the nginx container and execute the following command

$ docker exec -it nginx bash

Execute cat /etc/nginx/conf.d/default.conf in the nginx container

# cat /etc/nginx/conf.d/default.conf
server {
    listen 80;
    listen [::]:80;
    server_name localhost;

    #charset koi8-r;
    #access_log /var/log/nginx/host.access.log main;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    # proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    # root html;
    # fastcgi_pass 127.0.0.1:9000;
    # fastcgi_index index.php;
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    # include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    # deny all;
    #}
}

You can see that nginx monitors port 80.

$ docker cp nginx:/etc/nginx/conf.d/default.conf default.conf

docker cp copies the /etc/nginx/conf.d/default.conf file in the nginx container to the command line execution directory of the host. Then modify the nginx listening port to 8080 on the host, and then copy it to the nginx container.

$ docker cp default.conf nginx:/etc/nginx/conf.d/default.conf

Restart the nginx container

$ docker restart nginx

Later, I found that nginx could not be accessed in the host browser, because the listening port of nginx has changed to 8080. Stop executing the nginx container

$ docker stop nginx
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
949d037cddea nginx "/docker-entrypoint.…" 22 minutes ago Exited (1) About a minute ago nginx

Delete nginx container

$ docker rm 949d037cddea

Run the nginx container, the port of the nginx container becomes 8080.

$ docker run -d -p 8080:8080 --name nginx nginx
949d037cddea664d342a5a138d64dd964e2708d328f8a406b1c7f355d7423320

Re-run the nginx container, and the listening port becomes 80. Copy the files again and restart the nginx container

$ docker cp default.conf nginx:/etc/nginx/conf.d/default.conf

Restart the nginx container

$ docker restart nginx

After that, the nginx container is accessed normally. Copying files back and forth between the host and the container is very troublesome. The docker run -v command can create a mount and mount the path of the host to the path in the container.

$ docker run -d -p 8080:8080 -v /home/user/default.conf:/etc/nginx/conf.d/default.conf
-v /home/user/dist:/usr/share/nginx/html --name nginx nginx

The path after -v is an absolute path

By means of mounting, the front-end project is published to the nginx container.