Published on

Docker log output and startup commands

Authors
  • avatar
    Name
    Yuchen Wei
    Twitter

Options for docker run command

--attach, -a: Attach to STDIN, STDOUT or STDERR. This option can be specified multiple times, for example, -a stdin -a stdout.
--detach, -d: Run container in background and print container ID.
--interactive, -i: Keep STDIN open even if not attached.
--tty, -t: Allocate a pseudo-TTY.

Standard I/O streams: 0: stdin: Standard input. 1: stdout: Standard output. 2: stderr: Standard error. In the default situation with Docker, the container runs in the foreground with stdout and stderr connected, while stdin is closed (-d=false, -i=false, -t=false).

Scenario 1

When only -a stdin is specified, the container runs in the foreground, and Ctrl+C cannot exit it.

case1

Scenario 2

When only -a stdout is specified, the container will output standard output only.

case2

Scenario 3

When only -a stderr is specified, the container will output standard error only, and normal output will not be displayed on the command line.

case31

Others

However, regardless of the -a settings, docker logs can always show error output and standard output.

others1

Dockerfile

CMD

Supports three formats. It specifies the command executed when the container starts, and only the last CMD in a Dockerfile takes effect. If the user specifies a command when starting the container, it will override the CMD.

CMD ["executable","param1","param2"]: Uses exec; recommended.
CMD command param1 param2: Executes in /bin/sh; for interactive applications.
CMD ["param1","param2"]: Provides default parameters to ENTRYPOINT.

ENTRYPOINT

Configures the command executed when the container starts and cannot be overridden by parameters provided by docker run. Such as

ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2: Executes in shell.

Differences1: Commands specified by CMD can be overridden by the command specified in docker run, while those specified by ENTRYPOINT cannot. Differences2: CMD can set default parameters for ENTRYPOINT, which can be overridden by parameters specified in docker run.

Summary

If a command is configured in the container, it uses that command with arguments from the container if provided; if no arguments are provided, it executes the command without them. If no command is configured in the container, but arguments are provided, it uses the command from the Dockerfile with the arguments from the container. If neither command nor arguments are configured, it uses the defaults from the Dockerfile.