Docker常用命令整理

友情提醒:本文最后更新于 1824 天前,文中所描述的信息可能已发生改变,请谨慎使用。

本文只是整理了一些docker的一些常用命令,至于docker的基础知识,建议有能力的同学可以阅读一下 官方文档,英文能力稍差的同学可以查看这里 gitbook,里面所讲的内容比较全面,本文不在赘述。

1. 查看版本信息

docker --version # 查看版本
docker-compose --version # 查看版本
docker-machine --version # 查看版本
docker version # 查看client和server端版本,并可以查看是否开启体验功能

2. 镜像操作

docker search <image-name>  # 搜索镜像
docker pull <image-name:tag>  # 拉取镜像
docker build -t <image-name:tag> . # 使用当前目录下的Dockerfile构建镜像,注意`.`是必须的
docker images  # 查看本地所有镜像
docker rmi [image-id/image-name]  # 删除指定的镜像,如docker rmi nginx
docker image rm $(docker image ls -a -q)  # 删除所有的镜像
docker tag <image> <username>/<repository>:<tag> # 为自定义的镜像打上tag。如:$docker tag hellopython followtry/demo:latest
docker push <username>/<repository>:<tag> # 将自定义的镜像发布到仓库。如:docker push followtry/demo:latest
docker run <image-name:tag>  # 运行镜像

附录:run参数详解

-a, --attach=[]            Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
  --add-host=[]              Add a custom host-to-IP mapping (host:ip)
  --blkio-weight=0            Block IO (relative weight), between 10 and 1000
  -c, --cpu-shares=0          CPU shares (relative weight)
  --cap-add=[]                Add Linux capabilities
  --cap-drop=[]              Drop Linux capabilities
  --cgroup-parent=            Optional parent cgroup for the container
  --cidfile=                  Write the container ID to the file
  --cpu-period=0              Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0              Limit the CPU CFS quota
  --cpuset-cpus=              CPUs in which to allow execution (0-3, 0,1) 绑定容器到指定CPU运行
  --cpuset-mems=              MEMs in which to allow execution (0-3, 0,1) 绑定容器到指定MEM运行
  -d, --detach=false          Run container in background and print container ID 后台运行容器,并返回容器ID
  --device=[]                Add a host device to the container
  --dns=[]                    Set custom DNS servers 指定容器使用的DNS服务器,默认和宿主一致
  --dns-search=[]            Set custom DNS search domains 指定容器DNS搜索域名,默认和宿主一致
  -e, --env=[]                Set environment variables 设置环境变量
  --entrypoint=              Overwrite the default ENTRYPOINT of the image
  --env-file=[]              Read in a file of environment variables 从指定文件读入环境变量
  --expose=[]                Expose a port or a range of ports
  -h, --hostname=            Container host name 指定容器的hostname
  --help=false                Print usage
  -i, --interactive=false    Keep STDIN open even if not attached 以交互模式运行容器,通常与 -t 同时使用
  --ipc=                      IPC namespace to use
  -l, --label=[]              Set meta data on a container
  --label-file=[]            Read in a line delimited file of labels
  --link=[]                  Add link to another container
  --log-driver=              Logging driver for container
  --log-opt=[]                Log driver options
  --lxc-conf=[]              Add custom lxc options
  -m, --memory=              Memory limit
  --mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --memory-swap=              Total memory (memory + swap), '-1' to disable swap
  --name=                    Assign a name to the container 为容器指定一个名称
  --net=bridge                Set the Network mode for the container  指定容器的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型
  --oom-kill-disable=false    Disable OOM Killer
  -P, --publish-all=false    Publish all exposed ports to random ports
  -p, --publish=[]            Publish a container's port(s) to the host
  --pid=                      PID namespace to use
  --privileged=false          Give extended privileges to this container
  --read-only=false          Mount the container's root filesystem as read only
  --restart=no                Restart policy to apply when a container exits
  --rm=false                  Automatically remove the container when it exits
  --security-opt=[]          Security Options
  --sig-proxy=true            Proxy received signals to the process
  -t, --tty=false            Allocate a pseudo-TTY 为容器重新分配一个伪输入终端,通常与 -i 同时使用
  -u, --user=                Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                Ulimit options
  --uts=                      UTS namespace to use
  -v, --volume=[]            Bind mount a volume
  --volumes-from=[]          Mount volumes from the specified container(s)
  -w, --workdir=              Working directory inside the container

3. 容器操作

docker container ls # 列出所有运行中的容器
docker container ls -a # 列出所有容器,包括未运行的
docker container ls -q     # 只列出运行的容器的id集合
docker container stop <hash>  # 优雅停用指定的容器
docker container kill <hash>  # 强制关闭指定的容器
docker container rm <hash>    # 删除指定的容器
docker container rm $(docker container ls -a -q) # 删除所有的容器
docker run -d -p 8080:80 --name webserver nginx # 运行nginx镜像实例,-d:后台,-p:绑定端口8080到docker的80
docker stop <containerid/container-name> # 停止容器webserver
docker start <containerid/container-name> # 启动容器webserver
docker port <containerid/container-name> # 查看指定容器的端口映射
docker logs -f <containerid/container-name> # 查看指定容器的日志
docker top <containerid/container-name>  # 查看容器的进程
docker inspect <containerid/container-name> # 检查容器的底层信息
docker rm <containerid/container-name> # 删除容器

4. 交互式进入容器

docker exec -it <containerName or containerID> bash
docker exec -i <containerName or containerID> bash
docker exec -t <containerName or containerID> bash
docker exec -d <containerName or containerID> bash
  • 只用 -i 参数,由于没有分配伪终端,看起来像pipe执行一样。但是执行结果、命令返回值都可以正确获取
  • 只用 -t 参数,则可以看到一个 console 窗口,但是执行命令会发现由于没有获得stdin的输出,无法看到命令执行情况
  • 使用 -it 时,则和我们平常操作 console 界面类似,而且也不会像attach方式因为退出,导致整个容器退出
  • 使用 -d 参数,在后台执行一个进程。如果一个命令需要长时间进程,会很快返回

5. Dockerfile介绍

http://www.dockerinfo.net/dockerfile%E4%BB%8B%E7%BB%8D

6. 附录docker-compose常用命令

http://wiki.jikexueyuan.com/project/docker-technology-and-combat/commands.html

相关链接:
https://segmentfault.com/a/1190000012063374
https://www.jianshu.com/p/adaa34795e64
http://www.dockerinfo.net/dockerfile%E4%BB%8B%E7%BB%8D

上一篇:Django中使用Q构建复杂查询

下一篇:为Django的manage.py添加自定义命令