Dockerfile basic commands
FROM-based on base image
- FROM centos # Based on centos:latest
- FROM scratch # does not rely on any base image base image
- FROM tomcat:9.0.22-jdk8-openjdk
LABEL & MAINTAINER-Information
- MAINTAINER xxxx.com
- Institution & Personal ID
- LABEL version=“1.0”
- LABEL description="This is the description information"
WORKDIR-Set the working directory
- WORKDIR /usr/local
- WORKDIR /usr/local/newdir # automatic creation
- Try to use absolute paths
ADD & COPY-copy files
- ADD hello / # Copy to the root directory
- ADD test.tar.gz / # Add to the root directory and unzip
- In addition to copying, ADD also has the function of adding remote files
ENV-set environment constant
- ENV JAVA_HOME /usr/local/openjdk8
- RUN ${JAVA_HOME}/bin/java -jar test.jar
- Use environment constants as much as possible to improve program maintainability
Dockerfile execution instructions
RUN & CMD & ENTRYPOINT
-
RUN: execute commands during Build
- RUN yum install -y vim # Shell command format
- Will spawn a subshell process
- RUN ["yum", "install", "-y", "vim"] # Exec command format
- Will replace the current process with the Exec process and keep the PID unchanged
- After the execution is complete, exit directly without returning to the previous process environment
- It is recommended to use the Exec command format to execute commands
- RUN yum install -y vim # Shell command format
-
ENTRYPOINT: The command executed when the container starts
- Only the last entrypoint in the Dockerfile will be executed
- ENTRYPOINT["ps"] # Exec format is recommended
-
CMD: execute the default command or parameter after the container is started
- CMD is used to set the command executed by default
- If there are multiple CMDs in the Dockerfile, only the last one will be executed
- If additional instructions are added when the container is started, CMD will be ignored
- CMD ["ps", "-ef"] # Exec format is recommended
-
For example
FROM centos
RUN ["echo", "image building!!!"]
ENTRYPOINT ["ps"]
CMD ["-ef"]
Dockerfile combat-building a Redis image
FROM centos
RUN ["yum", "install", "-y", "gcc", "gcc-c++", "net-tools", "make"]
WORKDIR /usr/local
ADD redis-4.0.14.tar.gz .
WORKDIR /usr/local/redis-4.0.14/src
RUN make && make install
WORKDIR /usr/local/redis-4.0.14
ADD redis-7000.conf .
EXPOSE 7000
CMD ["redis-server", "redis-7000.conf"]
- Build image
- docker build -t python_id.com/docker-redis .