wdhilliard
wdhilliard

Reputation: 144

Docker commit without running

When I run docker build . the id that is spit out is of the image, which is what I thought was being committed to the docker repo. But when i run docker commit <id>, it says that it is not a valid container id. I usually get around this by starting the image in a container and then committing that id. But what should I do if the container requires linked containers to run? Running the container can take a long time especially when the build process is in the run script. If this fails, or requires a linked container to succeed, the process will exit, and my container will shut down, which does not allow me to create my new image. Is there a way to build your dockerfile and commit to the repo at the same time? Alternatives?

Upvotes: 1

Views: 1068

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91567

A Dockerfile is designed to provide a completely host independent way to repeatably build images without depending on any aspect of the host's configuration. This is why linking is not included in individual build steps, as it would render the build dependent on the other containers on the host at the time of build. Because of this Dockerfiles are not the only way to build containers.

When you must have a host dependent build environment, use a Dockerfile for the base part, installing dependencies etc, then use docker run from a script/configuration management system of your choice to setup the other containers and do the actual build. Once the build is complete, you can commit the resulting container, tag it with a name, and then push it to the repo.

To address the question at the top of the post, If you want to give a name to an image produced by a Dockerfile use docker tag image-id name

  • Committing takes a container and produces an image
  • tagging takes an image and gives it a name
  • pushing takes an image an a name and makes it available to pull later.

Upvotes: 3

Related Questions