user3828398
user3828398

Reputation: 565

run docker build in cmake

I'm trying to build the docker image in cmake with the following:

add_custom_target(module_tests
  COMMAND cp -r ${FFMPEG} .
  COMMAND cp ${FFMPEG_SO_INSTALL} ${FFMPEG}/lib
  COMMAND docker build -t moduletest -f Dockerfile .
)

But I got error saying /bin/sh: 1: docker: not found, while running docker build manually works.

Upvotes: 3

Views: 10839

Answers (1)

Kevin
Kevin

Reputation: 1483

To elaborate on the excellent comment of Tsyvarev, your code should look more like

find_program(Docker_EXECUTABLE docker)
if(NOT Docker_EXECUTABLE)
    message(FATAL_ERROR "Cannot find the docker executable!")
endif()
add_custom_target(module_tests
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${FFMPEG} ${CMAKE_CURRENT_LIST_DIR}
    COMMAND ${CMAKE_COMMAND} -E copy ${FFMPEG_SO_INSTALL} ${FFMPEG}/lib
    COMMAND ${Docker_EXECUTABLE} build --tag moduletest --file
        ${CMAKE_CURRENT_LIST_DIR}/Dockerfile ${CMAKE_CURRENT_LIST_DIR}
)

This code is portable across platform. Moreover, this code will generate a clear error message during CMake time, i.e. very early, if Docker is not found.

Finally, getting to the point of your question, the documentation of find_program explains how it works and where it looks to find programs. For instance, you'll see that the paths in the variable CMAKE_PROGRAM_PATH are searched, so something you could do is which docker from a shell, and then, using the path that which printed, you could call cmake using a command like

cmake -DCMAKE_PROGRAM_PATH=/dockerDirectory/printed/by/which ..

assuming that the CMakeLists.txt is in the directory ..

Bonus: Consider using ${CMAKE_COMMAND} -E copy_if_different instead of copy.

Upvotes: 7

Related Questions