docker中的小技巧

这篇文章下的指令都是我在工作年中常用到的一些操纵docker中container的小技巧。

  1. 如果只是做短暂的容器实验,可以通过在命令中指定 --rm 来避免清理工作的负担。容器进入退出状态时,就会自动删除。
    例如,下面的命令会将一个新的busyboy 容器显示的消息写到屏幕上并在容器退出时立即被删除:
        docker run --rm --name auto-exit-test busybox:latest echo Hello World  

  1. 使用--read-only标志创建容器时,会将挂载的容器文件系统设置为只读,防止容器被修改

  2. 容器重启策略,即再容器创建时设置--restart标志,将有助于系统出现故障时进行自动恢复。

docker build -t friendlyname .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname         # 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
docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
原文地址:https://www.cnblogs.com/Ethan2lee/p/7471401.html