up-for-docker.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # Program:
  3. # This program show docker image status.
  4. # Parameters:
  5. # $1 container-name,such as plg-wms-basic
  6. # $2 image-name,such as plg-eureka:1.0.0
  7. containerName=$1
  8. version=$2
  9. env=$3
  10. existContainer=`docker ps --format "{{.Names}}" | grep -w "$containerName" | head -n 1`
  11. if [ -n "$existContainer" ]; then
  12. imageName=`docker ps --format "{{.Image}} {{.Names}}" | grep -w "$containerName" | awk '{ print $1 }' `
  13. echo '=== Exist container '"$existContainer : $imageName"
  14. docker stop "$existContainer"
  15. fi
  16. existStopContainer=`docker ps -a --format "{{.Names}}" | grep -w "$containerName" | head -n 1`
  17. if [ -n "$existStopContainer" ]; then
  18. echo "=== Remove container ""$existStopContainer"
  19. docker rm "$existStopContainer"
  20. fi
  21. if [ -n "$imageName" ]; then
  22. existImage=`docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "$imageName" | head -n 1`
  23. if [ -n "$existImage" ]; then
  24. echo "=== Remove image ""$existImage"
  25. docker rmi "$imageName"
  26. fi
  27. fi
  28. existNewImage=`docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "$containerName":"$version" | head -n 1`
  29. if [ -n "$existNewImage" ]; then
  30. echo "=== Remove new image which exist ""$existNewImage"
  31. docker rmi "$existNewImage"
  32. fi
  33. existNewImage=`docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "$containerName":"$version" | head -n 1`
  34. if [ -z "$existNewImage" ]; then
  35. docker build --build-arg RUN_ENV="$env" -t "$containerName":"$version" .
  36. fi
  37. existNewImage=`docker images --format "{{.Repository}}:{{.Tag}}" | grep -w "$containerName":"$version" | head -n 1`
  38. if [ -n "$existNewImage" ]; then
  39. echo "=== Run image ""$version"
  40. docker run --network host --restart=always --name "$containerName" -v /data/logs:/usr/local/logs -v /data/upload:/usr/local/upload -d "$containerName":"$version"
  41. fi
  42. echo "=== over ==="
  43. exit 0