Docker - Container and Image
Docker Container
Docker container is a running instance of an image. You can use Command Line Interface (CLI) commands to run, start, stop, move, or delete a container. You can also provide configuration for the network and environment variables. Docker container is an isolated and secure application platform, but it can share and access to resources running in a different host or container.
Docker Image
An image is a read-only template with instructions for creating a Docker container. A docker image is described in text file called a Dockerfile, which has a simple, well-defined syntax.
Example
You can understand container and image with the help of the following command:
$ docker run hello-world
The above command docker run hello-world has three parts:
- docker: It is 
docker engineand used to rundocker program. It tells to the operating system that you are runningdocker program. - run: This subcommand is used to create and run a 
docker container. - hello-world: It is a 
name of an image. You need to specify the name of an image which is to load into thecontainer. 
Special commands to use daily
Pulling Image:
$ docker pull nginx- Used to pull an 
imagefrom thedocker hubandnginxis the name of theimage. 
- Used to pull an 
 Showing Image:
$ docker images ឬ $ docker image ls- Used to show all 
imagescontained in the local machine. 
- Used to show all 
 Running Container:
$ docker run nginx:latest- Used to run 
container, wherenginxis thename of the image, and thelatestis theversionof the image and must be associated with acolon(:)in front of the version. 
$ docker run -d nginx:latest- Used to run the 
containersame asdocker run nginx:latest, but this command is executed indetached mode, that is, run thecontainerand you can use theconsolewhere you arerunning the container normally, but if you use the above command, you cannot use theconsole. 
- Used to run 
 Showing Container:
$ docker container ls ឬ $ docker ps- Used to show all 
containersthat are running. 
$ docker ps -a- Used to show all 
containerswhether acontainerisrunningorstopped. 
$ docker ps -q- Used to show only the 
IDof thecontainerthat is running. 
$ docker ps -aq ឬ $ docker ps -a -q- Used to show only the 
IDof thecontainer, for bothrunningandstopped. 
$ docker ps --format="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"- Used to show all 
containersthat are running ineasy-to-viewformat. 
export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nImage\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n"- Used for 
export path variablesin the machine for easy retrieval next time without having to write it more. 
$ docker ps --format=$FORMAT- Used to show a 
containerthat isformattedby calling thepath variable. 
- Used to show all 
 Stop Container:
$ docker stop 19411e21c4fa- Used to stop a 
containerand19411e21c4fais thecontainer ID. 
$ docker stop boring_merkle- Used to stop the 
containersame as in the above command, butdocker stop 19411e21c4faabove use thecontainer ID, but this command uses thename of the container, however you can use any options. 
- Used to stop a 
 Exposing Port:
$ docker run -d -p 9000:80 nginx:latest- Used to run the 
containerindetached mode, and it includes with explicit specification with theexpose port, which is used to run thenginxprogram, and to specify theexpose port, and you need to use the-pflag, and then the port you want to place, and then thecolon(:)and then theportof thecontainer itself. 
$ docker run -d -p 9000:80 -p 9010:80 nginx:latest- Used to run the 
containersame as in the above command, but for this command you can usemultiple ports, ieport 9000is successful orport 9010is also successful. 
- Used to run the 
 Start Container:
$ docker start 19411e21c4fa ឬ $ docker start boring_merkle- Used to start an 
existing containerand you want to use the option start with thecontainer IDor use the start option with thecontainer name. 
- Used to start an 
 Removing Container:
$ docker rm 19411e21c4fa ឬ $ docker rm boring_merkle- Used to remove the 
container, and this can be removed using thecontainer IDor thecontainer name, and you can remove the container only if the container isstopped. 
$ docker rm -f 19411e21c4fa ឬ $ docker rm -f boring_merkle- Used to remove the 
containersame as in the above command, but this is inforce mode, which means removing the container without having to stop the container, and you can remove it using thecontainer IDorcontainer name. 
$ docker rm $(docker ps -aq)- Used to remove 
all containers, using thecontainer IDforall stopped containers. 
$ docker rm -f $(docker ps -aq)- Used to remove all 
containers, but you can delete both containers thatrunning and not running. 
- Used to remove the 
 Naming Container:
$ docker run -d --name nginx-ms -p 9000:80 nginx:latest- Used to assign a name to what you want to name using 
--nameflag, whilenginx-msis thecontainer name. 
- Used to assign a name to what you want to name using 
 Volumes:
$ docker run --name cambodia-website -d -p 9000:80 -v /home/mengsreang/Desktop/cambodia-website:/usr/share/nginx/html nginx:latest- Used to run a container with specific volumnes that connect the project contained in your computer to the nginx server in the docker. A little simplification: The 
-vflag represents volumnes, while/home/mengsreang/Desktop/cambodia-websiteis your project location on the computer, and/usr/share/nginx/htmlis the project location of the nginx server. And don't forget to put acolon(:)in front of it. 
$ docker exec -it cambodia-website bash- Used to create a bash to access a project on 
nginx server. Thatexeccommand represents execute, while the-itflag means execute bash ininteractive mode. 
- Used to run a container with specific volumnes that connect the project contained in your computer to the nginx server in the docker. A little simplification: The