赞
踩
开启 docker
systemctl start docker
查看 docker 镜像
docker images
查看 docker 容器
docker ps -a
交互模式启动 Tomcat
docker run -it -p 8080:8080 tomcat
进入 Tomcat 容器
docker exec -it 7bace7bfed93 /bin/bash
进入Tomcat,完善镜像
官方镜像默认 webapp 下没有文件
cp -r webapps.dist/* webapps
win 10 谷歌访问
退出 Tomcat 容器
exit
提交自己定义或修改之后的 Tomcat 的镜像
docker commit -a="coffeemao" -m="add webapps app" 7bace7bfed93 tomcat01:1.0
查看
[root@localhost ~]# docker commit -a="coffeemao" -m="add webapps app" 7bace7bfed93 tomcat01:1.0
sha256:c48ce45d3268cc5b8d436bdbf3d66d239eb3b9be27717d0239680f6cc63fa574
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat01 1.0 c48ce45d3268 11 seconds ago 684MB
nginx latest 605c77e624dd 7 months ago 141MB
tomcat latest fb5657adc892 8 months ago 680MB
portainer/portainer latest 580c0e4e98b0 17 months ago 79.1MB
[root@localhost ~]#
发布需要 dockerfile,docker pull tomcat
是不完整版本
docker 启动容器
[root@localhost ~]# docker start 7bace7bfed93
数据如果在容器中,容器一旦删除,就会造成删库跑路,需求数据持久化
容器数据卷技术:MySQL数据存储在本地,容器之间存在一个共享技术,Docker 容器中产生的数据,同步到本地挂载 -v
,容器之间数据共享,容器的持久化和同步操作。
docker run -it -v 虚拟机目录:/home centos /bin/bash
[root@localhost /]# docker run -it -v /home/ceshi:/home centos /bin/bash
Unable to find image 'centos:latest' locally
容器内部建立文件,文件外部同步双向同步
关闭容器
[root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7bd01d3d65b2 centos "/bin/bash" 15 minutes ago Exited (0) 19 seconds ago elegant_heyrovsky 6316bac4f9b1 nginx "/docker-entrypoint.…" 27 minutes ago Exited (137) 26 minutes ago nginx 7bace7bfed93 tomcat "catalina.sh run" About an hour ago Exited (143) 25 minutes ago youthful_chebyshev a38797375cb9 portainer/portainer "/portainer" 9 days ago Up About an hour 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp competent_ardinghelli [root@localhost /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a38797375cb9 portainer/portainer "/portainer" 9 days ago Up About an hour 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp competent_ardinghelli [root@localhost /]# docker stop a38797375cb9 a38797375cb9 [root@localhost /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7bd01d3d65b2 centos "/bin/bash" 16 minutes ago Exited (0) 45 seconds ago elegant_heyrovsky 6316bac4f9b1 nginx "/docker-entrypoint.…" 27 minutes ago Exited (137) 26 minutes ago nginx 7bace7bfed93 tomcat "catalina.sh run" About an hour ago Exited (143) 25 minutes ago youthful_chebyshev a38797375cb9 portainer/portainer "/portainer" 9 days ago Exited (2) 6 seconds ago competent_ardinghelli [root@localhost /]#
关闭容器之后,在主机上对hello.java
文件进行修改,在开启容器,进入正在运行的容器(attach
),查看已经同步。
[root@localhost /]# docker start 7bd01d3d65b2 7bd01d3d65b2 [root@localhost /]# docker attach 7bd01d3d65b2 [root@7bd01d3d65b2 /]# cd /home [root@7bd01d3d65b2 home]# ls hello.java word.java [root@7bd01d3d65b2 home]# cat hello.java public class hello{ public static void main(String[] args){ System.out.println("hello,java"); } } [root@7bd01d3d65b2 home]# exit exit [root@localhost /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost /]#
备注进入容器推荐:
[root@localhost ~]# docker exec -it f57e2a1ee1aa /bin/bash
root@f57e2a1ee1aa:/# ls
bin dev entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
优点:挂载之后,以后直接在本地进行修改,容器内部会自动的同步
配置MySQL密码
https://hub.docker.com/_/mysql
docker分配的主机:mysql自己的主机
3306:3306
[root@localhost ~]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql
f57e2a1ee1aa61e86d38ee7f7db224e12f2e401e73cbb7d561352c414423952a
[root@localhost ~]#
本地MySQL可视化连接工具可以连接
在 Navicat 上连接,建立数据库docker_test
之后,进入虚拟机映射的目录进行查看,可以发现文件已经共享同步实现。
假设将容器删除,文件依旧保存到虚拟机上,不会删除
匿名挂载
[root@localhost ~]# docker run -d -p 80:80 --name nginx01 -v /etc/nginx nginx
具名挂载
# 具名挂载 [root@localhost ~]# docker run -d -p 80:80 --name nginx02 -v juming_nginx:/etc/nginx nginx 2013475b401f289787df485a3e31445bd961316ef3317e531d0fb5065a63570e [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2013475b401f nginx "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx02 [root@localhost ~]# docker volume ls DRIVER VOLUME NAME # 匿名挂载 local 9cec5c43ffa11fe81f8c5b64f209fa4b80dcf4986b9db58d9dcbb8fd34d96ecb local e8573755ba1b68d10626d688a7f770d3c0c66807afc540ee7a261ea18103b0e1 # 具名挂载 local juming_nginx [root@localhost /]# docker volume inspect juming_nginx [ { "CreatedAt": "2022-08-26T15:30:56+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming_nginx/_data", "Name": "juming_nginx", "Options": null, "Scope": "local" } ] [root@localhost /]# cd var/lib/docker [root@localhost docker]# ls buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes [root@localhost docker]# cd volumes/ [root@localhost volumes]# ll 总用量 24 drwx-----x. 3 root root 19 8月 26 15:19 9cec5c43ffa11fe81f8c5b64f209fa4b80dcf4986b9db58d9dcbb8fd34d96ecb brw-------. 1 root root 253, 0 8月 26 10:07 backingFsBlockDev drwx-----x. 3 root root 19 8月 16 18:22 e8573755ba1b68d10626d688a7f770d3c0c66807afc540ee7a261ea18103b0e1 drwx-----x. 3 root root 19 8月 26 15:30 juming_nginx -rw-------. 1 root root 32768 8月 26 15:30 metadata.db [root@localhost volumes]# cd juming_nginx/ [root@localhost juming_nginx]# ll 总用量 0 drwxr-xr-x. 3 root root 132 8月 26 15:30 _data [root@localhost juming_nginx]# cd _data/ [root@localhost _data]# ll 总用量 24 drwxr-xr-x. 2 root root 26 8月 26 15:30 conf.d -rw-r--r--. 1 root root 1007 12月 28 2021 fastcgi_params -rw-r--r--. 1 root root 5349 12月 28 2021 mime.types lrwxrwxrwx. 1 root root 22 12月 28 2021 modules -> /usr/lib/nginx/modules -rw-r--r--. 1 root root 648 12月 28 2021 nginx.conf -rw-r--r--. 1 root root 636 12月 28 2021 scgi_params -rw-r--r--. 1 root root 664 12月 28 2021 uwsgi_params [root@localhost _data]# cat nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
docker volume inspect juming_nginx
查看具名挂载的位置
注意原来的Centos7 被裁减之后,有些基本的命令没有
FROM centos:7
MAINTAINER coffeemao<2750506316@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "------end------"
CMD /bin/bash
docker build -f dockerfile -t mycentos:0.1 .
Linux登录DockerHub和退出
[root@localhost code]# docker login -u username
Password:
Login Succeeded
[root@localhost code]# docker logout
Removing login credentials for https://index.docker.io/v1/
环境准备
[root@localhost environment]# ll
总用量 176284
-rw-r--r--. 1 root root 11593900 8月 27 12:51 apache-tomcat-9.0.65.tar.gz
-rw-r--r--. 1 root root 645 8月 28 16:05 Dockerfile
-rw-r--r--. 1 root root 168907175 8月 27 12:51 jdk-11.0.16.1_linux-x64_bin.tar.gz
drwxrwxr-x. 17 mao mao 4096 3月 24 12:05 Python-3.5.2
-rw-r--r--. 1 root root 0 8月 27 12:52 readme.txt
Dockerfile
文件编写
FROM centos:7 MAINTAINER coffeemao<2750506316@qq.com> COPY readme.txt /usr/local/readme.txt ADD jdk-11.0.16.1_linux-x64_bin.tar.gz /usr/local/ ADD apache-tomcat-9.0.65.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk11 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.65 ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.65 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.65/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.65/logs/catalina.out
如果运行报错话docker logs 容器名字
查看报错内容对应进行修改
可能报错有Tomcat 版本不一致导致部分目录没有,JDK 版本不一致,目录冲突
启动
[root@localhost environment]# docker build -t diytomcat . Sending build context to Docker daemon 366.9MB Step 1/15 : FROM centos:7 ---> eeb6ee3f44bd Step 2/15 : MAINTAINER coffeemao<2750506316@qq.com> ---> Using cache ---> 1e092941a253 Step 3/15 : COPY readme.txt /usr/local/readme.txt ---> cf0f007a016a Step 4/15 : ADD jdk-11.0.16.1_linux-x64_bin.tar.gz /usr/local/ ---> 01f121798e0d Step 5/15 : ADD apache-tomcat-9.0.65.tar.gz /usr/local/ ---> 8471193a83d7 Step 6/15 : RUN yum -y install vim ---> Running in 3e6ab25db0a8 ...... Successfully built c44606f4f964 Successfully tagged diytomcat:latest
挂载
[root@sail tomcat]# docker run -d -p 8080:8080 --name coffeemaotomcat -v /home/mao/tomcat/webapps:/usr/local/apache-tomcat-9.0.65/webapps -v /home/mao/tomcat/logs:/usr/local/apache-tomcat-9.0.65/logs diytomcat
9d391e13efdc495206429dbdb0392180a7bd3a4750cbc1419c31c80cd69c6b7b
[root@localhost environment]# docker run -it -d -p 8080:8080 --name tomcattest -v /home/mao/tomcat1/webapps:/usr/local/apache-tomcat-9.0.65/webapps -v /home/mao/tomcat1/logs/:/usr/local/apache-tomcat-9.0.65/logs diytomcat
230c89dfe6aeff874a207eb30424755d1fef3280ff7767cbaf1c48354612ac73
挂载成功
[root@localhost mao]# cd build
[root@localhost build]# ll
总用量 0
drwxr-xr-x. 4 root root 36 8月 28 16:21 tomcat
[root@localhost build]# cd tomcat/
[root@localhost tomcat]# ll
总用量 0
drwxr-xr-x. 2 root root 6 8月 28 16:21 test
drwxr-xr-x. 2 root root 26 8月 28 16:21 tomcatlogs
[root@localhost tomcat]#
[root@localhost mao]# curl localhost:8080
进入 容器查看一下
外部访问curl
访暴露的端口,不能在容器内部
因此在本地就可以发布项目,由于tomcat 不断更新,目录结构存放的内容要求也不同,所以必须和自己下载的tomcat 保持一致,不然会报404 错误
1、删除旧版本和自带的java 环境变量
rpm -qa | grep gcj
rpm -qa | grep jdk
yum -y remove [上面查出来的所有有关jdk的名字]
java -version
解压缩和环境变量的配置
tar -xzvf jdk-8u11-linux-x64.tar.gz
vim /etc/profil
export JAVA_HOME=自己解压缩的路径
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
配置更新加载
source /etc/profile
[root@localhost environment]# ll
总用量 166628
-rw-r--r--. 1 root root 11593900 8月 27 12:51 apache-tomcat-9.0.65.tar.gz
-rw-r--r--. 1 root root 640 8月 28 17:09 Dockerfile
drwxr-xr-x. 8 10 143 255 6月 17 2014 jdk1.8.0_11
-rw-r--r--. 1 root root 159019376 8月 28 17:57 jdk-8u11-linux-x64.tar.gz
drwxrwxr-x. 17 mao mao 4096 3月 24 12:05 Python-3.5.2
-rw-r--r--. 1 root root 0 8月 27 12:52 readme.txt
[root@localhost environment]# java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
[root@localhost environment]#
$ docker login --username= registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/---/coffeemao_docker:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/---/coffeemao_docker:[镜像版本号]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。