Skip to content

script to save time when building and running docker images locally

Notifications You must be signed in to change notification settings

michaelfswann/docker-build-and-run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

docker-build-and-run

Shell script to save time when building and running docker images locally.

Usage

bash ../bash-practise/docker-build-and-run.sh -a IMAGE_NAME -b HOST_PORT -c CONTAINER_PORT

  1. Script builds docker image using DOCKERFILE in current directory giving the image the title IMAGE_NAME

      `docker build -t "${IMAGE_NAME}"` .
    
  2. Script runs the image using following flags:

    • --rm flag sets cleanup

    • -it flag sets interactive and tty

    • --name sets container name to IMAGE_NAME

    • -p maps host port to container port eg 3000:8080

    • --env-file add in env vars from .env file in current dir

      `docker run --rm -it --name "${IMAGE_NAME}" --env-file ./.env -p "${MAP_HOST_PORT}:${MAP_CONTAINER_PORT}" "$(docker images | awk '{print $3}' | awk 'NR==2')"`
      
  3. Final part of the script manipulates output of docker images using awk to return the third column of the result then shortens to first row leaving us with just the IMAGEID of the most recently built image:

       `docker images | awk '{print $3}' | awk 'NR==2'`
    

Checked with ShellCheck

About

script to save time when building and running docker images locally

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages