来源:B站狂神说JAVA
狂神说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.csdn.net/xie1xiao1jun/article/details/79413436
# 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
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:当前进程id
PPID:父进程id
查看镜像的元数据
docker inspect [容器id]
进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bashShell
# 测试
docker exect -it [容器id] /bin/bash
# 方法二
docker attach 容器id
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
root@theSun:/home# docker attach 4ec656c03592
[root@4ec656c03592 /]# cd /home
[root@4ec656c03592 home]# ls
[root@4ec656c03592 home]# touch test.java
[root@4ec656c03592 home]# ls
test.java
[root@4ec656c03592 home]# exit
exit
root@theSun:/home# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ec656c03592 centos "/bin/bash" 2 minutes ago Exited (0) 6 seconds ago intelligent_goodall
c0cd544258f3 centos "/bin/bash" 12 hours ago Exited (0) 12 hours ago naughty_mcclintock
7dc103532dd1 hello-world "/hello" 37 hours ago Exited (0) 37 hours ago elegant_blackburn
# 拷贝
root@theSun:/home# docker cp 4ec656c03592:/home/test.java /home
root@theSun:/home# ls
test.java
# 拷贝是一个手动过程,未来可以使用 -v 卷的技术,可以实现自动同步
Docker安装Nginx
# 1、搜索镜像 search 建议去dockerhub搜索
# 2、下载镜像 pull
# 3、运行测试
root@theSun:/home# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
root@theSun:/home# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 8679ced16d20 8 days ago 448MB
mysql latest e3fcc9e1cc04 8 days ago 544MB
nginx latest 8cf1bfb43ff5 9 days ago 132MB
centos latest 831691599b88 6 weeks ago 215MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
root@theSun:/home# docker run -d --name nginx01 -p 3389:80 nginx
4ece82f587c1aad352303f6e938a384dd5a130cab88965e3f08c475b291f7c14
root@theSun:/home# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ece82f587c1 nginx "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 0.0.0.0:3389->80/tcp nginx01
root@theSun:/home# curl localhost:3389
<!DOCTYPE html>
Welcome to nginx!</title>