Skip to content

Instantly share code, notes, and snippets.

@vheidari
Last active April 3, 2020 23:35
Show Gist options
  • Save vheidari/4f844acf348c196a1b82917bad134e59 to your computer and use it in GitHub Desktop.
Save vheidari/4f844acf348c196a1b82917bad134e59 to your computer and use it in GitHub Desktop.
Quick docker command with example
download an image from docker hub :
docker pull hello-world
-------------------------------------
running or creating a process or container from an image:
docker run nginx
-------------------------------------
show list of process:
docker ps
show list of latests process with -a [archive] switch
docker ps -a
-------------------------------------
stop a docker ps or container :
docker stop [ container_id ]
ex: docker stop f6e1331ae05c
-------------------------------------
starting a stopped docker container:
docker start [ container_id or container_name]
ex: docker start f6e1331ae05c
-------------------------------------
removing a docker container :
docker rm [ container_id ]
ex: docker rm f6e1331ae05c
note: before removing a container must stop it
-------------------------------------
removing all docker container in archive:
show all container in archive :
docker ps -a
remove all container in archive :
docker container prune
--------------------------------------
show all pulled image on host machine :
docker images
-------------------------------------
removing an image from host machine:
docker rmi [image_id]
ex: docker rmi fce289e99eb9
-------------------------------------
running a container from images list with tag :
docker run [ image_name ]: [ image_tag ]
-------------------------------------
running a container and set a name for there :
docker run --name [ some_name ] [ image_name ]:[ tag ]
ex: docker run --name some_name_cenots centos:latests
------------------------------------
running a container and connect inside there :
docker run --name [ some_name ] -i -t [ image_name ]
ex : docker run --name ng_loadbalancer -i -t nginx:latest
-------------------------------------
running a stoped container with start command :
docker container start [container_id]
ex: docker container start fce2523e21315a
-------------------------------------
running a stoped container and executing a command inside container :
docker start [container_id] or [ container_name ]
ex : docker start 1f7fa5c3e773
ex : docker start focused_carson
-------------------------------------
running a command inside a running container :
docker container exec [ container_id ] [ linux_command]
ex : docker container exec dff82422ecea ls -la /etc/
ex : docker container exec dff82422ecea bash
ex : docker container exec dff82422ecea systemctl status nginx
ex : docker container exec dff82422ecea yum -y install nano
-------------------------------------
attach to a container with attach command :
docker container attach [ container_id ] or [ container_name ]
ex: docker container attach dff82422ecea
-------------------------------------
create an image from a container :
docker commit [ container_id ] [ image_name ]:[ tag ]
-------------------------------------
running a container with detach command :
docker run --name [ some_name ] -d [ image_name ]
ex : docker run --name ng_web_server -d nginx
-------------------------------------
stop all container with Linux bash command trick :
docker stop $(docker ps -q)
| |-------------> this command return all running container id
|-> and this command stoped all container with them id. for more information : docker ps -q --help
ex: docker stop $(docker ps -q)
-------------------------------------
starting all container in history archive with Linux bash command :
docker start $(docker ps -a -q)
ex: docker start $(docker ps -a -q)
-------------------------------------
removing all container stopped. at history or archive :
docker rm $(docker ps -a -q)
-------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment