Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Commit Created Images and ENTRYPOINT

Tags:

docker

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?

like image 513
Afraz Avatar asked Mar 12 '15 16:03

Afraz


People also ask

Does a docker image need an entrypoint?

Any Docker image must have an ENTRYPOINT or CMD declaration for a container to start. Though the ENTRYPOINT and CMD instructions may seem similar at first glance, there are fundamental differences in how they build container images. (This is part of our Docker Guide.

What docker commit does?

Description. It can be useful to commit a container's file changes or settings into a new image. This allows you to debug a container by running an interactive shell, or to export a working dataset to another server. Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.

Can docker commit command Create image from running container?

You can run docker commit (docs) to save the container to an image, then push that image with a new tag to the registry. Show activity on this post. This can be easily done by using "docker commit".


2 Answers

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.

like image 137
sxc731 Avatar answered Oct 05 '22 04:10

sxc731


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.

like image 35
larsks Avatar answered Oct 05 '22 02:10

larsks