来源:B站狂神说JAVA
狂神说Docker视频链接
Docker安装
# 系统内核是3.10以上的
root@theSun:/# uname -r
4.4.0-185-generic
# 系统版本
root@theSun:/# cat /etc/os-release
文档:
https://docs.docker.com/engine/install/ubuntu/
阿里云镜像参考:
https://blog.youkuaiyun.com/xie1xiao1jun/article/details/79413436
普通安装参考:
https://www.cnblogs.com/walker-lin/p/11214127.html
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
了解:
# 查看镜像
docker image
卸载:
1、卸载依赖
sudo apt-get purge docker-ce docker-ce-cli containerd.io
2、删除资源
sudo rm -rf /var/lib/docker
阿里云容器镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9htdhp67.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
使用网易镜像:
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
Docker的常用命令
帮助命令
docker version
docker info
docker 命令 --help
帮助文档地址:https://docs.docker.com/reference/
镜像命令
docker images
-a, --all #列出所有镜像
-q, --quiet #只显示镜像id
docker search 搜索镜像
# 通过收藏来过滤
root@theSun:/var/lib/docker# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9780 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3569 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
root@theSun:/var/lib/docker# docker pull mysql
Using default tag: latest # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
6ec8c9369e08: Pull complete # 分层下载,docker image的核心 联合文件系统
177e5de89054: Pull complete
ab6ccb86eb40: Pull complete
e1ee78841235: Pull complete
09cd86ccee56: Pull complete
78bea0594a44: Pull complete
caf5f529ae89: Pull complete
cf0fc09f046d: Pull complete
4ccd5b05a8f6: Pull complete
76d29d8de5d4: Pull complete
8077a91f5d16: Pull complete
922753e827ec: Pull complete
Digest: sha256:fb6a6a26111ba75f9e8487db639bc5721d4431beba4cd668a4e922b8f8b14acc #签名,防伪标志
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
# 等价的
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
root@theSun:/var/lib/docker# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec8c9369e08: Already exists
177e5de89054: Already exists
ab6ccb86eb40: Already exists
e1ee78841235: Already exists
09cd86ccee56: Already exists
78bea0594a44: Already exists
caf5f529ae89: Already exists
4e54a8bcf566: Pull complete
50c21ba6527b: Pull complete
68e74bb27b39: Pull complete
5f13eadfe747: Pull complete
Digest: sha256:97869b42772dac5b767f4e4692434fbd5e6b86bcb8695d4feafb52b59fe9ae24
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
docker rmi -f 容器id #删除指定容器
docker rmi -f 容器id 容器id 容器id #删除多个容器
docker rmi -f $(docker images -aq) #删除全部容器
容器命令
说明:有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomact01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-p 随机指定端口
# 测试,启动并进入容器
root@theSun:/var/lib/docker# docker run -it centos /bin/bash
[root@c0cd544258f3 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
# 从容器中退回主机
[root@c0cd544258f3 /]# exit
exit
root@theSun:/var/lib/docker# ls
builder containers network plugins swarm trust
buildkit image overlay2 runtimes tmp volumes
列出所有的运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+历史运行过的
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
root@theSun:/var/lib/docker# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@theSun:/var/lib/docker# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0cd544258f3 centos "/bin/bash" 4 minutes ago Exited (0) About a minute ago naughty_mcclintock
7dc103532dd1 hello-world "/hello" 25 hours ago Exited (0) 25 hours ago elegant_blackburn
退出容器
exit # 直接容器停止并退出
Ctrl+P+Q # 容器不停止退出
删除容器
docker rm 容器id # 删除指定的容器
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
# 问题docker ps,发现 centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了。
查看日志
docker logs -f -t --tail [CONTAINER ID]
# 自己写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1; done"
# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数
docker logs -tf --tail 10 [CONTAINER ID]
查看容器中进程信息
docker top [容器id]
PID:当