1、Docker 简介
Docker是一个开源的容器引擎,它可以帮助我们更快地交付应用。Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理。使用Docker,可更快地打包、测试以及部署应用程序,并可减少从编写到部署运行代码的周期。
Docker 官方网站https://www.docker.com/
docker 三要素:
- Docker 镜像(Images):Docker镜像是一个只读模板,它包含创建Docker容器的说明。它和系统安装光盘有点像——我们使用系统安装光盘安装系统,同理,我们使用Docker镜像运行Docker镜像中的程序。
- 容器(Container):容器是镜像的可运行实例。镜像和容器的关系有点类似于面向对象中,类和对象的关系。我们可通过Docker API或者CLI命令来启停、移动、删除容器。
- 仓库(Registry):Docker Registry是一个集中存储与分发镜像的服务。我们构建完Docker镜像后,就可在当前宿主机上运行。但如果想要在其他机器上运行这个镜像,我们就需要手动拷贝。此时,我们可借助Docker Registry来避免镜像的手动拷贝。
一个Docker Registry可包含多个Docker仓库;每个仓库可包含多个镜像标签;每个标签对应一个Docker镜像。这跟Maven的仓库有点类似,如果把Docker Registry比作Maven仓库的话,那么Docker仓库就可理解为某jar包的路径,而镜像标签则可理解为jar包的版本号。
2、Docker 安装与启动
环境:Linux 操作系统,版本是CentOS7 64位 Docker是CE版本(免费版)
Docker 下载链接:链接失效请私信
链接:https://pan.baidu.com/s/1VTxUIz9XDcn3dD6spZEL1A?pwd=qns1
提取码:qns1
将该 rpm 利用 Xftp 上传至 Linux
切换 root 用户获取权限,然后进行安装即可。
su root
yum install -y docker-ce-18.06.0.ce-3.el7.x86_64.rpm
安装完成,最后会出现 Complete !
3、启动与停止 Docker
systemctl start docker
systemctl status docker
systemctl stop docker
systemctl restart docker
查询是否安装成功:会出现Docker相关的详情信息。安装完毕
docker info
[root@localhost file]# docker info
Containers: 6
Running: 2
Paused: 0
Stopped: 4
Images: 2
Server Version: 18.06.0-ce
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d64c661f1d51c48782c9cec8fda7604785f93587
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.6MiB
Name: localhost.localdomain
ID: 33PU:JWNT:UBRS:CLMG:FEHQ:AGAL:APF6:3A3V:64SW:IEIM:XGAR:OOXP
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://4iydscvo.mirror.aliyuncs.com/
Live Restore Enabled: false
4、配置镜像加速器
阿里云镜像加速https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://4iydscvo.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
最后docker info 查看是否配置成功,下图表示成功。
5、Docker 镜像操作常用指令
(1)搜索指令 docker search ...
docker search tomcat
(2)下载镜像 docker pull 镜像名称
docker pull tomcat
下载完成。
(3)查看本地镜像
docker images
(4)删除镜像,可以通过镜像IMAGE ID删除,也可以镜像名:版本号
docker rmi fb5657adc892
[root@localhost root123]# docker rmi c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
(5)保存镜像为文件,首先下载一个java
docker pull java
docker save -o /home/file/jdk.tar java:latest
(6)将保存的镜像备份文件恢复镜像
先进行docker rmi 删除,在进行恢复。
docker rmi d23bdf5b1b1b
docker load -i/home/file/jdk.tar
查看镜像又恢复了。
docker images
6、Docker 容器常用操作指令
先pull一个镜像
docker pull java:8
(1)使用镜像创建一个容器
docker run java:8 /bin/echo 'caojun'
(2)列出容器
docker ps | 列出当前正在运行的容器 |
docker ps -a | 列出当前全部容器 |
docker ps -a -q | 列出当前容器的所有编号 |
(3)停止容器
一下是两种停止容器的方法。
(4)启动容器 docker start 容器名称或容器编号
docker start 6cef8b21cada
(5)强制停止容器 docker kill 容器名称或容器编号
[root@localhost file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2954933039e6 mysql:5.7 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 33060/tcp, 0.0.0.0:33306->3306/tcp offcn_mysql
c2439c591c66 java:8 "/bin/bash" 21 minutes ago Up 21 minutes jdk1
[root@localhost file]# docker kill 2954933039e6
2954933039e6
[root@localhost file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2439c591c66 java:8 "/bin/bash" 22 minutes ago Up 22 minutes jdk1
[root@localhost file]#
(6)重启容器 docker restart 容器名称或者容器编号
[root@localhost file]# docker restart 2954933039e6
2954933039e6
[root@localhost file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2954933039e6 mysql:5.7 "docker-entrypoint.s…" 13 minutes ago Up 6 seconds 33060/tcp, 0.0.0.0:33306->3306/tcp offcn_mysql
c2439c591c66 java:8 "/bin/bash" 23 minutes ago Up 23 minutes jdk1
[root@localhost file]#
(7)进入容器 docker exec -it heuristic_meninsky /bin/bash
[root@localhost file]# docker exec -it c2439c591c66 /bin/bash
root@c2439c591c66:/# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
root@c2439c591c66:/#
[root@localhost file]# docker exec -it 2954933039e6 /bin/bash
root@2954933039e6:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
(8)删除容器
docker rm 容器名称或者编号 //注意 不能删除正在运行中容器, 需要停掉才可以删除
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2439c591c66 java:8 "/bin/bash" 37 minutes ago Up 37 minutes jdk1
[root@localhost root123]# docker rm c2439c591c66
Error response from daemon: You cannot remove a running container c2439c591c66982f5a55e97a7bc1cd67a5ffed5090e94eef41c59fb6833518e8. Stop the container before attempting removal or force remove
[root@localhost root123]# docker stop c2439c591c66
c2439c591c66
[root@localhost root123]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost root123]# docker rm c2439c591c66
c2439c591c66
[root@localhost root123]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7、案例
(1)拉取java镜像
docker pull java:8
创建容器
docker run -di --name jdk1 java:8
进入容器
docker exec -it jdk1 /bin/bash
验证版本
java -version
退出
exit
[root@localhost file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java 8 d23bdf5b1b1b 6 years ago 643MB
[root@localhost file]# docker run -di --name jdk1 java:8
c2439c591c66982f5a55e97a7bc1cd67a5ffed5090e94eef41c59fb6833518e8
[root@localhost file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java 8 d23bdf5b1b1b 6 years ago 643MB
[root@localhost file]# docker exec -it jdk1 /bin/bash
root@c2439c591c66:/# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
root@c2439c591c66:/#
(2)拉取mysql镜像
docker pull mysql:5.7
创建 mysql 容器(这里是最简单的方法进行创建)
docker run -d --name=offcn_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=scwdb mysql:5.7
[root@localhost file]# docker run -d --name=offcn_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=scwdb mysql:5.7
2954933039e63cbf0a00e2f808b5cf805565dbfa4c7e92c92180be76a995b8e6
[root@localhost file]# docker exec -it offcn_mysql /bin/bash
root@2954933039e6:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
var code = "134c199f-0b00-47b4-b048-4e51ec776c0d"