准备
- 虚拟机
- 虚拟机安装Docker
在纯净的Centos镜像上搭建
Centos镜像准备
- 虚拟机上拉取 Centos 镜像: docker pull centos
- 创建一个容器运行Centos镜像:docker run -it -d --name mycentos centos /bin/bash
注:这里遇到了一个错误【WARNING: IPv4 forwarding is disabled. Networking will not work.】
更改虚拟机文件:vim /usr/lib/sysctl.d/00-system.conf添加如下内容net.ipv4.ip_forward=1重启网络:systemctl restart network
注:这里又衍生一个问题,docker中systemctl无法正常使用。在官网找到如下解决办法
链接:https://forums.docker.com/t/systemctl-status-is-not-working-in-my-docker-container/9075/4
run 镜像的时候用如下语句docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup -it -d --name usr_sbin_init_centos centos /usr/sbin/init#注意几点 #1. 必须有--privileged#2. 必须有-v /sys/fs/cgroup:/sys/fs/cgroup#3. 将bin/bash替换成 /usr/sbin/init
最后终于能够正常运行起来一个Centos镜像了。
安装JAVA 环境准备JDK tar包上传到 虚拟机中
- 使用docker cp 将tar包放入docker容器中
docker cp jdk-11.0.2_linux-x64_bin.tar.gz 41dbc0fbdf3c:/#与linux cp指定用法相同,需要加上container的标识:id或者name
tar -xf jdk-11.0.2_linux-x64_bin.tar.gz -C /usr/local/java/jdk
- 编辑profile文件 export java环境变量
# /etc/profileexport JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_91export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- 运行 source /etc/profile,使环境变量生效
- 检测是否成功
java --version #结果java 11.0.2 2019-01-15 LTSJava(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
安装Tomcat
- 准备好tomcat tar 包上传到虚拟机,并cp到docker容器中
- 解压到
tar -xf apache-tomcat-8.5.38.tar.gz -C /usr/local/tomcat
- 设置开机启动,通过使用rc.local文件实现
#rc.local 增加如下代码export JAVA_HOME=/usr/local/java/jdk/jdk-11.0.2/usr/local/tomcat/apache-tomcat-8.5.38/bin/startup.sh
#到/usr/local/tomcat/apache-tomcat-8.5.38/bin/目录下 运行./startup.sh
curl localhost:8080#返回html源码内容
安装mysql
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
validate_password=OFF # 关闭密码验证character-set-server=utf8 collation-server=utf8_general_cilog-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid[client]default-character-set=utf8
grep "password" /var/log/mysqld.log#结果:[Note] A temporary password is generated for root@localhost: k:nT
# 进入mysql -u root -p #更改密码ALTER USER 'root'@'localhost' IDENTIFIED BY '111111';# 更改 使mysql可以远端访问update user set host = '%' where user = 'root';
- 测试,可以使用物理机,使用navicat 对docker中的mysql进行访问
打包容器
# 将容器提交成 镜像docker commit -a 'kane' -m 'test' container_id images_name:images_tag# 推到dockerhubdocker push kane0725/tomcat
# 导出打成本地 tar 包docker export -o test.tar a404c6c174a2# 将 tar 包导入成镜像docker import test.tar test_images
使用Dockerfile
注:只搭建一个tomcat的镜像
准备工作
- 创建一个专门的文件夹,放入jdk 与tomcat的 tar包
- 在这个目录下创建Dockerfile文件
- centos 基础镜像
文件内容
FROM centosMAINTAINER tomcat mysqlADD jdk-11.0.2 /usr/local/javaENV JAVA_HOME /usr/local/java/ADD apache-tomcat-8.5.38 /usr/local/tomcat8EXPOSE 8080
使用docker build输出结果
[root@localhost dockerfile]# docker build -t tomcats:centos .Sending build context to Docker daemon 505.8 MBStep 1/7 : FROM centos ---> 1e1148e4cc2cStep 2/7 : MAINTAINER tomcat mysql ---> Using cache ---> 889454b28f55Step 3/7 : ADD jdk-11.0.2 /usr/local/java ---> Using cache ---> 8cad86ae7723Step 4/7 : ENV JAVA_HOME /usr/local/java/ ---> Running in 15d89d66adb4 ---> 767983acfacaRemoving intermediate container 15d89d66adb4Step 5/7 : ADD apache-tomcat-8.5.38 /usr/local/tomcat8 ---> 4219d7d611ecRemoving intermediate container 3c2438ecf955Step 6/7 : EXPOSE 8080 ---> Running in 56c4e0c3b326 ---> 7c5bd484168aRemoving intermediate container 56c4e0c3b326Step 7/7 : RUN /usr/local/tomcat8/bin/startup.sh ---> Running in 7a73d0317db3Tomcat started. ---> b53a6d54bf64Removing intermediate container 7a73d0317db3Successfully built b53a6d54bf64
docker build的问题
一定要带上命令后面的 . 否则会报错的"docker build" requires exactly 1 argument(s).
运行一个容器
# 进入容器docker run -it --name tomcats --restart always -p 1234:8080 tomcats /bin/bash#运行tomcat startup.sh/usr/local/tomcat8/bin/startup.sh#结果Using CATALINA_BASE: /usr/local/tomcat8Using CATALINA_HOME: /usr/local/tomcat8Using CATALINA_TMPDIR: /usr/local/tomcat8/tempUsing JRE_HOME: /usr/local/java/Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jarTomcat started.
使用docker compose
安装 docker compose
官方:https://docs.docker.com/compose/install/
我选择的方式是pip安装
# 安装pip install docker-compose# 检测docker-compose --version# -----------------------docker-compose version 1.23.2, build 1110ad0
编写docker-compose.yml
# 这个yml文件 搭建一个mysql 一个 tomcat的容器version: "3" services: mysql: container_name: mysql image: mysql:5.7 restart: always volumes: - ./mysql/data/:/var/lib/mysql/ - ./mysql/conf/:/etc/mysql/mysql.conf.d/ ports: - "6033:3306" environment: - MYSQL_ROOT_PASSWORD=******** tomcat: container_name: tomcat restart: always image: tomcat ports: - 8080:8080 - 8009:8009 links: - mysql:m1 #连接数据库镜像
注:
- volumn里必须是路径,不能指定文件
- tomcat指定外部conf的时候一直创建不成功,不知道为什么,提示
tomcat | Feb 20, 2019 2:23:29 AM org.apache.catalina.startup.Catalina loadtomcat | WARNING: Unable to load server configuration from [/usr/local/tomcat/conf/server.xml]tomcat | Feb 20, 2019 2:23:29 AM org.apache.catalina.startup.Catalina starttomcat | SEVERE: Cannot start server. Server instance is not configured.tomcat exited with code 1
注:必须在yml文件的目录下下执行
docker-compose up -d# 结果----------查看docker container-------[root@localhost docker-compose]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1a8a0165a3a8 tomcat "catalina.sh run" 7 seconds ago Up 6 seconds 0.0.0.0:8009->8009/tcp, 0.0.0.0:8080->8080/tcp tomcatddf081e87d67 mysql:5.7 "docker-entrypoint..." 7 seconds ago Up 7 seconds 33060/tcp, 0.0.0.0:6033->3306/tcp mysql