Create, List & Delete Docker Containers on Linux
Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux.Docker uses the resource isolation
features of the Linux kernel such as cgroups and kernel namespaces, and a union-capable filesystem such as aufs and others to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines.
Launch Docker Container
To launch a new Docker container using below command. This will start a new container and provide you access of that container with /bin/bash shell.
# docker run [OPTIONS] <IMAGE NAME> [COMMAND] [ARG...]
For example below command will create new docker container using image named "ubuntu". To list all available images use docker images command.
# docker run -i -t ubuntu /bin/bash
To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console. If you used exit command, it will stop the current container. Click here to read for more options about docker run command.
List Docker Containers
After existing from Docker container, execute below command to list all running containers.
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES g25536g95fgd ubuntu "/bin/bash" 1 hours ago Up 1 hours first_ubuntu
By default Above command will list only running containers. To list all containers (including stopped container) use following command.
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES g25536g95fgd ubuntu "/bin/bash" 1 hours ago Up 1 hours first_ubuntu j555io5jkdkd centos "/bin/bash" 1 days ago Exited (0) 24 hours ago ubuntu-web
You can start, stop or attach to any containers with following commands. To start container use following command.
# docker start <CONTAINER ID|NAME>
# docker stop <CONTAINER ID|NAME>
# docker attach <CONTAINER ID|NAME>
Drop Docker Container
Before deleting any container make sure that container is stopped. You can use ‘docker ps -a’ command to list status of containers. If container is still running first stop that container using given commands in above step.
Now use following command to delete single or multiple containers use following command.
# docker rm <CONTAINER ID|NAME> <CONTAINER ID|NAME>
You can also delete all stopped containers at once using following command.
# docker rm $(docker ps -a -q)