Docker
文章目录
- Docker
- 一、关于 Docker
- 安装 Docker
-
- 常见命令
- 查看Docker 磁盘使用情况
- 清理磁盘
- 停止Docker 服务
- 二、镜像
- 查看已安装镜像
- 拉取镜像
- 删除镜像
- 查找镜像
- 方式一:通过 Docker Hub
- 方式二:docker search 命令
- 加速服务(daocloud)
- 三、容器
- 查看容器
- 启动/停止容器
- 删除容器
- 检查容器的状态信息
- 查看容器内运行的进程
- 启动容器 run
- 进入容器并命令式交互
-
- 从容器拷贝文件到主机上
- 相关资料
一、关于 Docker
Docker 官网:https://www.docker.com
Github Docker 源码:https://github.com/docker/docker-ce
Docker 文档:https://docs.docker.com/reference/
安装 Docker
1、查看版本
安装之前,先看看是否安装,以及版本号
$ docker --version
Docker version 20.10.6, build 370c289
$ docker-compose --version
docker-compose version 1.29.1, build c34c88b2
$ docker version
Client:
Cloud integration: 1.0.14
Version: 20.10.6
API version: 1.41
Go version: go1.16.3
Git commit: 370c289
Built: Fri Apr 9 22:46:57 2021
OS/Arch: darwin/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:44:56 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2、安装
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615170320313.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM5MDk5OQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615170343821.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM5MDk5OQ==,size_16,color_FFFFFF,t_70)
$ docker cp repo:/git/getting-started/ .
$ cd getting-started
$ docker build -t docker101tutorial .
[+] Building 9.2s (6/24) [+] Building 332.0s (25/25) FINISHED
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
$ docker run -d -p 80:80 --name docker-tutorial docker101tutorial
8b17f9ba92e67e2d475db56345c23873129cf15f31688ba0ce2d860fe0d58a98
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:80: bind: address already in use.
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615170402332.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM5MDk5OQ==,size_16,color_FFFFFF,t_70)
3、卸载 Docker
常见命令
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container changes
cp Copy files/folders from the containers filesystem to the host path
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in an existing container
export Stream the contents of a container as a tar archive
history Show the history of an image
images List images
import Create a new filesystem image from the contents of a tarball
info Display system-wide information
inspect Return low-level information on a container
kill Kill a running container
load Load an image from a tar archive
login Register or Login to the docker registry server
logout Log out from a Docker registry server
logs Fetch the logs of a container
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
pause Pause all processes within a container
ps List containers
pull Pull an image or a repository from the docker registry server
push Push an image or a repository to the docker registry server
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image to a tar archive
search Search for an image on the Docker Hub
start Start a stopped containers
stop Stop a running containers
tag Tag an image into a repository
top Lookup the running processes of a container
unpause Unpause a paused container
version Show the docker version information
wait Block until a container stops, then print its exit code
查看Docker 磁盘使用情况
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 2 995.7MB 821.7MB (82%)
Containers 2 1 17.86MB 17.77MB (99%)
Local Volumes 1 0 10.32MB 10.32MB (100%)
Build Cache 48 0 402MB 402MB
清理磁盘
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]
删除 关闭的容器、无用的数据卷 和 网络无TAG的镜像
-a
参数 无用的镜像也会删除 (慎用)
停止Docker 服务
$ systemctl stop docker
二、镜像
查看已安装镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker101tutorial latest 20c870477397 21 hours ago 28MB
alpine/git latest 7660bb3edeb5 4 days ago 25.1MB
ubuntu latest 7e0aa2d69a15 4 weeks ago 72.7MB
Options:
-a
:列出本地所有镜像-q
:只显示镜像id-digests
:显示镜像的摘要信息--no-trunc
:显示完整的镜像信息
拉取镜像
以 ubuntu 为例
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
345e3491a907: Pull complete
57671312ef6f: Pull complete
5e9250ddb7d0: Pull complete
Digest: sha256:cf31af331f38d1d7158470e095b132acd126a7180a54f263d386da88eb681d93
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:adf73ca014822ad8237623d388cedf4d5346aa72c270c5acc01431cc93e18e2d
Status: Image is up to date for ubuntu:latest
docker.io/library/ubuntu:latest
拉取指定 tag 的 image
删除镜像
$ docker rmi ubuntu
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 5235a49aea61 is using its referenced image 7e0aa2d69a15
$ docker rmi 镜像名1:TAG 镜像名2:TAG
$ docker rmi -f $(docker images -qa)
查找镜像
方式一:通过 Docker Hub
从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615170438978.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTM5MDk5OQ==,size_16,color_FFFFFF,t_70)
方式二:docker search 命令
$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 12302 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 535 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 273 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 251 [OK]
OPTIONS:
-s
:列出收藏数不小于指定值的镜像--automated
:只列出 automated build 类型的镜像
标签说明
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本。
如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
加速服务(daocloud)
市面上有很多加速服务的提供商,如:DaoCloud,阿里云等。
直接访问docker hub比较慢。通过daocloud来访问稍微快一些。
在虚拟机浏览器里打开< http://www.daocloud.io/>,然后注册用户或者直接用微信扫二维码登录。登录后点击“镜像仓库”。
点击DockerHub镜像
,这里可以打开docker官方的镜像仓库,然后在里面搜索你要的镜像即可。
三、容器
创建容器会有单独的介绍
查看容器
最右侧 NAMES 列,对应的是容器的名字
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd05dd4f0b3d docker101tutorial "/docker-entrypoint.…" 2 minutes ago Exited (0) About a minute ago vibrant_franklin
8b17f9ba92e6 docker101tutorial "/docker-entrypoint.…" 16 hours ago Created docker-tutorial
f0ecd71a3a90 alpine/git "git clone https://g…" 21 hours ago Exited (0) 21 hours ago repo
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Options:
-a
:列出当前所有正在运行的容器+历史上运行过的-l
:显示最近创建的容器。-n
:显示最近n个创建的容器。-q
:静默模式,只显示容器编号。--no-trunc
:不截断输出。
启动/停止容器
$ docker start c001
c001
$ docker restart <容器 ID>
c001
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab16bcb55850 ubuntu "bash" 8 minutes ago Up 17 seconds c001
$ docker stop c001
c001
$ docker start -i c001
root@ab16bcb55850:/
exit
$ docker kill <容器 ID>/<容器 NAME>
使用 版本为15.10的ubuntu系统镜像 来运行容器时,命令如下:
$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/
删除容器
$ docker rm b83c32b59c67
docker rm -f tomcat1/containerid
$ docker rmi 7e0aa2d69a15
$ docker rm -f $(docker ps -a)
检查容器的状态信息
查看容器内部细节
$ docker inspect 容器名
查看容器内运行的进程
$ docker top 容器名
如:
$ docker top con4
Error response from daemon: Container bd9177d4ec977dcc5cb1920fd23a3e0c4 is not running
启动容器 run
$ docker run -d con4
$ docker logs -f -t --tail con4
进入容器并命令式交互
方式一:exec
$ docker exec -it c001 /bin/bash
在容器中打开终端,并且可以启动新的进程。
方式二:attach
重新进入
$ docker attach c001
直接进入容器,启动命令的终端,不会启动新的进程
从容器拷贝文件到主机上
$ docker cp 容器ID:容器内路径 目的主机路径
相关资料
- 知乎 慕课网:可能是把Docker的概念讲的最清楚的一篇文章
https://zhuanlan.zhihu.com/p/38552635 - 刀客传奇:Docker应用详细解析
https://www.jianshu.com/p/ddcea0e29a31 - 知乎十里:macOS 下使用 Docker 搭建 ubuntu 环境
https://zhuanlan.zhihu.com/p/59548929 - 博客园三只松鼠:Docker实践:python应用容器化
https://www.cnblogs.com/shenh/p/9518343.html - Compose 命令说明
https://wiki.jikexueyuan.com/project/docker-technology-and-combat/commands.html - 梁桂钊:30 分钟快速入门 Docker 教程
https://mp.weixin.qq.com/s/DY3pLat1ehudP624Io73Hg
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)