How to create, start, stop and delete the Docker containers ?

Create docker container with Ubuntu Image

Note: To create docker container, you need to make sure docker is installed on your machine by following “How to install Docker on Ubuntu 18.04 ?”

$ sudo docker create ubuntu:18.04

Once, you see docker container is installed, you can see its container id as,

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
49db5136dac8        ubuntu:18.04        "/bin/bash"         5 seconds ago       Created                                 distracted_hamilton

Note: for all below commands “CONTAINER ID” is required, in our case the container id is “49db5136dac8” as can be seen above, please change it to your container id.

Login to docker container shell

To login to docker container, we need to make sure we are running the docker, you can run the docker in background as,

$ sudo docker run -it  -d "ubuntu:18.04"

above command will run the docker in background and then you can access the shell as,

$ sudo docker exec -i -t 49db5136dac8 bash

Start the docker container

$ sudo docker start 49db5136dac8
49db5136dac8

once you start the container, you can see the same in “ps” command as,

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
49db5136dac8        ubuntu:18.04        "/bin/bash"         8 minutes ago       Up 8 minutes                            distracted_hamilton

Stop the docker container

$ sudo docker stop 49db5136dac8
49db5136dac8

Delete the docker container

$ sudo docker rm CONTAINER ID

here, you can check which are the all containers available on your machine using,

$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
49db5136dac8        56def654ec22        "/bin/bash"         15 minutes ago      Exited (0) 6 minutes ago                        distracted_hamilton
97f8cfdb23d8        56def654ec22        "/bin/bash"         17 minutes ago      Exited (0) 15 minutes ago                       angry_ishizaka
218a9b8c69ef        a4a181159e32        "/bin/bash"         2 hours ago         Exited (0) 19 minutes ago                       trusting_franklin

so, here we have 3 containers, of which if we want to delete “49db5136dac8” , we can use the command as,

$ sudo docker rm 49db5136dac8

Leave a Comment