docker安装
在Linux centOS7上面安装docker。操作如下(参考官网给出的安装手册:https://docs.docker.com/engine/install/centos/)
1.删除本机docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.安装docker
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
3.启动docker
systemctl start docker
4.拉取hello-world
docker pull hello-world:latest
5.查看镜像
docker images
6.启动hello-world
docker run hello-world
7.查看容器
docker ps
8.配置中国的镜像仓库,加快下载速度。使用阿里云的镜像仓库
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://po7p45t1.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker架构图
docker | java | 备注 |
---|---|---|
Registry | maven | 参考存放各种镜像文件 |
Images | 类 | 镜像文件和类差不多 |
Containers | 对象 | 容器和对象差不多,容器是由镜像创建的 |
Images-镜像
1.添加一个镜像
创建一个centos的镜像
1.搜索centos的镜像
https://hub.docker.com/
2.使用命令:
docker pull centos:centos7
3.检查docker镜像
[root@VM-0-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
centos centos7 eeb6ee3f44bd 7 months ago 204MB
4.将镜像运行成容器 -it表示创建一个带交互的终端
[root@VM-0-4-centos ~]# docker run -it centos:centos7
[root@d998f693710e /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
5.检查运行中的容器
[root@VM-0-4-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d998f693710e centos:centos7 "/bin/bash" 18 seconds ago Up 17 seconds inspiring_galileo
6.使用exit退出终端,退出的同时会关闭容器
[root@d998f693710e /]# exit
exit
2.搜索镜像
docker search xxx
[root@VM-0-4-centos ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 7106 [OK]
centos/systemd systemd enabled base container. 108 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 93
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/python-35-centos7 Platform for building and running Python 3.5… 39
centos/php-56-centos7 Platform for building and running PHP 5.6 ap… 34
centos/mysql-56-centos7 MySQL 5.6 SQL database server 22
centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 19
kasmweb/centos-7-desktop CentOS 7 desktop for Kasm Workspaces 18
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16
centos/mariadb-101-centos7 MariaDB 10.1 SQL database server 13
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
centos/mongodb-36-centos7 MongoDB NoSQL database server 8
centos/redis-32-centos7 Redis in-memory data structure store, used a… 6
centos/mariadb-102-centos7 MariaDB 10.2 SQL database server 6
continuumio/centos5_gcc5_base 3
centos/ruby-25-centos7 Platform for building and running Ruby 2.5 a… 3
kasmweb/core-centos-7 CentOS 7 base image for Kasm Workspaces 2
bitnami/centos-base-buildpack Centos base compilation image 0 [OK]
bitnami/centos-extras-base 0
datadog/centos-i386 0
ibmcom/fhe-toolkit-centos The IBM Fully Homomorphic Encryption (FHE) T… 0
centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0
ibmcom/fhe-toolkit-centos-amd64 The IBM Fully Homomorphic Encryption (FHE) T… 0
[root@VM-0-4-centos ~]#
使用docker仓库搜索,可以指定tag来下载指定版本
3.拉取镜像
docker pull centos:centos7
// xxx:xxx 指定下载tag
比如拉取mysql镜像
docker pull mysql:8.0
4.删除镜像
删除镜像前需要停止该镜像的容器,并且删除
docker stop hello-world
清除所有停止的容器
docker container prune
或者删除容器
docker rm 容器id
删除镜像
docker rmi hello-world
5.生成镜像
了解dockerfile文件
实验:基于centos7创建一个包含vim命令的我的centos7
实验:基于Tomcat创建一个含有首页的Tomcat
1.拉取Tomcat镜像
docker pull tomcat
2.启动镜像,生成Tomcat容器。以容器终端的方式启动Tomcat
docker run -it -p 8888:8080 tomcat
3.访问首页
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1plazgaggj20yq0d4mz2.jpg" alt="image-20220428180110049" style="zoom:50%;" />
4.重新打开一个终端,并且创建一个首页
docker exec -it 容器ID /bin/bash
root@6e01e405ba82:/usr/local/tomcat# cd webapps
root@6e01e405ba82:/usr/local/tomcat/webapps# ls
root@6e01e405ba82:/usr/local/tomcat/webapps# mkdir ROOT
root@6e01e405ba82:/usr/local/tomcat/webapps# echo 'hello docker' > index.html
root@6e01e405ba82:/usr/local/tomcat/webapps# mv index.html ROOT/
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1plfj44whj20s006ydg9.jpg" alt="image-20220428180535007" style="zoom:50%;" />
5.将当前容器build成自己的镜像
docker commit -a='cjxz' -m='add index.html' 容器ID cjxz/tomcat:1.0
[root@VM-0-4-centos ~]# docker commit -a='cjxz' -m='add index.html' 6e01e405ba82 cjxz/tomcat:1.0
sha256:6217c92b3c1caf7f985df4cd38420682104672b3096561b41a08d55cb8b44b03
[root@VM-0-4-centos ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
cjxz/tomcat 1.0 6217c92b3c1c 9 seconds ago 680MB
tomcat latest fb5657adc892 4 months ago 680MB
mysql 8.0 3218b38490ce 4 months ago 516MB
centos centos7 eeb6ee3f44bd 7 months ago 204MB
[root@VM-0-4-centos ~]#
这样我们可以直接通过cjxz/tomcat创建一个新的容器,并且容器携带index.html
Containers-容器
镜像被run之后就成为容器
容器通过commit又能够变成镜像
Registry-镜像仓库
查找地址:docker hub