Precondition
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 0.0.0.0:3306->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" http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.tar.gz
3. Download Tomcat
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.11/bin/apache-tomcat-8.5.11.tar.gz
III. writing Dockerfile
1. Build the Dockerfile of javaweb image (my storage directory is / docker/java/javaweb /)
FROM ubuntu:latest MAINTAINER gancy "18451117878@163.com" #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 ENV PATH $JAVA_HOME/bin;$PATH ENV CLASSPATH .:$JAVA_HOME/lib #Configure the operating environment ENV LANG C.UTF-8 #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 EXPOSE 8080 ENTRYPOINT /usr/local/tomcat/apache-tomcat-8.5.11/bin/startup.sh && /bin/bash
2. Build the Dockerfile of mysql image in utf-8 encoding format (my storage directory is / docker/mysql /)
FROM mysql:latest MAINTAINER gancy "18451117878@163.com" #Modify coding method RUN echo "character-set-server=utf8" >> /etc/mysql/mysql.conf.d/mysqld.cnf RUN service mysql restart EXPOSE 3306
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 ff26f0d854ea1e1e36b592070c85d911b6501de4e1292b637749bcb28ced6520 root@VM-71-179-ubuntu:/docker/java/javaweb# docker run -i -t -d -p 8088:8080 --name="javawebtest" gancy/javaweb 3315b161443c2b47b8205a5469ad4a3bf515d9494b93c91ae47f4fee94f2aa21 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 0.0.0.0:8088->8080/tcp javawebtest ff26f0d854ea gancy/mysql "docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0:3306->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 https://hub.docker.com -d Background operation
Six, test
Java Web test
MySql test