注:這裏遇到了一個錯誤【WARNING: IPv4 forwarding is disabled. Networking will not work.】html
更改虛擬機文件:vim /usr/lib/sysctl.d/00-system.conf 添加以下內容 net.ipv4.ip_forward=1 重啓網絡:systemctl restart network
注:這裏又衍生一個問題,docker中systemctl沒法正常使用。在官網找到以下解決辦法java
連接:https://forums.docker.com/t/systemctl-status-is-not-working-in-my-docker-container/9075/4mysql
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鏡像了。linux
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
# /etc/profile export JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_91 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
java --version #結果 java 11.0.2 2019-01-15 LTS Java(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)
tar -xf apache-tomcat-8.5.38.tar.gz -C /usr/local/tomcat
#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源碼內容
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_ci log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid initialize specified but the data directory has files in it # 從5.6開始timestamp 的默認行爲已是 deprecated 了,須要關閉警告 [client] default-character-set=utf8
grep "password" /var/log/mysqld.log #結果:[Note] A temporary password is generated for root@localhost: k:nT<dT,t4sF #使用這個密碼登陸mysql
# 進入 mysql -u root -p #更改密碼 ALTER USER 'root'@'localhost' IDENTIFIED BY '111111'; # 更改 使mysql能夠遠端訪問 update user set host = '%' where user = 'root';
# 將容器提交成 鏡像 docker commit -a 'kane' -m 'test' container_id images_name:images_tag # 推到dockerhub docker push kane0725/tomcat
# 導出打成本地 tar 包 docker export -o test.tar a404c6c174a2 # 將 tar 包導入成鏡像 docker import test.tar test_images
注:只搭建一個tomcat的鏡像sql
FROM centos MAINTAINER tomcat mysql ADD jdk-11.0.2 /usr/local/java ENV JAVA_HOME /usr/local/java/ ADD apache-tomcat-8.5.38 /usr/local/tomcat8 EXPOSE 8080
[root@localhost dockerfile]# docker build -t tomcats:centos . Sending build context to Docker daemon 505.8 MB Step 1/7 : FROM centos ---> 1e1148e4cc2c Step 2/7 : MAINTAINER tomcat mysql ---> Using cache ---> 889454b28f55 Step 3/7 : ADD jdk-11.0.2 /usr/local/java ---> Using cache ---> 8cad86ae7723 Step 4/7 : ENV JAVA_HOME /usr/local/java/ ---> Running in 15d89d66adb4 ---> 767983acfaca Removing intermediate container 15d89d66adb4 Step 5/7 : ADD apache-tomcat-8.5.38 /usr/local/tomcat8 ---> 4219d7d611ec Removing intermediate container 3c2438ecf955 Step 6/7 : EXPOSE 8080 ---> Running in 56c4e0c3b326 ---> 7c5bd484168a Removing intermediate container 56c4e0c3b326 Step 7/7 : RUN /usr/local/tomcat8/bin/startup.sh ---> Running in 7a73d0317db3 Tomcat started. ---> b53a6d54bf64 Removing intermediate container 7a73d0317db3 Successfully built b53a6d54bf64
必定要帶上命令後面的 . 不然會報錯的 "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/tomcat8 Using CATALINA_HOME: /usr/local/tomcat8 Using CATALINA_TMPDIR: /usr/local/tomcat8/temp Using JRE_HOME: /usr/local/java/ Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar Tomcat started.
官方:https://docs.docker.com/compose/install/docker
我選擇的方式是pip安裝數據庫
# 安裝 pip install docker-compose # 檢測 docker-compose --version # ----------------------- docker-compose version 1.23.2, build 1110ad0
# 這個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 #鏈接數據庫鏡像
注:apache
volumn裏必須是路徑,不能指定文件bootstrap
tomcat指定外部conf的時候一直建立不成功,不知道爲何,提示vim
tomcat | Feb 20, 2019 2:23:29 AM org.apache.catalina.startup.Catalina load tomcat | 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 start tomcat | 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 -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a8a0165a3a8 tomcat "catalina.sh run" 7 seconds ago Up 6 seconds 0.0.0.0:8009->8009/tcp, 0.0.0.0:8080->8080/tcp tomcat ddf081e87d67 mysql:5.7 "docker-entrypoint..." 7 seconds ago Up 7 seconds 33060/tcp, 0.0.0.0:6033->3306/tcp mysql