Skip to content

Instantly share code, notes, and snippets.

@nerdalert
Created June 17, 2019 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdalert/b9cefaac4582c88eb71eeaab758e5e1d to your computer and use it in GitHub Desktop.
Save nerdalert/b9cefaac4582c88eb71eeaab758e5e1d to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check that the image name in the Docker Container matches the image name being inspected
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func checkDockerImageName(dockerImage string, containerID string) bool {
if containerID == "" {
errMessage := "The container id/name is blank!"
printError(errMessage)
inspectionData.ProcessesResults = append(inspectionData.ProcessesResults, formatHTMLError(errMessage))
return false
}
var dockerCommand string
// Determine correct docker command syntax as the syntax changed after 1.12
if strings.Contains(inspectionData.SystemDockerClientVersion, "1.12") {
dockerCommand = "docker inspect --format '{{.Config.Image}}' " + containerID
} else {
dockerCommand = "docker container inspect --format '{{.Config.Image}}' " + containerID
}
output, err := runCommand(dockerCommand)
if err != nil {
errMessage := "Unable to retrieve the image name from the Docker container!"
if output != "" {
errMessage = errMessage + ", " + output
} else {
errMessage = errMessage + ", " + err.Error()
}
printError(errMessage)
inspectionData.ProcessesResults = append(inspectionData.ProcessesResults, formatHTMLError(errMessage))
return false
}
if output != dockerImage {
errMessage := "The container " + containerID + " is running from the docker image " + output + ", and not from the docker image " + dockerImage + " being inspected!"
printError(errMessage)
inspectionData.ProcessesResults = append(inspectionData.ProcessesResults, formatHTMLError(errMessage))
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment