23

I have a docker container running

> docker container ls
                                                                                                                         
CONTAINER ID  IMAGE   COMMAND  CREATED         STATUS         PORTS  NAMES
c5a24953e383  gradle  "bash"   22 minutes ago  Up 22 minutes  #      naughty_torvalds

Can I duplicate this running container and run it? What is the command for it?

Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
Mahabub
  • 511
  • 2
  • 6
  • 16

2 Answers2

48

You can create a new image from that container using the docker commit command:

docker commit c5a24953e383 newimagename

And then start a new container from that image:

docker run [...same arguments as the other one...] newimagename
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Unfortunately this does not copy volumes – Sych Jan 18 '23 at 11:10
  • That’s true, but the question wasn’t about copying volumes. The easiest way to do that is probably to mount the old and new volumes in a container and use `tar`. I suspect that process may be addressed in more detail elsewhere on this site, but if not you can open a new question. – larsks Jan 18 '23 at 11:28
  • but this does not copy things I have created in the running container, does it? – KansaiRobot Apr 22 '23 at 05:22
  • It does because of the `docker commit` in the first step. – larsks Apr 22 '23 at 12:14
0

You can use:

docker run --name duplicateImage --volumes-from Image -d -p 3000:80 nginix:latest

The --volumes-from Image duplicates the 'Image' container.

So you will now have a container named Image and a container named duplicateImage and they will contain the same image that is running (a container).

rmobis
  • 26,129
  • 8
  • 64
  • 65
  • 1
    the argument is `--volumes-from` not `--volumes -from`. – Mohammad Faisal Jan 11 '21 at 14:50
  • I tried this method to get a new container with the same Python dependencies installed. However, the duplicated container comes up without those dependencies. – sbmthakur May 24 '22 at 16:18
  • @sbmthakur this may have happened because this command duplicates an **image**, not the **container** itself. The original image didn't have these dependencies and while `--volumes-from` duplicates the volumes from the container, these dependencies may have been installed on the OS folder. – Ricardo Jun 02 '22 at 02:10