Docker Community Forums

Share and learn in the Docker community.

Generating Dockerfile from image

Hi, is it possible to create Dockerfile from image, below is details.

  1. started container from base image i.e. centos
  2. went inside container through docker exec /bin/bash
  3. copied application from host to container and installed all dependencies in terms of library.
  4. started application
  5. commit container by container name
  6. tag image by using image id through docker tag
  7. saved image in .tar through docker save

Here i want to generate Dockerfile for above mentioned, kindly help.


No this is not possible, docker dosnt know which commands you executed in the container.
So you need to write the Dockerfile yourself.

Thanks Martin,

I was under assumption that run container through base image and install all dependencies w.r.t to application and once all is tweaked then prepare Dockerfile from that, as you confirmed it’s not possible hence what do you suggest for below.

  1. Best way is for creating container is through Dockerfile? or else run container from base image and install all dependencies w.r.t to application and then prepare Dockerfile based on command executed inside container or else
  2. as I mentioned in my initial post follow approach i.e., run container from base image then tweak all parameter w.r.t app then commit, tag, save through docker save in .tar file and use that .tar file to run container in other machine.

the best way for creating IMAGES is though a Dockerfile, yes, because then you have a recipe you can change if something needs updating, or you’re deploying a new version of your app, just re-run the dockerfile with the new version and then you only have the necessary componenets in your image, without any bloat.

and the proccess of creating a dockerfile is very straight-forward, once you know the syntax its very easy.

once you then have build your image, push it to a repo like docker hub and let your users pull it that way

There’s a few things you could do to give yourself a head start. Using your base image :-

  1. Use the docker history command to see what commands were used and copy those into your own Dockerfile. Then add additional commands as needed.

  2. Many base images have an associated Git repo which will contain the Dockerfile used to create the image.

  3. Use a tool such as Dive to examine the docker image and continue as per (1).

Thanks, let me check over this. Also just to cross check if I needs to link multi-container as all are dependent to each other for running application then I need to use docker-compose? or else any other mechanism is there.

Yes, unless you have more complex orchestration needs (in which case I would recommend K8s) then docker-compose is suitable for multi-container applications. Don’t use the links construct though, that has been deprecated for a while. If you have dependencies between compose services you can use depends_on which will ensure that a dependent service is running before launching the one that depends on it (its basically a health check that impacts start-up order). In terms of inter container communication, that is enabled via networking. You can either declare an explicit external network or use the implicit default networking enabled by default (depends what n/w characteristics you need).



COPY command in dockerbuild file is copying content inside folder from host to container, need to copy whole folder, below is extract from docker file.
COPY folder1 /usr/local/lib64/

Google is your friend :