Docker 安装和使用
参考文档
https://yeasy.gitbook.io/docker_practice/install/centos
https://blog.csdn.net/qq_41822345/article/details/107123141
https://www.exception.site/docker
docker 实战
https://yeasy.gitbook.io/docker_practice/
windows 安装 docker
https://blog.csdn.net/weixin_43411585/article/details/128107359
安装引擎-CentOS
https://docs.docker.com/engine/install/centos/#install-from-a-package
卸载旧docker
#1.卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
从rpm 存储库安装
#2.安装依赖包
yum install -y yum-utils
#3.1 在线安装
推荐使用国内的
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3.2 从 rpm 包安装
下载对应的rpm 包
https://download.docker.com/linux/centos/
上传到服务器
安装
rpm -ivh [包名]
或者 yum install xxx.rpm
# 启动
systemctl start docker
# 开机自启
systemctl enable docker
#6. 使用docker version查看是否安装成功
docker version
#7. 测试
docker run hello-world
安装指定 版本
#查看可用版本
yum list docker-ce --showduplicates | sort -r
安装指定版本
yum install docker-ce-19.03.9
rpm 离线安装
下载 rpm 包上传到服务器
https://download.docker.com/linux/centos/
# 将下面路径改为 docker 包的路径
yum install /path/to/package.rpm
安装 docker compose
compose 是一种容器管理技术,一键启动。
https://docs.docker.com/compose/install/compose-plugin/#installing-compose-on-linux-systems
下载安装包
https://github.com/docker/compose
把安装包上传到 /usr/local/bin 目录
重命名文件
mv docker-compose-linux-x86_64 docker-compose
设置二进制文件的权限
chmod +x /usr/local/bin/docker-compose
测试,查看版本
docker-compose --version
参考:
https://blog.csdn.net/hanxiaotongtong/article/details/125477514
https://www.cnblogs.com/ludangxin/p/15097035.html
https://blog.csdn.net/qq_41779565/article/details/126373801
卸载
#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!
更换镜像源
https://www.cnblogs.com/nhdlb/p/12567154.html
创建目录
mkdir -p /etc/docker
创建 /etc/docker/daemon.json 文件
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
重载配置
systemctl reload docker
https://blog.csdn.net/weixin_43584835/article/details/108192859
https://blog.csdn.net/weixin_39816740/article/details/111180884
命令
帮助
docker version #显示docker的版本信息。
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
#帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/
镜像
docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search 搜索镜像
docker pull 下载镜像 docker image pull
docker rmi 删除镜像 docker image rm
docker images 查看本地镜像
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 e73346bdf465 24 hours ago 448MB
# 解释
#REPOSITORY # 镜像的仓库源
#TAG # 镜像的标签
#IMAGE ID # 镜像的id
#CREATED # 镜像的创建时间
#SIZE # 镜像的大小
# 可选项
Options:
-a, --all Show all images (default hides intermediate images) #列出所有镜像
-q, --quiet Only show numeric IDs # 只显示镜像的id
➜ ~ docker images -aq #显示所有镜像的id
e73346bdf465
d03312117bb0
d03312117bb0
602e111c06b6
2869fc110bf7
470671670cac
bf756fb1ae65
5acf0e8da90b
docker pull 拉取镜像
# 下载镜像 docker pull 镜像名[:tag]
➜ ~ docker pull tomcat:8
8: Pulling from library/tomcat #如果不写tag,默认就是latest
90fe46dd8199: Already exists #分层下载: docker image 的核心 联合文件系统
35a4f1977689: Already exists
bbc37f14aded: Already exists
74e27dc593d4: Already exists
93a01fbfad7f: Already exists
1478df405869: Pull complete
64f0dd11682b: Pull complete
68ff4e050d11: Pull complete
f576086003cf: Pull complete
3b72593ce10e: Pull complete
Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df640a027 # 签名 防伪
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8 #真实地址
#等价于
docker pull tomcat:8
docker pull docker.io/library/tomcat:8
docker rmi 删除镜像
➜ ~ docker rmi -f 镜像id #删除指定的镜像
➜ ~ docker rmi -f 镜像id 镜像id 镜像id 镜像id#删除指定的镜像
➜ ~ docker rmi -f $(docker images -aq) #删除全部的镜像
查看镜像或容器元信息
docker inspect : 容器/镜像的id或name。
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
OPTIONS说明:
-f :指定返回值的模板文件。
-s :显示总的文件大小。
--type :为指定类型返回JSON。
容器
参考:
https://www.runoob.com/docker/docker-container-usage.html
docker run 镜像id 新建容器并启动
docker ps 列出运行的容器
docker ps -a 列出所有容器
docker rm 容器id 删除指定容器
docker start 容器id #启动容器
docker restart容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
说明:我们有了镜像才可以创建容器,Linux,下载centos镜像来学习
➜ ~ docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
新建容器并启动
docker run [可选参数] image | docker container run [可选参数] image
#参书说明
--name="Name" 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台运行
-it 打开终端交互
-p 指定容器的端口 -p 8080(宿主机):8080(容器)
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P(大写) 随机指定端口
-v /opt/nginx:/etc/nginx 主机目录:容器目录,主机目录会覆盖容器目录使用
--restart=always 跟随 docker 重启
--privileged=true 获取系统特权,root
参考:https://blog.csdn.net/weixin_39998006/article/details/99680522
# 测试、启动并进入容器
➜ ~ docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
8a29a15cefae: Already exists
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
[root@95039813da8d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@95039813da8d /]# exit #从容器退回主机
exit
➜ ~ ls
shell user.txt
进入容器
docker exec -it 0cd4d9d94de2 /bin/bash
将文件从容器拷贝到宿主机上
docker cp 容器id:容器内文件的路径 宿主机路径#实例
docker cp 0cd4d9d94de2:/Test.java /Test.java
列出运行的容器
#docker ps命令 #列出当前正在运行的容器
-a, --all Show all containers (default shows just running)
-n, --last int Show n last created containers (includes all states) (default -1)
-q, --quiet Only display numeric IDs
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68729e9654d4 portainer/portainer "/portainer" 14 hours ago Up About a minute 0.0.0.0:8088->9000/tcp funny_curie
d506a017e951 nginx "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:3344->80/tcp nginx01
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95039813da8d centos "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago condescending_pike
1e46a426a5ba tomcat "catalina.sh run" 11 minutes ago Exited (130) 9 minutes ago sweet_gould
14bc9334d1b2 bf756fb1ae65 "/hello" 3 hours ago Exited (0) 3 hours ago amazing_stonebraker
f10d60f473f5 bf756fb1ae65 "/hello" 3 hours ago Exited (0) 3 hours ago dreamy_germain
68729e9654d4 portainer/portainer "/portainer" 14 hours ago Up About a minute 0.0.0.0:8088->9000/tcp funny_curie
677cde5e4f1d elasticsearch "/docker-entrypoint.…" 15 hours ago Exited (143) 8 minutes ago elasticsearch
33eb3f70b4db tomcat "catalina.sh run" 15 hours ago Exited (143) 8 minutes ago tomcat01
d506a017e951 nginx "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:3344->80/tcp nginx01
24ce2db02e45 centos "/bin/bash" 16 hours ago Exited (0) 15 hours ago hopeful_faraday
42267d1ad80b bf756fb1ae65 "/hello" 16 hours ago Exited (0) 16 hours ago ecstatic_sutherland
➜ ~ docker ps -aq
95039813da8d
1e46a426a5ba
14bc9334d1b2
f10d60f473f5
68729e9654d4
677cde5e4f1d
33eb3f70b4db
d506a017e951
24ce2db02e45
42267d1ad80b
退出容器
exit #容器直接退出
ctrl +P +Q #容器不停止退出
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf
docker rm -f $(docker ps -aq) #删除指定的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
运行
后台启动命令
# 命令 docker run -d 镜像名
➜ ~ docker run -d centos
a8f922c255859622ac45ce3a535b7a0e8253329be4756ed6e32265d2dd2fac6c
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题docker ps. 发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs --help
Options:
--details Show extra details provided to logs
* -f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
* --tail string Number of lines to show from the end of the logs (default "all")
* -t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
➜ ~ docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done" #模拟日志
#显示日志
-tf #显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志
查看容器中进程信息
docker top 容器id
查看镜像的元数据
# 命令
docker inspect 容器id
#测试
➜ ~ docker inspect 55321bcae33d
[
{
"Id": "55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066",
"Created": "2020-05-15T05:22:05.515909071Z",
"Path": "/bin/sh",
...
}
]
➜ ~
进入容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bash
#测试
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55321bcae33d centos "/bin/sh -c 'while t…" 10 minutes ago Up 10 minutes bold_bell
a7215824a4db centos "/bin/sh -c 'while t…" 13 minutes ago Up 13 minutes zen_kepler
55a31b3f8613 centos "/bin/bash" 15 minutes ago Up 15 minutes lucid_clarke
➜ ~ docker exec -it 55321bcae33d /bin/bash
[root@55321bcae33d /]#
# 方式二
docker attach 容器id
#测试
docker attach 55321bcae33d
正在执行当前的代码...
区别
#docker exec #进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端
从容器内拷贝到主机上
docker cp 容器id:容器内路径 主机目的路径
#进入docker容器内部
➜ ~ docker exec -it 55321bcae33d /bin/bash
[root@55321bcae33d /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#新建一个文件
[root@55321bcae33d /]# echo "hello" > java.java
[root@55321bcae33d /]# cat java.java
hello
[root@55321bcae33d /]# exit
exit
➜ ~ docker cp 55321bcae33d:/java.java / #拷贝
➜ ~ cd /
➜ / ls #可以看见java.java存在
bin home lib mnt run sys vmlinuz
boot initrd.img lib64 opt sbin tmp vmlinuz.old
dev initrd.img.old lost+found proc srv usr wget-log
etc java.java media root swapfile var
教程:
https://blog.csdn.net/weixin_33398049/article/details/112781059
Dockerfile
https://www.cnblogs.com/panwenbin-logs/p/8007348.html
构建镜像
参考:
https://blog.csdn.net/chanmufeng/article/details/80459607
https://blog.csdn.net/we_shell/article/details/38445979
https://zimug.blog.csdn.net/article/details/124920298
登录仓库
docker login docker-hub.eggcode.top:5443
进入 dockerfile 所在目录
构建镜像
docker build -t docker-hub.eggcode.top:5443/app/system .
推送到仓库
docker push docker-hub.eggcode.top:5443/app/system