Build and run Dockerfile with one command

Is it possible to build and run the Dockerfile with just one command, as there is one command docker build

to build the Dockerfile and docker run -it

to run the command.

Is there any combination of the two to make it easier to build and run with just one command?

+78


source to share


5 answers


No, there is no single team. But if you tag your image as you create it, it will be easier to run it:



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

      

+97


source


If you want to avoid the tags, docker build -q

it outputs nothing but the hash of the final image , which you can use as an argument for docker run

:

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

      



And add --rm

in docker run

if you want the container to be automatically deleted on exit.

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

      

+140


source


If you are using a Makefile, I find this snippet useful:

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 to add tags as in @ jonathon-reinhart's answer, but you get build output as well.

+3


source


Windows PC

Create a file run.bat

. Then in the file add this:

docker build -t foo . 
docker run -it foo

      

To run a file using powershell or cmd, do the following:

./run.bat

      

-1


source


docker-build-and-run

I created a small helper command to build and run in one command. On Linux or Mac, you can add this to yours ~/.bash_profile

to make it available in Terminal.

Using:

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

      

Scenario:

Add this to your file .sh

or add to yours ~/.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
}


      

-1


source







All Articles