Dockerfile Use Tutorial

Posted by Andy82 on Fri, 20 Mar 2020 20:46:03 +0100

1. What is a Dockerfile

Dockerfile is a description file for creating a custom Docker image. We can write an instruction in the Dockerfile to control what the image needs to do. The instructions in the Dockerfile are detailed below.

2. Four Functional Partitions of Dockerfile

1. Basic Mirroring

Declare the base image for a custom image

FROM centos

2. Descriptive information

Here you can declare a list of information about the owner, version, build time, description of the mirror, and anything else

LABEL maintainer=tingtiandadi@360.cn
LABEL version="1.0"
LABEL date="2020/03/08"
LABEL description="RPM Software Package Making"

3. Install Mirror Execution Instructions

3.1 USER

Users specified when executing shell directives in Dockerfile, including RUN, CMD, ENTRYPOINT

USER <user>[:<usergroup>]
USER root

3.2 EXPOSE

Specify the service port at which the container runs

EXPOSE 80 443

3.3 WORKDIR

Set working directory for instructions executed in Dockerfile, including ADD, COPY, RUN, CMD, ENTRYPOINT

WORKDIR /root

3.4 ADD

Copy files/directories into the mirror

ADD <src>...<dest>
ADD rpmbuild.tar.gz /root/
ADD https://xxx.com/rpmbuild.tar.gz /root/

3.5 COPY

Copy files/directories into the mirror in the same usage as ADD, but automatic download and decompression are not supported

ADD /home/mysql_cron /etc/cron.d/

3.6 VOLUME

Mounting a host's directory into a docker, similar to nfs file sharing, is rarely used, mostly in docker run Use-v for mounting

VOLUME ["/usr/local/"]

3.7 ARG

Parameters specified when building a mirror

ARG user
USER $user

Just need to docker build Just take the user parameter with you

docker build --build-arg user=root -f dockerfile_rpmbuild .

3.8 RUN

Instructions to run when building a mirror

RUN ["yum", "install", "net-tools", "-y"]
RUN yum install net-tools -y

3.9 ENV

Setting environment variables within the current system

ENV JAVA_HOME /usr/local/jdk1.8.0_231

3.10 HEALTHCHECK

Check if the container is still working

HEALTHCHECK --interval=5m --timeout=3s --retries=3 \
    CMD curl -f http:/localhost/ || exit 1
  • Option description:
    --interval=DURATION (default: 30s): how often to probe, default 30 seconds

-- timeout= DURATION (default: 30s): Service response timeout, default 30 seconds
--start-period= DURATION (default: 0s): how long after the service starts to probe, default 0 seconds
--retries=N (default: 3): think detection failures are downtime several times, default three times

  • Return value description:
    0: Container success is healthy and ready to use

1: Unhealthy containers do not work properly
2: Retain not using this exit code

4. Container Start Execution Instruction

4.1 CMD Mode

Multiple edits can be made, and each instruction is executed sequentially.

CMD ["-C", "/start.sh"] 
CMD ["/usr/sbin/sshd", "-D"] 
CMD /usr/sbin/sshd -D

4.2 ENTRYPOINT mode

Similar to CMD, but commands executed this way will not be docker run The parameters being executed are overridden, and docker run The parameters executed are passed to the script executed this way.

ENTRYPOINT [ "rpmbuild", "-bb" , "/root/rpmbuild/SPEC/mysql-install.spec"]
ENTRYPOINT [ "dpkg", "-b", "mysql-install", "mysql-install.deb"]

Important note: ENTRYPOINT can have more than one line, but only the last line will take effect when executed, remember!

3. Dockerfile instance

# base image
FROM centos

# Descriptive Information
LABEL maintainer=tingtiandadi@360.cn
LABEL version="1.0"
LABEL date="2020/03/08"
LABEL description="RPM Software Package Making"

# Install Mirror Execution Instruction
WORKDIR /root
USER root

RUN rpm -ivh net-tools-2.0-0.51.20160912git.el8.x86_64.rpm
RUN yum install rpm-build-4.14.2-25.el8.x86_64 -y

# Start Container Execution Instruction
ENTRYPOINT [ "rpmbuild", "-bb", "rpmbuild/SPEC/*.spec"]

Topics: Docker RPM yum MySQL