Build Nginx and PureFTPd in the same Docker image

I started to contact Docker a few days ago. After learning about its isolation and security, I am going to transfer my blog to the Docker container.

Why build your own image

Docker online warehouse provides many application images, which cover almost all server requirements. In the warehouse, users can directly pull and run from an operating system to an MC server. However, if only off the shelf images are used, they cannot be called learning Dockers. In addition, in some cases, these existing images cannot meet the requirements.

My blog program is WordPress, which runs in the Nginx+PHP 7+MySQL environment and uses FTP for file management. After thinking for a while, I decided to run PHP and MySQL in two separate Docker containers, and Nginx and PureFTPd in the same Docker container.

Nginx and PureFTPd together are easy to configure permissions and avoid many pitfalls. I didn't find a suitable image in the existing warehouse, so I decided to build one myself.

Several Common Commands of Dockerfile

Dockerfile is a collection of commands for building images, which is similar to a shell file to some extent. The Docker will execute the commands in the Dockerfile when building the image.

1.FROM

 FROM alpine:3.9

FROM introduces an existing image as our basic image. For example, if we plan to install QQ For Windows, we must have a Windows environment, which is the basic image.

Nginx and PureFTPd can both run in alpine Linux, so I use alpine Linux as the basic image. You can see that alpine is followed by a version number of 3.9, which can also be left blank. When the version number is omitted, it will get the latest (last) image version.

2.ENV

ENV defines some environment variable values, which can be used in Dockerfile. In the official DockerNginx script, you can see a line of environment variable definitions:

 ENV NGINX_VERSION 1.15.8

This environment variable is used later in the compilation process.

3.RUN

RUN is used to specify commands to be executed during the construction process, and is often used to install software packages. For example, you can execute the SHELL command through RUN:

 # RUN <command> the commanddefault is /bin/sh -c on Linux or cmd /S /C on Windows) RUN apk update \ &&apk add build-base \ &&echo "finish!" # RUN ["/bin/sh","-c","apk update"]

The result of the current RUN execution will be added to the image as the basic image of the following command. If you write the command to install QQ here, this step will install QQ for us when generating images.
The two writing methods of RUN, Shell and Exec, are clear at a glance and will not be discussed here.

4.COPY && ADD

 COPY nginx.conf /var/www/nginx/nginx.conf

The COPY command copies a local file to a specified location in the container. It is often used to copy the configuration file to the image.
The function of ADD is similar, but the first parameter of ADD can be a local file, URL, etc.

5.CMD

The cmd command is a command that runs after the container is started, and can be regarded as a startup item. Its writing method is similar to that of RUN, but it is not executed when the image is generated, but every time the container is "powered on".
If there are multiple CMD instructions in the Dockerfile, only the last CMD is valid.
CMD can be replaced by the command line parameters followed by the docker run.

6.ENTRYPOINT

 # ENTRYPOINT ["executable", "param1", "param2"] #ENTRYPOINT command param1 param2 # The shell format ignores the CMD or docker run parameters

If the entry after the container is started exists with the CMD, the content of the CMD will become an ENRTYPOINT parameter.
ENTRYPOINT will not be ignored and will be executed even if other commands are specified when running docker run.

The process of writing Dockerfile

The script process is basically the same as the process of installing software in the shell, except that these commands need to be automatically called by Docker, rather than we go to the shell to write a carriage return. The file I wrote is a combination of the Docker installation process of Nginx and the installation process of PureFTPd, and some configuration files are added. There is no complicated content.

It should be noted that if there is no foreground program in the container after startup, Docker will think that "you have nothing to do" and close the container. Use "daemon off;" The parameter allows Nginx to run in the foreground to prevent the container from being stopped by the Docker.

Complete Dockerfile

DockerNginxWithPureFTP

Postscript

As a beginner of Docker, my understanding and use may not be correct. I'm not a professional in operation and maintenance, so these things are enough at present.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4634.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. It's easier to download the whole site, leaving no technical tears

  2. OK, I understand (akimbo)~