docker是把应用程序和器依赖打包在image文件里面,只有经过这个镜像文件才能生成docker容器。一个image文件能够生成多个容器实例。docker
# 下载hello-world镜像 docker pull hello-world # 运行hello-world镜像,产生容器实例 # 运行一个镜像,若是这个镜像不存在,自动下载 docker run hello-world # 导出系统当前的docker镜像 docker save centos > /opt/centos.tar.gz # 导入一个docker镜像 docker load < /opt/centos.tar.gz # 运行centos镜像,而且以交互式的形式,进入centos容器当中 docker run -it centos /bin/bash -i 交互式操做 -t 开启一个终端提供访问 centos 镜像名 /bin/bash 指定容器运行shell解释器 # 运行一个ubuntu容器 docker run -it ubuntu /bin/bash docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done" -d 后台运行容器,返回容器ID -c "while true; do echo hello world; sleep 1; done" 后台运行一个shell脚本
删除镜像以前,须要先删除依赖于这个镜像的全部容器。shell
# 删除容器 docker rm 容器id # 删除镜像 docker rmi 镜像id # 强制性删除镜像,跳过容器记录 docker rmi -f 镜像id # 一次性删除全部容器记录,慎用!!!!! docker rm `dokcer ps -aq` # 一次性删除全部镜像记录,慎用!!!!! docker rmi `dokcer images -aq`
# 查询当前机器的镜像 docker image ls docker images # docker容器必须有后台进程在运行,不然容器就会挂掉。 # 查看正在运行的容器记录 docker container ls # 全部运行过的容器记录 docker ps -a # 查询docker镜像,默认去docker hub 搜索镜像 docker search hello-world # 实时打印容器内的日志 docker logs -f 容器id # 中止正在运行的容器记录 docker stop 容器id # 开启容器 docker start 容器id
提交本身的容器记录,产生新的镜像文件,能够发送给其余人使用。ubuntu
1. 运行一个centos容器记录,此时没有vim工具 docker run -it centos /bin/bash 2. 退出容器,提交这个容器记录 docker commit 容器id docker hub帐号/centos-vim 3. 检查容器镜像 docker images 4. 提交这个镜像到docker hub 1. 登陆到docker hub docker login 2. 登陆以后,推送这个镜像到docker hub docker push docker hub帐号/centos-vim 3. 推送到docker hub公网以后,就能够提供公网下载 docker pull docker hub帐号/centos-vim