Shell script to save time when building and running docker images locally.
bash ../bash-practise/docker-build-and-run.sh -a IMAGE_NAME -b HOST_PORT -c CONTAINER_PORT
-
Script builds docker image using DOCKERFILE in current directory giving the image the title IMAGE_NAME
`docker build -t "${IMAGE_NAME}"` .
-
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')"`
-
-
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'`