Both RUN and ENTRYPOINT directives can be executed in two ways: shell and exec.

Shell way



Example:

RUN apt-get install python3
CMD echo "Hello world"
ENTRYPOINT echo "Hello world"
Copy the code

When an instruction is executed in shell mode, it calls /bin/sh -c in the background and does normal shell processing. For example, the following definition in Dockerfile:

ENV name voidking
ENTRYPOINT echo "Hello, $name"
Copy the code

Docker run will print Hello, voidking, and the variable will be replaced.

The exec mode


[“executable”, “param1”, “param2”,…] Example:

RUN ["apt-get"."install"."python3"]
CMD ["/bin/echo"."Hello world"]
ENTRYPOINT ["/bin/echo"."Hello world"]
Copy the code

When an instruction executes in exec mode, it invokes the executable directly and does no shell processing. For example, the following definition in Dockerfile:

ENV name voidking
ENTRYPOINT ["/bin/echo"."Hello, $name"]
Copy the code

Docker run will print Hello, $name, and the variable will not be replaced.

If you want to run bash instead of sh, use exec. In this case, normal shell processing is done. For example, the following definition in Dockerfile:

ENV name voidking
ENTRYPOINT ["/bin/bash"."-c"."echo Hello, $name"]
Copy the code

Docker run will print Hello, voidking, and the variable will be replaced.

CMD definition

Access dockerHub Ubuntu, Supported tags and respective Dockerfile Links to access dockerHub Ubuntu, Supported tags and respective Dockerfile Links to access dockerHub Ubuntu, Supported tags and respective Dockerfile Links. Click on the link to see the definition of Dockerfile.

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



CMD ["/bin/bash"]
Copy the code

The default command is /bin/bash. The default command is /bin/bash.

Docker run Ubuntu :16.04 will run the default command /bin/bash.

Executing a specific command

We want to execute the command, so we need to specify the command when docker run, overriding the default command. Docker run Ubuntu :16.04 sleep 3600

If you want to make this particular command permanent, you need to define a new image using Dockerfile.

FROM ubuntu:16.04

CMD ["sleep"."3600"]
Copy the code

Docker build-t Ubuntu-sleeper., generates a new image.

Docker run ubuntu-sleeper, execute the default command sleep 3600.

Specific parameter

If we want to modify the time of sleep, what should we do?

docker run ubuntu:16.04 sleep 3600
docker run ubuntu:16.04 sleep 1200
Copy the code

The sleep command has not changed, only the parameter has changed. Can the sleep be omitted? Yes, define a new mirror.

FROM ubuntu:16.04

ENTRYPOINT ["sleep"]
CMD ["3600"]
Copy the code

Docker build-t Ubuntu-sleeper., generates a new image.

Docker run ubuntu-sleeper, execute the default command sleep 3600.

Docker run ubuntu-sleeper 1200, run sleep 1200.

Can commands in ENTRYPOINT be replaced? That’s ok. Docker run –entrypoint sleep2.0 Ubuntu-sleeper 1200, sleep2.0 1200

To sum up, Docker run executes ENTRYPOINT + CMD by default. Typically, we define ENTRYPOINT as a fixed command in Dockerfile and define CMD as the default argument.

When pod is defined in K8S, there are two fields, args and Command. These two fields override CMD and ENTRYPOINT, respectively.

apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: ubuntu name: ubuntu spec: containers: - image: Ubuntu :16.04 Name: Ubuntu Resources: {} command: ["sleep"] args: ["1200"] dnsPolicy: ClusterFirst restartPolicy: Always status: {}Copy the code

After the pod is started, the execution command is sleep 1200.

www.jb51.net/article/136…