Docker | dockerfile file preparation

Role of dockerfile

The role of dockerfile is to create images and maintain the consistency of development, testing and production environments.

Mirror containers directly

Make a new image

	# After personalizing the container according to your own needs, you can create your own image
	docker commit container id Image name:Version number

Let's use the nginx service deployed in the previous article to make an image Docker | deploy Nginx

	docker commit 07388cf54586 frost_nginx:1.0

Pressure sink image

	docker save -o Compressed file name image name:Version number

Only the personality made in the container will be packaged into the compression file when compressed

Unzip the image and run

	docker load -i Compressed file name

Because it is in the test environment, delete the image first and restore it with the compressed name

	docker load -i frost_nginx.tar

Using dockerfile to generate images

dockerfile command

keyword effect remarks
FROM Specify parent mirror Specifies which image the dockerfile is built on
MAINTAINER Author information Used to indicate who wrote this dockerfile
LABEL label The Label used to indicate dockerfile can be used instead of Maintainer. Finally, it can be viewed in the basic information of docker image
RUN Execute command The default format for executing a command is / bin/sh: RUN command or run ["command", "Param1", "param2"]
CMD Container start command Provide the default command when starting the container and use it with ENTRYPOINT Format CMD command param1 param2 or CMD ["command", "Param1", "param2"]
ENTRYPOINT entrance It is generally used in making containers that are closed after execution
COPY Copy file Copy the file to the image during build
ADD Add file Adding files to the image during build is not limited to the current build context, but can come from remote services
ENV environment variable Specify the environment variable during build. You can override the format ENV name=value through - e when starting the container
ARG Construction parameters Build parameters are only used during construction. If ENV exists, the value with the same name of ENV always overrides the arg parameter
VOLUME Define data volumes that can be mounted externally Specify the build image directories that can be mounted to the file system when starting. Use the - v binding format VOLUME ["directory"] when starting the container
EXPOSE Exposed port Define the port to listen to when the container is running. Start the container and use - p to bind the exposed port format: export 8080 or export 8080 / UDP
WORKDIR working directory Specify the working directory inside the container. If it is not created, it will be automatically created. If the specified / uses an absolute address, if it does not start with /, it is the relative path of the path of the previous workdir
USER Specify execution user Specify the user when the run CMD entry is executed when the build or startup is started
HEALTHCHECK health examination The command to specify the health monitoring of the current container is basically useless, because many times the application itself has a health monitoring mechanism
ONBUILD trigger When there is an image with the ONBUILD keyword as the basic image, the command of ONBUILD will be executed after the execution of FROM, but it does not affect the current image and is not very useful
STOPSIGNAL Send semaphore to host The STOPSIGNAL instruction sets the system call signal to be sent to the container to exit.
SHELL Specifies the shell to execute the script Specifies the shell used when RUN CMD ENTRYPOINT executes the command

Simple dockerfile writing

	# Download nginx installation package
	wget -c 

Write dockerfile file:

# base image
FROM centos:7


# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src

# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel 
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx

# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2

# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio  --with-http_ssl_module  --with-http_realip_module    --with-http_addition_module    --with-http_xslt_module   --with-http_image_filter_module    --with-http_geoip_module  --with-http_sub_module  --with-http_dav_module --with-http_flv_module    --with-http_mp4_module --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_auth_request_module  --with-http_random_index_module   --with-http_secure_link_module   --with-http_degradation_module   --with-http_stub_status_module && make && make install

ENV PATH /usr/local/nginx/sbin:$PATH


ENTRYPOINT ["nginx"]

CMD ["-g","daemon off;"]

Put the installation package and dockerfile files in the same directory

	# Execute compile command
	docker build -t frost_test_nginx:1.0 .

Finally, a mirror is generated:

Test this image

	docker run -d -p 80:80 --name=frost_nginx frost_test_nginx:1.0

Details determine success or failure!
Personal folly, if there is something wrong, please correct it!

Tags: Docker Nginx dockerfile

Posted by krraleigh on Wed, 05 Jan 2022 17:04:18 +1030