Search code examples
dockercontainers

how to use the same container which was exited in docker


I had created a lot of containers. I am new to this docker container system. I do a docker ps -a then I get following result

debian@osboxes:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                       PORTS               NAMES
a6a62181dcf0        python              "/bin/bash"              About a minute ago   Exited (127) 8 seconds ago                       try1
ed5ef0b8155d        1d1a162a72a6        "/bin/bash"              22 hours ago         Exited (0) 22 hours ago                          pedantic_ellis
19a4eff3b5e5        1d1a162a72a6        "/bin/bash"              22 hours ago         Exited (0) 22 hours ago                          assignment4
30933891f08c        eeadc22d21a9        "python3"                22 hours ago         Exited (0) 22 hours ago                          python
ccdab94fd32f        1d1a162a72a6        "/bin/bash"              28 hours ago         Exited (0) 24 hours ago                          confident_wu
ce462ecfc5f2        1d1a162a72a6        "/bin/bash -v /home/…"   28 hours ago         Exited (127) 28 hours ago                        stupefied_grothendieck
6123f134934c        1d1a162a72a6        "/bin/bash"              28 hours ago         Exited (1) 28 hours ago                          stupefied_taussig
0ed23a8112a4        1d1a162a72a6        "/bin/bash"              29 hours ago         Exited (0) 29 hours ago                          vigilant_bartik
c343731b7cde        1d1a162a72a6        "/bin/bash"              30 hours ago         Exited (0) 29 hours ago                          gallant_ardinghelli
2f95d3b4c1b8        1d1a162a72a6        "/bin/bash"              30 hours ago         Created                                          nice_hermann
5ebe9f18c744        1d1a162a72a6        "/bin/bash"              46 hours ago         Created                                          pensive_easley
c1b43edfafb9        1d1a162a72a6        "/bin/bash"              46 hours ago         Exited (1) 22 hours ago                          adoring_williams
42dea69d1d4e        1d1a162a72a6        "/bin/bash"              46 hours ago         Created                                          funny_austin
6f736902e650        1d1a162a72a6        "/bin/bash"              46 hours ago         Exited (1) 46 hours ago                          strange_ride
09306e5ec5d1        1d1a162a72a6        "--name=kaushik"         2 days ago           Created                                          pensive_shtern
699fb2a23e1c        1d1a162a72a6        "--name=kaushik"         2 days ago           Created                                          sharp_feistel
9f7b29ab512e        1d1a162a72a6        "--name=kaushik"         2 days ago           Created                                          elastic_payne2
25bfc74fab3b        1d1a162a72a6        "/bin/bash"              2 days ago           Exited (1) 2 days ago                            festive_einstein
e658dd320297        1d1a162a72a6        "/bin/bash"              2 days ago           Exited (255) 2 minutes ago                       objective_napier
ebae378d9152        1d1a162a72a6        "/bin/bash"              2 days ago           Exited (1) 2 days ago                            brave_ritchie
23c7f4293b30        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (0) 2 days ago                            suspicious_lumiere
5090081f6809        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (1) 2 days ago                            quizzical_keller
425d59be9cbf        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (0) 2 days ago                            distracted_lederberg
11c55ce7f011        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          elastic_noyce
1ccaf0477995        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          mystifying_tu
62528115f4b7        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          determined_meninsky
fca64af2f595        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          elastic_goldwasser
eecb3153bded        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          cool_cray
30b6d61fcac9        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (1) 2 days ago                            quirky_kapitsa
cf992a8b8286        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          hungry_goldstine
0f9a951f7593        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          crazy_wright
e25dcf8a8be8        hadoop-build-1001   "/bin/bash"              2 days ago           Created                                          bold_pasteur
73d068e0d756        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (0) 2 days ago                            condescending_goodall
adda325294cd        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (0) 2 days ago                            serene_wilson
75a9a3262505        hadoop-build-1001   "/bin/bash"              2 days ago           Exited (0) 2 days ago                            hardcore_khorana
e38726a74e9b        hadoop-build-1001   "/bin/bash"              3 days ago           Exited (255) 2 days ago                          beautiful_clarke
4060dbc85d2d        hadoop-build-1001   "/bin/bash"              3 days ago           Exited (0) 3 days ago                            strange_yonath
174509213b30        hadoop-build-1001   "/bin/bash"              8 days ago           Exited (255) 7 days ago                          hadoop-c
fa82c595e214        1d1a162a72a6        "/bin/bash"              8 days ago           Exited (0) 8 days ago                            agitated_edison
4b07fcc45271        python              "python3"                2 weeks ago          Exited (255) 8 days ago                          pyC
1cc06f213eb7        abee520343a4        "/bin/sh -c 'apt-get…"   3 months ago         Exited (100) 3 months ago                        compassionate_kowalevski
8c1eb67f7325        1b1c7b120b48        "/bin/sh -c 'cd /opt…"   3 months ago         Exited (255) 3 months ago                        dreamy_jepsen
1406d7476a28        1180f37ef8b1        "/bin/sh -c 'mkdir -…"   3 months ago         Exited (35) 3 months ago                         upbeat_taussig
e88bcf7743e2        f0f5acc11f91        "/bin/sh -c 'apt-get…"   3 months ago         Exited (100) 3 months ago                        tender_cohen

I had made some changes in a container which was created using one of hadoop image as below

debian@osboxes:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              latest              eeadc22d21a9        2 weeks ago         933MB
hadoop-build-1001   latest              1d1a162a72a6        3 months ago        2.02GB
hadoop-build        latest              5c1480006f78        3 months ago        1.96GB
ubuntu              xenial              5f2bf26e3524        3 months ago        123MB

I powered off the machine and rebooted etc. I want to know that I made changes to one of the containers say in above output of docker ps -a the container was

25bfc74fab3b        1d1a162a72a6        "/bin/bash"              2 days ago           Exited (1) 

how can after a reboot etc I connect or start the same container again. What I am currently doing is

docker run -it 1d1a162a72a6 /bin/bash

I make some changes and run a few python programms. But after a reboot all the changes are gone that means those python files which I had edited I have to copy paste them again in the container and do every thing from scratch.I have to install vim each time and then each time I do apt-get update install softwares and then check edit the programe. I am not able to understand how to go back to same container which was created moments ago before exiting. What mistake I am doing here?


Solution

  • docker run always starts a new container. You can re-attach to the existing container provided it wasn't started with --rm using docker start:

    docker start -ai <CONTAINER_ID>
    

    Notice that you should use container ID, not image ID as a parameter to docker start. See more in Docker help:

    $ docker start --help
    
    Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]
    
    Start one or more stopped containers
    
    Options:
      -a, --attach               Attach STDOUT/STDERR and forward signals
          --detach-keys string   Override the key sequence for detaching a container
      -i, --interactive          Attach container's STDIN