docker+nginx installation and deployment modify resource directory configuration files and container port information

View the docker image

You can first check whether there is an nginx image under docker, and use the following commands to check:

  • docker images: List all images.
  • docker images nginx: List all nginx images, different versions, etc.
  • docker search nginx: Search and view all nginx image information.

Pull and install nginx image

Use the pull command to pull the nginx image:

docker pull nginx

The following content appears, just wait for the download to complete:

run nginx

After downloading the nginx image, you can start and run nginx, use the following command:

docker run -d --name nginx -p 8080:80 nginx

Some descriptions are as follows:

  • -d runs continuously in the background.
  • --name Custom container name.
  • -p maps the host port number to the port number of the docker container.
    Here port 8008 is the port number for external web access, and 80 is the port number exposed by the nginx container.

If the startup is successful, you can  ip:8008 access the site by.

The above is the success of docker installation and starting nginx container service.

Very simple processing, the next thing we have to do is how to deal with nginx configuration, logs, static resources, etc. These are relatively complicated. There are various ways to handle configuration resource files etc., which are described in detail below.

map to a local directory

View the configuration resource directory of the nginx container

  1. Enter the container, you can execute the command
docker exec -it container-id/container-name bash
docker exec -it container-id/container-name /bin/bash
// -i: 以交互模式运行
// -t: 为容器重新分配一个伪输入终端
// -i 与 -t 通常同时使用
// container-id     容器id
// container-name   自定义容器名称
// bash (/bin/bash) linux 命令,启动一个bash终端,可与用户交互
  1. After entering the container bash terminal, you can view the directory location of nginx-related content as follows:

log Log file directory: /var/log/nginx
config configuration file directory: /etc/nginx
web resource storage directory: /usr/share/nginx/html

Note that you can view the corresponding file information at this time, but you cannot use vi to view the file content here, because it is a new terminal command environment, vi cannot be used.
You can use  cat commands like:

cat nginx.conf

In addition, use the exit command to exit the current terminal command environment of the container.

Map nginx directory

-v To map the configuration log resource and other directories of the nginx container to the local host directory, you need to use commands when starting the container,  such as:

docker run -d --name nginx \
-p 8080:80 \
-v /usr/nginx/html:/usr/share/nginx/html \
-v /usr/nginx/log:/var/log/nginx \
-v /usr/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx

Among them  -v is the directory mount, which maps the local directory to the container directory, and then directly modifies the contents of the local directory, which will be synchronized to the container.
The above command mounts the resource file directory, log directory, and configuration file of nginx.
At this time, if you look at the configuration file nginx.conf, you can find that there is such a paragraph:

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

This is because the nginx container loads sub-configuration files in the conf.d directory in addition to the main configuration file nginx.conf, usually at least one default.conf. Therefore, when starting the container, you can also mount the directory:

-v /usr/nginx/conf.d:/etc/nginx/conf.d

You can also only use nginx.conf and modify the configuration without using the sub-configuration of conf.d, but the docker container uses self-configuration by default.

After the content resource is mounted, the static web file can be uploaded to the corresponding host directory.
You can also directly modify the configuration file in the host directory, and then restart nginx to take effect.

docker stop nginx
docker start nginx

Copy container resources

Another way to change the contents of configuration resources is to use  docker cp commands.

docker cp: used to copy data between the container and the host

Through this command, you can copy the local content to the container. For example, the following command can copy the html file to the static resource directory of the nginx container:

docker cp /usr/nginx/html/mian.html nginx:/usr/share/nginx/html

You can also copy configuration files:

// 从容器拷贝到本地目录
docker cp nginx:/etc/nginx/nginx.conf /usr/nginx/nginx.conf
// 从本地目录拷贝到容器
docker cp /usr/nginx/nginx.conf nginx:/etc/nginx/nginx.conf

By copying, you can change static resource files, modify configuration files, view logs, etc.

port problem

Note:  When starting nginx 
through  the command,  the port number of the nginx container selected by default is 80 by setting the port. If the port number of the nginx container is modified through the configuration file, such as changing it to   ,  restart the nginx container, at this time, the nginx service will be invalid and the site cannot be accessed effectively. That is, containers other than port 80 cannot be accessed.docker run-p
server 80server 8080

If you want to deploy multiple websites, you can start multiple containers, and each container can correspond to a port number. At this time, you can modify the log configuration resource file and other content of each container.

How to modify the port of a running container

  • Stop the nginx container from running,docker stop nginx

  • View the id of the nginx container, docker inspect nginx | grep "Id", will display the complete container Id

  • The general path /var/lib/docker/containers/{container Id}/hostconfig.json contains container ports and local ports, which can be modified, and can be viewed and modified by commands  vi :


    80/tcp It is the container port, HostPort: 8005 which is the local port and can be modified.

  • restart docker, systemctl restart docker

  • Restart the nginx container, docker start nginx

Guess you like

Origin blog.csdn.net/jh035/article/details/128128021