Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Dockerfile to create Custom Image in docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use Dockerfile to create a custom image in docker. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Dockerfile is a configuration file in text format, and users can use dockerfile to quickly create custom images. Instructions include "configuration instructions" and "operation instructions".

Configuration instruction FROM

FROM debian:jessie

The first line of the dockerfile instruction, which specifies the base image

ARG

ARG VERSION=9.3

Defines a temporary variable in the process of creating a mirror, which is equivalent to a temporary variable in the operation of dockerfile

LABEL

LABEL version= "1.1.1.1"

Add metadata label information to the generated image for easy filtering

EXPOSE

EXPOSS 22 80 443

Declare the service listening port in the image. If you need to map it out, use the-p hostport:container_port parameter mapping to specify the port when starting the container.

ENV

ENV APP_VERSION=1.0.0

Specify environment variables to facilitate the use of subsequent RUN, which can be overridden by the env specified when docker starts.

ENTRYPOINT

ENTRYPOINT ["tail"]

Specify the default entry command, which is executed as the root command when starting the container. There is only one ENTRYPOINT in each dockerfile, and the runtime can be overridden by the-- entrypoint parameter. The difference from the CMD parameter is described in the "operation instruction CMD".

VOLUME

VOLUME ["/ data"]

Mount a data volume

USER

USER daemon

Specify the user name or UID when running the container, and subsequent RUN instructions will also use this user identity, or you can create a user

RUN groupadd-r postgres & & useradd-- no-log--init-r-g postgres postgres

Need to obtain administrator privileges to use the gosu command

WORKDIR

WORKDIR / app

Configure the working directory for subsequent RUN,CMD,ENTRYPOINT instructions

ONBUILD

ONBUILD RUN / python-build-- dir / user/app

Specify this parameter in the dockerfile, and when the sub-dockerfile uses the mirror created by the dockerfile as the base mirror, the command is executed first. Priority is used to create automatic compilation and to check the basic image.

STOPSIGNAL

STOPSIGNAL signal

Specifies the signal value that the container that creates the mirror starts to receive exit.

HEALTHCHECK

HEALTHCHECK NONE: no health check HEALTHCHECK CMD command: if the value returned by the command is 0 or 0, it will be healthy. By default, it will be checked once in 30s, timed out in 30s waiting for the result, and retried 3 times. If unhealthy, unhealthy will be displayed in STATUS

SHELL

SHELL ["/ bin/sh", "- c"]

Specify the shell type when other commands use shell. The delimiters of the windows system are inconsistent. Use # escape=' at the beginning of the dockerfile to translate.

Operation instruction RUN

RUN apt-get update

RUN ["/ bin/bash", "- c", "apt-get update"]

Runs the specified instruction, and each run generates a new mirror layer based on the base image of the last run

CMD

CMD ["python", "run.py"] or

CMD python run.py

Specify the command to be executed by default when starting the container. When the ENTRYPOINT parameter is used, the parameter of CMD will be passed into ENTRYPOINT as the default parameter, such as:

CMD ["tail", "- f", "/ usr/aaa"]

ENTRYPOINT ["tail"]

With the splicing of tailf tailf-f / usr/aaa, it is obvious that there will be an exception in this command and you need to pay attention to its reasonable use.

COPY

COPY. / app copies all the files under the current directory to the / app directory. The basic image without this directory will be created.

ADD

ADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz /

Advanced commands from COPY.

If you compress a tar file in the format of gzip,bzip2 and xz, the ADD instruction will decompress the compressed file automatically. If it is not compressed, it will be the same as COPY. If you need to copy the compressed file into it, you need to use the COPY command.

Multi-stage build # build the first stage, create the compilation environment FROM golang:1.8-alpine as builderRUN apk-no-cache add gitWORKDIR / go/src/github/go/HelloWorld/RUN go get-d-v github/go-sql-driver/mysqlCOPY app.go. # compile environment RUN CGO_ENABLED=0 GOOS=linux go build-a-installsuffix cgo-o app. # build the second segment The runtime environment image FROM alpine:latest as progRUN apk-- no-cache add ca-certificatesWORKDIR / root/#--from=0 copies the compiled binaries from the compiler environment container to the current directory # COPY-- from=nginx:latest / etc/nginx/nginx.conf / nginx.conf, or copies the files COPY-- from=0 / go/src/github/go/Helloworld/app .CMD [". / app"] .docker.idea__pycache__itchat.pklQR.png from other images.

When making an image, these files are not transferred to the image, so as to reduce the mirror volume and speed up the production process.

Dockerfile experience

Merge commands such as ADD RUN COPY as far as possible, reduce the number of surprise layers, create appropriate multi-step images, simplify the basic image, use .docker files, and reduce interference from external sources.

The above is how to use Dockerfile to create custom images in docker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report