docker 常用命令
https://docs.docker.com/get-started/part2/#recap-and-cheat-sheet-optional
docker build -t friendlyhello . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry
eg
-
查看本地镜像
docker image ls [-a] # -a 查看所有
-
删除本地镜像
docker image rm <image id> # image id 为一串hash,不需要输完,输前几个字母即可,以下相同
-
删除本地所有镜像
docker image rm $(docker image ls -a -q)
-
查看本地容器
docker container ls [-a]
-
启动/停止容器
docker [container] start/stop <hash|name> # hash 一般只输入前两个字母即可,以下类似, name 为容器完整名称
-
删除容器
docker container rm <hash>
-
删除所有容器
docker container rm $(docker container ls -a -q)
-
进入指定容器的bash交互, 如对应镜像的hash为
a98850c27a67
,则输入docker exec -it a98 /bin/bash
即可
docker exec -it <hash|name> /bin/bash
-
执行 docker-compose.yml 文件
docker-compose up [-d] [--build]
# -d 以 deamon 模式启动
# --build 重新build后再启动