Using docker to install nginx and vsftpd to build file server

Time:2020-11-27

Seriously, I will never learn docker in my life!

It is recommended to refer to the official website tutorial for the installation method of docker in centos7. Otherwise, docker installation of nginx may be wrong.

To install docker, you can directly refer to the installation process in my brief book
https://www.jianshu.com/p/3b2…

<!–more–>

  • We learned a lot from the process of installing nginx with docker, such as copying the files in the docker container to the host computer, and so on
  • One of the most important things about using nginx is modification nginx.conf This configuration file
  • First, pull the latest image of nginx from dockerhub

    docker pull nginx
  • Simply start a nginx container to copy from the container nginx.conf file

    #Docker starts the nginx container named test
    docker run --name test -d nginx
  • View nginx container ID

    docker ps -a

    Note: docker needs two configuration files to start nginx

    Here reference ☛ youth of camouflage temptation
    https://blog.csdn.net/weixin_…

    • One is located inside the container/etc/nginx/nginx.conf
    • The other is located inside the container/etc/nginx/conf.d/default.conf
    • default.confThe configuration isserver { } Content of

##So here we need to copy these two configuration files to the host for mounting

  • Create a directory for storing nginx.conf And default.conf

    #- P create multi level directory
    mkdir /usr/docker/nginx -p
    mkdir /usr/docker/nginx/conf.d/ -p
  • Copy the nginx file from the nginx container to the newly created directory

    #Docker replicates / etc / nginx with container ID of e9bf057b53bb/ nginx.conf Path of nginx.conf File to the / docker / nginx / directory of the host outside the container
    docker cp e9bf057b53bb:/etc/nginx/nginx.conf /usr/docker/nginx/
    #Copy default.conf , the meaning of which is given in the previous note
    docker cp e9bf057b53bb:/etc/nginx/conf.d/default.conf /usr/docker/nginx/conf.d/
  • After copying, delete the container of the test that was just started temporarily

    #Stop the container named test
    docker stop test
    #Delete the container named test
    docker rm test
  • Restart a full nginx container

    docker run -d -p 80:80 \
    --name nginx --net host \
    -v /usr/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
    -v /usr/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
    -v /usr/docker/nginx/html:/usr/share/nginx/html \
    -v /usr/docker/vsftpd/root:/mnt/data \
    -v /usr/docker/nginx/logs:/var/log/nginx \
    --privileged=true --restart=always nginx

The following configuration explanation is important****Combine with me default.conf The configuration of

Explanation:

-P port 80 of the host is mapped to port 80 of the container

–The name nginx container is named nginx

–Net host container and host public network

-To map the path between the host and the container, the first step is to set the two configuration files nginx.conf and default.conf Mapping from the container directory to the corresponding directory under / usr / docker / nginx of the host, and then changing the configuration of the host is equivalent to changing the configuration file in the container.

Second, look at the configuration in the following pictures. All the files in the corresponding positions of the container are found. Yes, I have mapped these locations. For example, the home page location of nginx is mapped to / usr / docker / nginx / HTML. The most important thing is to map the static resource location to the vsftpd static resource server / usr / docker / vsftpd / root directory of the host close default.conf You can access the static image file under vsftpd through nginx.

-V also maps the log file directory

–Privileged = true gives root permission to the container

–Restart = always starts automatically with docker

start-up nginx:latest image

Using docker to install nginx and vsftpd to build file server

#Open Host Firewall port 80
firewall-cmd --zone=public --add-port=80/tcp --permanent
#Restart the firewall
firewall-cmd --reload
#View the ports where the firewall has been started
firewall-cmd --zone=public --list-ports
  • At this time, open the browser to access the host IP address, which is 192.168.137.100. I should visit the nginx welcome page,Error 403 will be reported here, because it is not found in the / usr / docker / nginx / HTML path index.html This welcome page
  • Well, to be precise, nginx has been started as a docker container

    #Simply write a welcome page, at least visit the host IP I this is 192.168.137.100 to see the effect
    cd /usr/docker/nginx/html
    #Add one index.html File write Hello nginx
    echo "hello nginx" > index.html
    #Nginx hot update in container
    docker exec -it nginx  nginx -s reload
    #Restart the ngixn container directly without using it
    docker restart nginx
    #Obsessive compulsive disorder requires nginx home page. If I say so, I directly copy the index page of nginx in windows to / usr / docker / nginx / HTML

Using docker to install nginx and vsftpd to build file server

Using docker to install nginx and vsftpd to build file server

Now that the nginx container is started, start the vsftpd container

#Pull vsftpd image
docker pull fauria/vsftpd
#Start container
docker run -d -v /usr/docker/vsftpd:/home/vsftpd \
-p 20:20 -p 21:21 -p 21100-21110:21100-21110 \
-e FTP_USER=root -e FTP_PASS=123456 \
-e PASV_ADDRESS=192.168.137.100 -e PASV_MIN_PORT=21100 -e PASV_MAX_PORT=21110 \
--name vsftpd --privileged=true --restart=always fauria/vsftpd
#Open 20 firewall port
firewall-cmd --zone=public --add-port=20/tcp --permanent
#Open port 21 of firewall
firewall-cmd --zone=public --add-port=21/tcp --permanent
#Open 21100-21110 firewall port
firewall-cmd --zone=public --add-port=21100-21110/tcp --permanent
#Restart the firewall
firewall-cmd --reload
#View the ports where the firewall has been started
firewall-cmd --zone=public --list-ports

Explanation of docker start command:

-V mount the directory, which is equivalent to mounting the directory in the container locally

-P port mapping local 20 container 20… Etc

-E. set the user name, password, host address, minimum and maximum port respectively

–Name names the container vsftpd

–Privileged = true give the container root permission

–Restart = always starts automatically when docker is opened

A mirror image of the name of the run that fauria / vsftpd runs

  • Open browser accessftp://192.168.137.100
  • So far, the file server is successfully set up
  • Add a new FTP user to an existing container

    #Enter the inside of the container
    docker exec -it vsftpd bash
    #New folder for user myuser
    mkdir /home/vsftpd/myuser
    #Add a new user, myuser. The newline password 5678 is written into the file
    echo -e "myuser\n5678" >> /etc/vsftpd/virtual_users.txt
    #Password write to database
    /usr/bin/db_load -T -t hash -f /etc/vsftpd/virtual_users.txt /etc/vsftpd/virtual_users.db
    #Exit inside container
    exit
    #Restart vsftpd
    docker restart vsftpd
  • Through nginx access to the static resource server imageshttp://192.168.16.128/images/test.png

Using docker to install nginx and vsftpd to build file server

  • Using nginx is convenient for us to use HTTP protocol to access static resource server
  • In fact, only need to install vsftpd can FTP protocol upload images, browser accessftp://192.168.16.128, fill in the user name we set earlierrootpassword5678

Using docker to install nginx and vsftpd to build file server

In this paper, blog group send one article, multi post and other operation tool platformOpenWriterelease