CrashingBrain
CrashingBrain

Reputation: 35

Can I export an already existing docker container into a dockerfile?

I may have not followed best practices, I believe, and I am kinda trying to correct myself

I have created a container starting from the Rust docker image for a project. I have been using this container more like a small VM rather than a container. Through the docker dashboard I start it every morning and work on it, either by docker -it exec my-container bash or using docker exec from scripts. Since I created it from the Rust image, I have added users, installed crates and packages etc.

Is there a way to create a Dockerfile from an existing docker container, that will contain all this, so I can use it to create ephemeral copies of this container to run specific jobs?

Upvotes: 1

Views: 15041

Answers (2)

Ashok
Ashok

Reputation: 3611

docker export [OPTIONS] CONTAINER

It'll export a container’s filesystem as a tar archive.

Note: The docker export command does not export the contents of volumes associated with the container. If a volume is mounted on top of an existing directory in the container, docker export will export the contents of the underlying directory, not the contents of the volume.

  1. sudo docker ps -aqf "name=containername" or docker inspect --format="{{.Id}}" container_name
  2. $ docker export CONTAINERID > <your_exported_image>.tar
  3. $ docker import <your_exported_image>.tar your-new-image:latest
  4. $ docker commit CONTAINERID your-new-image:latest

Upvotes: 0

amiasato
amiasato

Reputation: 1020

No, you can't. Since Docker doesn't keep track of your executed commands in interactive mode, it cannot generate a Dockerfile with your history. What you can do, though, is use docker commit to save your changes as an image, or docker export to save your container as a tarball which can be later transformed in an image with the docker import command. The whole process follows:

  • Get the CONTAINER ID from the container you wish to export with the docker ps command.
  • If you wish to export the container:
    • Export you container with $ docker export CONTAINERID > img.tar
    • Import the container as an image with $ docker import img.tar my-new-image:latest
  • If you wish to only save your changes as a new image:
    • Run $ docker commit CONTAINERID my-new-image:latest

Upvotes: 9

Related Questions