Docker
-
run - start a container
docker run nginx
If docker image is not present in the host, it will pull the image from docker hub. -
ps - list all containers
docker ps
Lists all containers that are currently running. -
ps -a : list all containers that are and were running.
docker ps -a
Lists all containers that were running in the past -
stop - to stop a running container
docker stop silly_samet
we must either mention its docker ID, or its docker name, given by docker
On success, we see the name/ID printed on the console. -
rm - remove a docker container
docker rm silly_samet
To remove a docker container. On success it prints the name of the container. -
images - list of docker images
docker images
Lists all the images in the machine -
rmi - removes an image
docker rmi nginx
Note, we mention the service/tool's name and not container name/ID -
pull - it pulls the image without running it
docker pull nginx
It pulls the image from dockerhub without running them in the host machine. We should then usedocker runto get the image up and running. -
docker run ubuntu sleep 5
To append a command to the container, we use the aforementioned syntax.
The command runs and the container's process is halted, therefore after the execution of the command,psdoesn't have the container listed under it. -
docker exec silly_sammet cat /etc/hosts
To execute a command on a running container, we the exec command. -
docker attach and detach
docker run nginx
If you run a command like the above the terminal is occupied by the process/container that is running, and you cannot use the terminal. The terminal is used as stdout for the container. To run the container in detach mode, use the following command:
docker run -d nginxTo attach a terminal the container use command:
docker attach a043d
And obviouslya043dbeing the ID of the container. -
docker run
-itcentos bash
The-itflag is used to allocate a pseudo-TTY and keep STDIN open, allowing you to interact with the container's command line. This command opens a new interactive shell (/bin/bash) within the specified container (container_name), providing a way to run commands and perform actions inside the running container. -
Use
docker exec -itfor a more controlled and interactive shell session within a running container, which is especially useful when you want to run specific commands or perform tasks. -
Use
docker attachwhen you simply want to attach your terminal to the primary process of a running container, but be aware of the potential drawbacks if the main process exits abruptly. -
tag: to mention the version of a container
docker run redis:4.2
By default it pulls thelatest.