Docker - Random

Pull repo from Docker hub:

docker pull ubuntu:latest

Run command on container and quit:

docker run --name disposable --rm -it disposable:16.04 cat /etc/issue

Run command on container on file mounted from current folder and quit:

docker run --name disposable -v $(pwd):/mnt/ --rm -it disposable:16.04 sh -c 'echo "test" > /mnt/test'

Run bash on container CMD+P CMD+Q to leave running:

docker run --name disposable -v $(pwd):/mnt/ --rm -it disposable:16.04 bash

Run container with systemctl daemon in background and launch bash on it:

docker run --rm -ti --name disposable -d --privileged=true -p 2222:22 disposable:16.04
docker exec -ti disposable bash

List Docker images:

docker images

List Docker running containers:

docker container ls

List all docker running and non-running containers:

docker container ls -a

Run an image as container:

docker run -it <image>:<tag>  --restart always [/bin/bash]

-d for daemon
–name for name
-p to expose port

Stop docker container gracefully:

docker stop <container id|name>

Stop all running containers:

docker stop $(docker ps -a -q)

Start docker container:

docker start <container id>

Start all containers:

docker start $(docker ps -a -q)

Kill container immediately:

docker kill <container id>

Output logs from docker container proxying stdout:

docker logs -f <container id>

Exec command on docker container:

docker exec -it /bin/bash

Search images in docker hub:

docker search <search term>

Change the name that has been randomly generated for the container:

docker rename <current_container_name> <new_container_name>

Take a peek inside our containers:

docker stats <container_name>

List of all running processes inside the container: docker top

Remove container:

docker rm <container>

-v to remove with volume

Remove all existing containers:

docker rm $(docker ps -a -q)

If some containers are still running as a daemon, use -f (force) param immediately after rm command.

Remove all containers with status=exited:

docker rm $(docker ps -q -f status=exited)

Remove image:

docker rmi <image>