3. Managing Your Docker Images
Activity 3.01: Build Scripts Using Git Hash Versioning
Solution:
There are a variety of ways you could complete this activity. Here is one example:
- Create a new build script. The first line, showing the set
–ex
command, prints each step to the screen and will fail the script if any of the steps fail. Lines 3 and 4 set the variables for your registry and service names:1Â set -ex 2 3Â REGISTRY=dev.docker.local:5000 4Â SERVICENAME=postgresql
- In line 6, set the
GIT_VERSION
variable to point to your short Git commit hash. The build script then prints this value to the screen in line 7:6Â GIT_VERSION=`git log -1 --format=%h` 7Â echo "version: $GIT_VERSION "
- Use the
docker build
command in line 9 to create your new image and add thedocker push
command in line 11 to push the image to your local Docker registry:9 docker build -t $REGISTRY/$SERVICENAME:$GIT_VERSION . 10 11 docker push ...