关于Docker build命令中'-'的作用及Linux下含义的技术咨询
- in docker build - < Dockerfile Great question! Let's break down what this - does, both in the context of Docker and as a general Linux convention.
1. What - means in docker build
When you run docker build - < Dockerfile, the - tells Docker to read the build context from standard input (stdin).
Normally, docker build expects a directory path as its argument (like docker build .), which becomes the build context — all files in that directory get sent to the Docker daemon to use during the build (think of COPY or ADD commands that pull files from the context). But with -, instead of sending a full directory, Docker reads the context directly from stdin.
In your specific command, the < Dockerfile redirects the contents of your Dockerfile into stdin. So Docker uses that input as the Dockerfile itself, and since there are no other files in the context, any COPY or ADD instructions in your Dockerfile will fail (there's nothing to copy from the context). This is handy when you want to build an image without sending an entire directory, or when generating a Dockerfile dynamically and passing it straight to docker build.
2. General meaning of - in Linux commands
Across most Linux tools, - is a widely adopted convention that represents standard input (stdin) or standard output (stdout), depending on what the command is designed to do:
- For commands that read input:
-means "read from stdin instead of a file". Examples:cat -will read text you type in the terminal and print it back out.grep "pattern" -will search for "pattern" in input piped into stdin.
- For commands that write output:
-means "write to stdout instead of a file". Examples:curl https://example.com -o -will print the downloaded content to your terminal instead of saving it to a file.tar -czf - ./my-foldercreates a compressed tar archive and sends it to stdout (you can pipe this tossh user@host "tar -xzf -"to copy files over SSH directly).
There's a less common use case too: some commands use - to signal the end of command-line options, so any arguments after - are treated as filenames even if they start with -. For example, rm -- -my-file lets you delete a file named -my-file without rm interpreting it as an option.
内容的提问来源于stack exchange,提问作者pythonic

