文章目录
- docker基础命令使用
- 镜像基础命令
- 查看镜像:docker images
- 搜索镜像: docker search [image_name]
- 拉取镜像: docker pull [image_name]
- 删除镜像:docker rmi [image_name]
- 更新镜像: docker commit [-m=m -a=a container_id image_name]
- 设置镜像的标签:docker tag [image_id] [image_name:tag_name]
- 构建镜像: docker build -t=[image_name] [dockerfile/dockpath ]
- 查看镜像的历史信息:docker history [image_id]
- 容器基础命令
- 容器查询: docker ps
- 启动一个新的容器:dokcer run【image_name】 /bin/bash
- 启动已经停止的容器: docker start [container_id]
- 停止容器:docker stop [container_id]
- 重新启动容器: docker restart [container_id]
- 删除容器: docker rm -f [container_id]
- 进入容器: docker exec -it [container_id] /bin/bash
- 查看容器的端口:docker port [container_id]
- 查看容器的进程:docker top [container_id]
- 导出容器:docker export [container_id] > [file_zip]
- 导入容器: cat [file_zip] | docker import - [image_name]:[tag]
- 容器日志的查看
- 镜像的仓库
docker基础命令使用
镜像基础命令
查看镜像:docker images
返回列的名称
REPOSITORY: 镜像仓库的源
TAG: 镜像的标签
IMAGE ID: 镜像ID
CREATED: 创建时间
SIZE: 镜像大小
搜索镜像: docker search [image_name]
image_name:为镜像的变量名称
示例: docker search nginx
NAME: 镜像仓库的源名称
DESCRIPTION: 镜像的描述
STARS: 点赞数或喜欢数量
OFFICAL: 是否官方发布的
AUTOMATED: 是否自动构建
拉取镜像: docker pull [image_name]
image_name:为镜像的变量名称
示例: docker pull nginx
删除镜像:docker rmi [image_name]
image_name:为镜像的变量名称
示例: docker rmi nginx
更新镜像: docker commit [-m=m -a=a container_id image_name]
ps: 如果需要更新镜像,则必须要运行一个容器,有对应的容器id才可以进行更新
m : 提交的描述信息
a: 作者
container_id: 容器的id
image_name: 镜像的名称
示例: docker commit -m=‘新的镜像’ -a=‘xuwei’ 1abe8591e88f hello-world-xuwei
设置镜像的标签:docker tag [image_id] [image_name:tag_name]
image_id: 镜像id
image_name: 镜像名称
tag_name: 标签名称
示例: docker tag b49ad61205e4 hello-world-xuwei:v1.0
构建镜像: docker build -t=[image_name] [dockerfile/dockpath ]
dockerfile 为自己创建的一个dockerfile文件,后续会详细介绍
image_name: 为指定的镜像名称
dockerfile/dockpath: 为dockerfile的地址或目录
示例:docker build -t hello_world-test1 /usr/dockfile
查看镜像的历史信息:docker history [image_id]
image_id:镜像id
容器基础命令
容器查询: docker ps
查询当前运行的容器实例
CONTAINER ID:容器ID
IMAGE:镜像名称
COMMAND : 启动容器描述
CREATED :创建时间
STATUS :容器的状态
– created: 已创建
– restarting:重启中
–running/UP: 运行中
– removing: 迁移中
–paused:暂停
– exited: 退出
–dead: 死亡
PORTS: 端口号
NAMES: 容器的名称
docker ps -a
查询所有的容器示例
启动一个新的容器:dokcer run【image_name】 /bin/bash
-i :交互式操作
-t: 终端
image_name: 镜像名称
/bin/bash: 交互式终端
docker run 参数说明
-d :则启动的时候不会进入容器终端,后台运行,并返回容器id
-it :以交互模式运行
–name=‘XX’: 容器的名称
-p :指定端口映射
-P :随机端口
-v:目录挂载
常见的格式:
# docker run -it -v /宿主机目录:/容器目录 镜像名 /bin/bash
# docker run -P -d nginx:latest
# docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
# docker run --name mynginx -d nginx:latest
启动已经停止的容器: docker start [container_id]
container_id: 容器的id
示例: docker start
停止容器:docker stop [container_id]
container_id: 容器id
示例: docker stop 94b83b7f1336
重新启动容器: docker restart [container_id]
container_id: 容器id
示例: docker restart 94b83b7f1336
删除容器: docker rm -f [container_id]
container_id: 容器id
示例: docker rm -f cfa2915e0fe1
进入容器: docker exec -it [container_id] /bin/bash
container_id: 容器id
-it 为交互式操作终端
bin/bash:交互式shell
示例: docker exec -it 94b83b7f1336 /bin/bash
查看容器的端口:docker port [container_id]
container_id: 容器id
查看容器的进程:docker top [container_id]
container_id: 容器id
导出容器:docker export [container_id] > [file_zip]
contianer_id: 容器id
file_zip: 需要生成的压缩包名称
示例: docker export 0c95b0ff6671 > hello.zip
导入容器: cat [file_zip] | docker import - [image_name]:[tag]
file_zip: 生成容器以后的压缩包
image_name:镜像的名称
tag:镜像的标签
ps:实际过程是生成一个新的镜像id,用来运行容器
示例:cat docker hello.zip | docker import - hello_world:v1
容器日志的查看
查看近段时间的日志:–since
示例:docker logs --since 30m [CONTAINER_ID] #查看近30分钟的日志
container_id: 容器id
查看指定时间的日志:-t --since
docker logs -t --since=“2018-02-08” CONTAINER_ID #查看2018-02-08号的日志
container_id: 容器id
查看实时日志: -f
docker logs -f [CONTAINER_ID]
container_id: 容器id
查看指定当前X行的日志:–tail
docker logs --tail= [CONTAINER_ID]
container_id: 容器id
镜像的仓库
ps:使用docker的集中仓库,需要在docker官网注册一个docker的账号: https://hub.docker.com
登录镜像仓库:docker login
示例: docker login
登出镜像仓库:docker logout
示例: docker logout
上传镜像: docker push【image:tag】
示例: docker push 【image:tag】
image: 镜像的名称
tag:镜像的标签
拉取镜像: docker pull 【image:】
image:镜像的名称