Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop/relaunch docker container without losing the changes?

I did the following and lost all the changed data in my Docker container.

  1. docker build -t <name:tag> .
  2. docker run *-p 8080:80* --name <container_name> <name:tag>
  3. docker exec (import and process some files, launch a server to host them)

Then I wanted to run it on a different port. docker stop & docker run does not work. Instead I did

  1. docker stop
  2. docker rm <container_name>
  3. docker run (same parameters as before)

After the restart I saw the changes that happened in the container at 1-3 had disappeared, and had to re-run the import.

How do I do this correctly next time?

like image 306
culebrón Avatar asked Dec 09 '16 18:12

culebrón


1 Answers

what you have to do is build the image from the container you just stopped after making changes. Because your old command still using the old image which doesn't have new changes(you have made changes in container which you just stopped not in image )

docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

docker commit -a me new_nginx myrepo/nginx:latest then you can start container with the new image you just built

but if you dont want create image with the changes you made(like you dont want to put config containing password in the image) you can use volume mount

docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py

This command mounts the host directory, /src/webapp, into the container at /webapp. If the path /webapp already exists inside the container’s image, the /src/webapp mount overlays but does not remove the pre-existing content. Once the mount is removed, the content is accessible again. This is consistent with the expected behavior of the mount command.

Manage data in containers

like image 180
Deepak Avatar answered Nov 16 '22 03:11

Deepak