How to work with Single Container Ubuntu image in Docker ?

As described in previous post “How to run Ubuntu 18.04 in docker container ?” once you have the docker image running for first time using,

$ sudo docker run -it 56def654ec22

Note: You don’t need to run this command always and for one docker instance this can be once in a lifetime of this instance and then just run “docker start CONTAINER_ID” to start the docker after you reboot the machine.

you will get an console and you can see the docker container ID getting created using below command,

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
97f8cfdb23d8        56def654ec22        "/bin/bash"         59 seconds ago      Up 54 seconds                           angry_ishizaka

so, here every time you exit and run the docker using command “docker run -it IMAGE ID” , you will get new container ID, but since we want to keep our server as optimsed as possible in terms of resource usage, its always better to create container only once and work with the same container afterwords..

So, once you have exited the docker container first time, you can see the all current docker containers using command,

$ sudo docker ps -a
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
49db5136dac8        56def654ec22        "/bin/bash"         3 minutes ago       Up 3 minutes                                    distracted_hamilton

Now, when you want to work with same container again for next times, just start the container using below command,

$ sudo docker exec -i -t 49db5136dac8 bash
root@49db5136dac8:/#

Note: If you have rebooted your machine, then before running above command, You will need to start the docker instance as, “docker start CONTAINER ID “

$ sudo docker start 49db5136dac8

and then run the docker exec command,

$ sudo docker exec -i -t 49db5136dac8 bash

and stop the same container using, “docker stop CONTAINER ID “

$ sudo docker stop 49db5136dac8

Leave a Comment