-2

i'm trying to start up tomcat on my docker desktop,and i followed the official tomcat tutorial on docker hub.but somehow i found that docker will create a new container everytime after running the command:docker run -it --rm tomcat and delete the container automatically when tomcat shuts down.

i have already known the reason is that run --rm can automatically remove the container when it exits.

now i finally built webs on tomcat,and i don't want them to be vanished. how can i save my container before it's deleted?

thx! ;D

nikiru
  • 9
  • 2
  • Why do you need to do this? There shouldn't be anything important in the container; recreating the container as you describe is extremely routine. Also: why is Tomcat exiting? – David Maze Oct 09 '21 at 10:55

1 Answers1

0

Based on what I've found on the internet, remove the --rm flag is not possible currently. docker update gives you the ability to update some parameters after you start your container, but you cannot update the cleanup flag (--rm) according to the document.

References:

I started a docker container with --rm Is there an easy way to keep it, without redoing everything?

Cancel --rm option on running docker container

But some workaround can be applied. You can export your current container to an image, act as a checkpoint, then you can start a new container without the --rm flag, and based on the image you exported. You can use docker commit to do so:

docker commit [your container name/id] [repo/name:tag]

(Use docker ps to list your containers, do it in a new bash/cmd/PowerShell session, or you will lose your work when you exit your docker container)

Then start a new container without the --rm flag:

docker run -it [repo/name:tag]

Disclaimer:

In the production environment, you should never change the container by running bash or sh in it. Use Dockerfile and docker build instead. Dockerfile will give you a reproducible configuration even you delete your container. By design, the container should not have any important data (aka not persistent). Use the image and volumes to save your custom changes and configurations.

天空Blond
  • 24
  • 1
  • 1
  • thank you for your help! now i've set up a new container which have all my content in.the commands you gave are effective and convenient .thanks again. – nikiru Oct 09 '21 at 08:35
  • `docker commit` is almost never a best practice. Having committed your container, it's now essentially impossible to update the Tomcat inside, because you don't have the steps recorded to apply your custom changes on top of it. – David Maze Oct 09 '21 at 10:56
  • @DavidMaze Thanks for the comment, I added a disclaimer below my original answer to point out that containers are not persistent. The workaround is useful when you're new to docker and don't know how to build images. After several hours of searching on google and StackOverflow, you know you shouldn't make changes on the fly, and cannot preserve the container with the `--rm` flag, and should use `Dockerfile` to make reproducible changes. But you have already put a lot of time into that container. You might use Dockerfile next time, but you definitely don't want to lose your current work. – 天空Blond Oct 10 '21 at 11:04