What is a Docker Container
A running instance of an image is called a container. Docker launches them using the Docker images as read-only templates. If you start an image, you have a running container of this image. Naturally, you can have many running containers of the same image. We use the command “docker run” to run a container.
Listing Docker Containers
To list the containers, run the following command:
# docker ps [ OPTIONS ]
To list both running and stopped containers, use the -a option as follows:
# docker ps -a
here,
CONTAINER ID – Unique ID given to all the containers.
IMAGE – Base image from which the container has been started.
COMMAND – Command which was used when the container was started (default is /bin/bash, if you do not specify any command with “docker run”).
CREATED – Time at which the container was created.
STATUS – The current status of the container (Up or Exited).
PORTS – Port numbers if any, forwarded to the docker host for communicating with the external world.
NAMES – Dockers daemon names the cluster in some funny way. You can also specify your own name while spawning a container though.
To list only the container ID, use the -aq option.
# docker ps -qa eeae1186ea78 52249ba75f0f 709773bb7128
To list the last container created (running or stopped) :
# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eeae1186ea78 centos "/bin/bash" About a minute ago Up About a minute modest_hypatia
Starting a Docker Container
Use the below command to start a Docker container:
# docker run [ OPTIONS ] IMAGE[:TAG] [COMMAND] [ARG...]
For example :
# docker run -i -t --name=centos7_lab centos:latest /bin/bash
here,
-i : Starts the container in interactive mode.
-t : allocates a pseudo-tty and attaches it to the standard input
–name : User friendly name for the container. If the name is not specified, random string will be assigned as the container name.
When you run a docker command with -t, you would get attached to the container immediately and would see the command prompt changed. You can use the command “exit” to exit out from the container.
In all above examples, when you start the container, you are automatically logged into it. And when you exit the container, the container is stopped. You can also keep the container running even when you logout by using the -d option. For example :
# docker run -itd --name=geeklab centos:latest /bin/bash cae760f70ec4bd232891364824773c2a3cac8f7854261108c474d304e6a5c5fa
Here,
-d – Runs container in background and print container ID.
Stopping a Docker Container
You can stop one or more (all) containers at once. The syntax of the command to stop a docker container is :
docker stop [-t|--time[=10]] CONTAINER [CONTAINER...]
Here,
–time/-t is grace period to wait before stopping the container.
For Example, first check which container is running.
# docker ps -q eeae1186ea78
Now to stop the above container use the below command.
# docker stop eeae1186ea78 eeae1186ea78
To stop all the containers, run the below command:
# docker stop `docker ps -q`
Deleting a Docker container
To delete docker container use the syntax below:
# docker rm [ OPTIONS ] CONTAINER [ CONTAINER ]
For Example:
# docker rm eeae1186ea78 eeae1186ea78
To delete a container we need to first stop it. For example, if you try deleting a running container you would get an error as:
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cae760f70ec4 centos:latest "/bin/bash" 4 minutes ago Up 4 minutes geeklab
# docker rm cae760f70ec4 Error response from daemon: You cannot remove a running container cae760f70ec4bd232891364824773c2a3cac8f7854261108c474d304e6a5c5fa. Stop the container before a ttempting removal or force remove
So to delete the container, first stop it and then delete it.
# docker stop cae760f70ec4 cae760f70ec4
# docker rm cae760f70ec4 cae760f70ec4
To forcefully delete a container without stopping it, use the -f option.
# docker rm -f 18a1924e8499 18a1924e8499
To delete all the containers at once, first stop them all and then delete them.
# docker stop `docker ps -q` # docker rm `docker ps -aq`
How to install docker on CentOS / RHEL / Fedora