Docker builds Java Web + Mysql running environment

Keywords: MySQL Docker Java Tomcat


An Ubuntu machine with Docker installed. If you don't know how to install Docker, Please poke here..

I. pull the basic image of ubuntu and mysql

1. Get mysql image

docker pull mysql:latest

2. Get ubuntu image

docker pull ubuntu

2. Check whether the image is pulled to

root@gancy:/# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              7666f75adb6b        4 weeks ago         406 MB

3. Create and run the container

root@gancy:/# docker run --name mysql -v /data/mysql:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
root@gancy:/# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b7e8a431a310        mysql:latest        "docker-entrypoint..."   18 minutes ago      Up 6 minutes>3306/tcp   mysql

Download jdk and Tomcat

1. Switch to the stored directory (the same directory as the later Dockerfile)

cd /docker/java/javaweb

2. Download jdk

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie"

3. Download Tomcat


III. writing Dockerfile

1. Build the Dockerfile of javaweb image (my storage directory is / docker/java/javaweb /)

FROM ubuntu:latest
#Refresh package cache and install wget tool
RUN apt-get update

#Install and configure the java environment

ADD jdk-8u101-linux-x64.tar.gz /usr/local/java

#Configure environment variables
ENV JAVA_HOME /usr/local/java/jdk1.8.0_101

#Configure the operating environment

#Configure Tomcat
ADD apache-tomcat-8.5.11.tar.gz /usr/local/tomcat/
RUN chmod +x /usr/local/tomcat/apache-tomcat-8.5.11/bin/*.sh
ENTRYPOINT /usr/local/tomcat/apache-tomcat-8.5.11/bin/ && /bin/bash

2. Build the Dockerfile of mysql image in utf-8 encoding format (my storage directory is / docker/mysql /)

FROM mysql:latest
#Modify coding method
RUN echo "character-set-server=utf8" >> /etc/mysql/mysql.conf.d/mysqld.cnf

RUN service mysql restart


IV. build image according to Dockerfile

Note: switch to the directory of Dockerfile before building the image
1. Build mysql

docker build -t="gancy/mysql" .

2. Building the basic image of Java Web

docker build -t="gancy/javaweb" .

3. View the built image

root@VM-71-179-ubuntu:/docker/java/javaweb# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
gancy/javaweb       latest              69fdf275136e        55 seconds ago      548 MB
gancy/mysql         latest              f898d7b9976d        4 minutes ago       406 MB
mysql               latest              22be5748ecbe        2 days ago          406 MB
ubuntu              latest              0ef2e08ed3fa        3 days ago          130 MB

V. running mysql and Java Web Image

root@VM-71-179-ubuntu:/docker/java/javaweb# docker run --name mysql -v /data/mysql:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d gancy/mysql
root@VM-71-179-ubuntu:/docker/java/javaweb# docker run -i -t -d -p 8088:8080 --name="javawebtest" gancy/javaweb
root@VM-71-179-ubuntu:/docker/java/javaweb# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3315b161443c        gancy/javaweb       "/bin/sh -c '/usr/..."   21 seconds ago      Up 20 seconds>8080/tcp   javawebtest
ff26f0d854ea        gancy/mysql         "docker-entrypoint..."   2 minutes ago       Up 2 minutes>3306/tcp   mysql

run      Run the container.
–name  Name the container. If it is not named, it will be a long string Hash Value, not beautiful.
-v     Data volume storage. If the storage container is not closed, all data will be destroyed. Map a local folder to the container's folder.
-p     Port mapping: map the local 3306 port to the 3306 port in the container. It feels like the database is local. 2333
-e     Set the variable value, where the database is set root Many variables can be set by yourself, and specific access
-d     Background operation

Six, test

Java Web test

Web test results

MySql test

MySQL test results

Posted by luked23 on Fri, 06 Dec 2019 19:47:39 -0800