Docker Commit Created Images and ENTRYPOINT

2019-02-06 00:25发布

How do you ensure that the original CMD specified in your Dockerfile is still set to run on docker run, when you make changes via docker commit?

Here's the sequence of events, to make it a little clearer:

  1. Create image with Dockerfile
  2. Run container from image with -ti --entrypoint /bin/bash at some point afterwards to make some changes
  3. Make changes inside container and run docker commit to create new image, with new tag
  4. When the new image is run, the original CMD entry from the original Dockerfile is no longer run

So I'm asking; how do you reset the CMD from the Dockerfile again on a committed image?

标签: docker
2条回答
forever°为你锁心
2楼-- · 2019-02-06 01:05

Current Docker versions (I'm on 1.11.1) provide a --change option that allow in-line manipulation of the image at commit time, as in:

docker commit --change='ENTRYPOINT ["myEntryPoint.sh"]' $(docker ps -lq)

CMD is also supported as are a few others. See manpage for more details and examples.

查看更多
Emotional °昔
3楼-- · 2019-02-06 01:29

You would create a Dockerfile to set the CMD or ENTRYPOINT. Simply base the Dockerfile on the image id returned by docker commit. For example, given this:

$ docker commit $(docker ps -lq)
69e9c08825508ec780efc86268a05ffdf4edae0999a2424dbe36cb04c2a15d6b

I could create a Dockerfile that looked like this:

FROM 69e9c08825508ec780efc86268a05ffdf4edae0999a2424dbe36cb04c2a15d6b
CMD ["/bin/bash"]

And then use that to build a new image:

$ docker build .
Step 0 : FROM 69e9c08825508ec780efc86268a05ffdf4edae0999a2424dbe36cb04c2a15d6b
 ---> 69e9c0882550
Step 1 : CMD /bin/bash
 ---> Running in f886c783551d
 ---> 13a0f8ea5cc5
Removing intermediate container f886c783551d
Successfully built 13a0f8ea5cc5

That said, your best course of action is probably to not make changes in the container and then use Docker commit; you end up with a much more auditable set of changes if you just rely on the Dockerfile to implement the necessary changes in the first place.

查看更多
登录 后发表回答