รวม Docker Command ที่ใช้งานบ่อยๆ

Docker machine

Create Docker machine

$ docker-machine create [arg]
- [arg]: docker machine name

Ex.

$ docker-machine create dev

Start Docker machine

$ docker-machine start [arg]
- [arg]: docker machine name

Ex.

$ docker-machine start dev

Restart Docker machine

$ docker-machine restart [arg]
- [arg]: docker machine name

Ex.

$ docker-machine restart dev

Docker machine Status

$ docker-machine status [arg]
- [arg]: docker machine name

Ex.

$ docker-machine status dev

Stop Docker machine

$ docker-machine stop [arg]
- [arg]: docker machine name

Ex.

$ docker-machine stop dev

Remove Docker machine

$ docker-machine rm [option] [arg]
- [arg]: docker machine name
- [option]:
--force, -f force remove docker machine
-y auto accept remove docker

Ex.

$ docker-machine rm -f dev
$ docker-machine rm --force dev
$ docker-machine rm -f -y dev

Docker Images

List Images

$ docker images [options] [repo[:tag]]
- [options]
--all, -a Default option for list all images
--digests Show images digests
--filter -f Filter output based on conditions provided
--format Pretty-print images using a Go template
--no-tunc Don’t truncate output
--quiet , -q Only show numeric IDs

Ex.

$ docker images [--all]
$ docker images microsoft/dotnet
$ docker images microsoft/dotnet:2.1.3-runtime

Remove image

$ docker rmi [hash id/ image id]

Ex.

$ docker rmi 8bf5d34

Remove all unused images

$ docker image prune

Build image

$ docker build [OPTIONS] PATH | URL | -
- [option]:
--file, -f docker file name
--label Image label
--no-cache Do not use cache when building the image
--tag, -t Set image tag etc. repository:tag

Ex.

$ docker build -t demo:1.2 .  
//build with default Dockerfile name
$ docker build -t demo:1.2 -f DemoDockerfile . 
//build with custom Dockerfile name

Pull image

$ docker pull [Repository[:tag]]

Ex.

$ docker pull microsoft/dotnet
//pull with lasted image
$ docker pull microsoft/dotnet:2.1.3-runtime
//pull with specific tag

Container

List all running containers

$ docker ps

List all containers

$ docker ps -a

Run / Create containers

$ docker run [options] image [command] [arg]
- [arg]: docker machine name
- [options]
--detach , -d Run container in background
--env , -e Set container enviroment
--env-file Set container enviroment from file
--expose Export container port or port ranges
--interactive , -i Keep STDIN open even if not attached
--publish , -p Publish a container’s port(s) to the host
--tty , -t Allocate a pseudo-TTY
--restart [no] Restart policy when a container exits

Ex.

$ docker run -it --name demo -p 80:999 -e "tile=test" demo:1.2

Remove container

$ docker rm [hash id/ image id]

Ex.

$ docker rm 1d324ef

Stop Container

$ docker stop [hash id/ image id]

Ex.

$ docker stop 45ecd3d5

Container logs

$ docker logs [hash id/ image id]

Ex.

$ docker logs 2daef341