First enter the command line of the host terminal and start docker service docker start to view the list of docker images installed






docker pull nginx






docker run -d -p 80:80 nginx



-d



-p
xx.xx.xx.xx:8080



nginx

If you want to change the configuration file of nginx, or change a static page, we need to change the inside of the container

Docker ps-a docker ps-a docker ps-a docker ps-a docker ps-a



docker rm xxxxx

Pull so much useless, now on the point, if to change the nginx configuration files in the container, specific paths and contents of the log file, it will be used to mount, I understand the meaning of the mount is in using the nginx container, not to use the container configuration and file paths, using resources on host file, right Sudo docker exec it XXXX bash XXXX is a container id

Is not a bit familiar in fact this mirror their own world, not bothered by the outside world, but the function specified no host machine all ah, such as you want to use vim command…. inside

If you want to mount nginx, you need to know where the nginx configuration files in your image are, and then mount them to the specified location on your host The path of the default. The conf configuration file/etc/nginx/conf. D/default. Conf default home page HTML folder path/usr/share/nginx/HTML/var/log/nginx log file path

exit

Conf /nginx/{conf,conf. D, HTML,logs} /nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx/ nginx

Log/access.log/access.log/access.log/access.log/access.log/access.log/access.log In HTML you put the.html file that the home page needs to display, and then when you visit, the HTML you add replaces the default Nginx welcome screen… It doesn’t work now. You haven’t mounted it yet. Later.

Put the nginx.conf configuration file in the conf file, So the contents of this file have to be copied from the image, so let’s try to keep it the same as the configuration file in the image. Now let’s talk about how to copy the configuration file that we just saw in the container into the host The container id: / etc/nginx/nginx. Conf/nginx/conf/nginx. Conf


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}

Copy the code

In the same conf. D under default. Conf copied docker cp container id: / etc/nginx/conf. D/default. Conf/nginx/conf. D/default. Conf

server {
    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;
    #}
}


Copy the code

Then, we’ll write a simple HTML page of our own and give it a random name, which I won’t change, called index.html

Then drop the file into the /nginx/ HTML/directory of the host. I used FileZilla to transfer the file to the host

Now comes the most important part

Docker rm container id OMG we have to close it before deleting it, docker stop container ID

Once you’ve done that, start again. The difference this time is that you need to mount it using -v

docker run --name mynginx -d -p 80:80 -v /nginx/html:/usr/share/nginx/html -v /nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nginx/logs:/var/log/nginx nginx
Copy the code

The nginx container path follows the host path

I am also a beginner of Java and Docker. Please advise me if there is any problem, mainly to keep a record for myself