Docker安装MySQL&Tomcat

2021年08月10日 · 缤纷网络 · 203次阅读

docker命令

1.拉取镜像(下载):

docker pull <镜像名>:<tag>  

说明:tag可以省略,省略默认拉取latest版本(最新版)

2.镜像操作:

查看:

docker images

删除:

docker rmi <镜像id>

3.运行镜像:

docker run --name <自定义容器名称> -d <镜像名称>:<tags>

说明:--name 为容器命名 -d 后台运行

4.查看运行中的容器:

docker ps

查看所有容器

docker ps -a

5.停止运行中的容器:

docker stop <容器id>

6.启动容器:

docker start <容器id>

7.删除一个容器:

docker rm <容器id>

8.运行容器并做端口映射:

docker run --name <自定义容器名称> ‐d ‐p <虚拟机端口>:<容器端口> <镜像名称>

9.查看容器的日志:

docker logs <容器名称|容器id>

10.修改docker镜像拉取地址为阿里云

cd /etc/docker
vi /etc/docker/daemon.json 
{
    "registry-mirrors":["https://f3lu6ju1.mirror.aliyuncs.com"]
}

运行下面的命令后生效

systemctl daemon-reload
systemctl restart docker

11.复制文件到容器下

docker cp 要复制的文件 容器id:容器的目录名称
docker cp ./test.html cf3c:/usr/local/tomcat/webapps/web1

12.容器下文件复制到linux

docker cp  容器id:容器的目录名称  linux的目录
docker cp 1212121:/yy/2020.txt /root/pp

13.进入一个容器

sudo docker exec -it 容器id /bin/bash 

安装MySQL

1.下载镜像

docker pull mysql:5.7

2.运行镜像

docker run --name mysql5.7 -p 3307:3306 -d -e MYSQL_ROOT_PASSWORD=11 -idt mysql:5.7 --character-set-server=utf8 --collation-server=utf8_unicode_ci

-d 表示在后台运行,不随当前命令行窗口的退出而退出

--name 给容器起一个别名,以后可以通过这个别名管理此容器

-p 3307:3307 把宿主机的3307端口映射到Mysql容器的3306端口

-e MySQL容器的环境配置

-i:表示以“交互模式”运行容器

-t:表示容器启动后会进入其命令行

MYSQL_ROOT_PASSWORD=11 指定mysql的密码,用户名默认为root,注意如果没有指定密码,会启动失败

3.查看运行的容器

docker ps -a

4.启动容器

docker start mysql5.7

5.进入MySQL容器

docker exec -it mysql5.7 /bin/bash

提示符变成:如:root@290094a1cdb9:/# 其中290094a1cdb9是容器的id

mysql -uroot -p11

exit退出mysql

exit退出docker容器

6.停止mysql容器

按容器名称

docker stop mysql5.7

按容器id

docker stop 29000

安装Tomcat

1.拉取镜像

docker pull tomcat

2.启动一个Tomcat命名为mytomcat

docker run -d --name mytomcat -p 8090:8080 tomcat

3.复制war到Tomcat的webapps目录下

docker cp /home/xxxx.war cecb37db8134:/usr/local/tomcat/webapps

4.进入Tomcat

sudo docker exec -it cecb37db8134 /bin/bash

docker安装Tomcat后出现的问题

1.从阿里云拉下来的tomcat启动后,访问容器出现404错误

进入容器,首先检查webapps文件夹是否为空,将其删除后把webapps.dist文件夹改名为webapps即可.

参考链接:https://blog.csdn.net/qq_40891009/article/details/104012792

2.部署war包不方便

进入Docker容器的文件目录,通过tomcat-users.xml 配置tomcat管理员

1.将tomcat-users.xml复制到 容器ID:/usr/local/tomcat/conf/中

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0">
    <role rolename="manager"/>
    <role rolename="manager-gui"/>
    <role rolename="admin"/>
    <role rolename="admin-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-status"/>
    <user username="admin" password="admin"
          roles="admin-gui,admin,manager-gui,manager,manager-script,manager-jmx,manager-status"/>

</tomcat-users>

2.将manager.xml复制到 容器ID:/usr/local/tomcat/conf/Catalina/localhost/中

<Context privileged="true" antiResourceLocking="false"   
         docBase="${catalina.home}/webapps/manager">  
             <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />  
</Context>  

重启docker访问控制页面输入账号密码即可直接部署项目

参考链接:https://blog.csdn.net/qq_39530754/article/details/82909777

标签:none

最后编辑于:2021-08-10 22:35

  1. 2021-08-11 09:48

    {!{data:image/webp;base64,UklGRsgQAABXRUJQVlA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