简体   繁体   English

一条命令构建并运行 Dockerfile

[英]Build and run Dockerfile with one command

Is it possible to build image from Dockerfile and run it with a single command?是否可以从 Dockerfile 构建图像并使用单个命令运行它?
There is one command docker build to build a Dockerfile and docker run -it to run the image.有一个命令 docker docker build用于构建 Dockerfile 和 docker docker run -it -it 用于运行图像。

Is there any combination of these two commands to make it easier to build and run with just one command?是否有这两个命令的任何组合,可以更轻松地仅使用一个命令构建和运行?

If you want to avoid tagging, docker build -q outputs nothing but the final image hash , which you can use as the argument to docker run :如果你想避免标记, docker build -q只输出最终图像哈希,你可以将其用作docker run的参数:

docker run -it $(docker build -q .)

And add --rm to docker run if you want the container removed automatically when it exits.如果您希望容器在退出时自动删除,则将 --rm 添加到--rm docker run

docker run --rm -it $(docker build -q .)

No, there is no single command.不,没有单一的命令。 But if you tag your image as you build it, it will be easier to run:但是如果你在构建它的时候标记你的图像,它会更容易运行:

docker build -t foo . && docker run -it foo

I use docker-compose for this convenience since most of the apps I'm building are talking to external services sooner or later, so if I'm going to use it anyway, why not use it from the start.我使用 docker-compose 是为了方便,因为我正在构建的大多数应用程序迟早会与外部服务通信,所以如果我无论如何都要使用它,为什么不从一开始就使用它。 Just have docker-compose.yml as:只需将 docker-compose.yml 作为:

version: "3"
services:
  app:
    build: .

and then just run the app with:然后运行应用程序:

docker-compose up --build app

It will rebuild the image or reuse the container depending on whether there were changes made to the image definition.它将根据是否对图像定义进行了更改来重建图像或重用容器。

If you use Makefile, I find this snippet useful:如果你使用 Makefile,我发现这个片段很有用:

build:
    @docker build . | tee .buildlog

bash: build
    @docker run --rm -it $(shell grep "Successfully built" .buildlog | cut -d ' ' -f 3) /bin/bash

You don't need tagging, like in @jonathon-reinhart answer, but you also get the build output.您不需要标记,就像@jonathon-reinhart 的回答一样,但您也可以获得构建输出。

Recently I started getting a promo message about using docker scan after every build.最近我开始收到关于在每次构建后使用docker scan促销信息。

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them使用“docker scan”对图像运行 Snyk 测试以发现漏洞并学习如何修复它们

Here's what I used to do:这是我过去常做的事情:

docker build --quiet .

and here's what is working now:这是现在的工作:

docker build --quiet . | head -n1

You can also do docker build and pipe image name which it outputs to docker run :您还可以执行docker build和管道图像名称,它输出到docker run

docker build . | tail -n1 | cut -d' ' -f3 | xargs -I{} docker run {}
  • docker build will give you multi-line text ... Successfully built 18e77bc0d83a docker build会给你多行文本... Successfully built 18e77bc0d83a
  • you get the last line with tail -n1你得到最后一行tail -n1
  • split by ' ' and get 3 rd word with cut -d' ' -f3' '分割并用cut -d' ' -f3得到3
  • pass it as argument to run with xargs -I{} docker run {}将其作为参数传递以使用xargs -I{} docker run {} run

docker-build-and-run

I've created a little helper command for building and running, in a single command.我已经在一个命令中创建了一个用于构建和运行的小帮助命令。 On Linux or Mac, you can add this to your ~/.bash_profile to make it available in the Terminal.在 Linux 或 Mac 上,您可以将其添加到您的~/.bash_profile以使其在终端中可用。

Usage:用法:

docker-build-and-run BUILD_ARGS [-- RUN_ARGS] [-- RUN_COMMAND]

Examples:例子:

docker-build-and-run . -- npm run test
docker-build-and-run --file ./Dockerfile . -- -v ~/volume:/var/volume -- node server.js

The Script:剧本:

Add this to a .sh file, or add it to your ~/.bash_profile :将其添加到.sh文件,或将其添加到您的~/.bash_profile

TERM_GREEN="\033[1;32m"
TERM_BLUE="\033[1;34m"
TERM_NC="\033[0m"
docker-build-and-run() {
    if [[ -z "$@" ]]; then
        echo "
            Usage:
                docker-build-and-run BUILD_ARGS [-- RUN_ARGS] [-- RUN_COMMAND]
            Examples:
                docker-build-and-run . -- npm run test
                docker-build-and-run --file ./Dockerfile . -- -v ~/volume:/var/volume -- node server.js
        "
        return
    fi

    # Extract the segments between the dashes:
    BEFORE_THE_DASHES=
    while (( "$#" )); do
        if [[ "$1" = "--" ]]; then
            shift
            break
        fi
        BEFORE_THE_DASHES="$BEFORE_THE_DASHES $1"
        shift
    done
    SEGMENT_1=$BEFORE_THE_DASHES

    BEFORE_THE_DASHES=
    while (( "$#" )); do
        if [[ "$1" = "--" ]]; then
            shift
            break
        fi
        BEFORE_THE_DASHES="$BEFORE_THE_DASHES $1"
        shift
    done
    SEGMENT_2=$BEFORE_THE_DASHES

    SEGMENT_3=$@


    BUILD_ARGS=$SEGMENT_1
    RUN_ARGS=$SEGMENT_2
    RUN_COMMAND=$SEGMENT_3
    if [ -z "$RUN_COMMAND" ]; then
      RUN_COMMAND=$RUN_ARGS
      RUN_ARGS=
    fi


    TEMP_TAG=docker-build-and-run-temp

    docker rm -f $TEMP_TAG 2>/dev/null
    printf "${TERM_GREEN}Building Docker container (${TERM_BLUE}docker build $BUILD_ARGS${TERM_GREEN})${TERM_NC}\n" \
    && docker build --tag $TEMP_TAG $BUILD_ARGS \
    && printf "${TERM_GREEN}Running Docker container (${TERM_BLUE}docker run $RUN_ARGS $RUN_COMMAND${TERM_GREEN})${TERM_NC}\n" \
    && docker run --rm -it $RUN_ARGS --label $TEMP_TAG $TEMP_TAG $RUN_COMMAND
}

For anyone looking for a reusable solution, you could install this docker plugin which i created: https://github.com/stasmihailov/docker-script对于寻找可重用解决方案的任何人,您可以安装我创建的这个 docker 插件: https ://github.com/stasmihailov/docker-script

Then you could build and run a Dockerfile as follows:然后你可以构建并运行一个 Dockerfile,如下所示:

docker script ./Dockerfile

Windows PC电脑

Make a run.bat file.制作一个run.bat文件。 Then in the file add this:然后在文件中添加:

docker build -t foo . 
docker run -it foo

To run the file using powershell or cmd do this:要使用 powershell 或 cmd 运行文件,请执行以下操作:

./run.bat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM