Nginx是一款轻量级的web服务器,也是一款优秀的反向代理服务器
1)下载基础镜像
# docker pull centos:7
2)建立工作目录
# mkdir nginx
# cd nginx
# rz -E
把 nginx-1.12.0.tar.gz 软件包复制过来
# vim Dockerfile
FROM centos:7
#用户信息
MAINTAINER this is nginx image
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#下载nginx软件包
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src
#指定工作目录
WORKDIR nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
#指定http和https端口
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
#添加宿主机中run. sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
4)编写执行脚本内容
# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
5)生成镜像
# docker build -t nginx.new .
# docker run -d -P nginx.new
查看容器,可看到80和443端口,分别被随机映射到本地端口
# docker ps -a
Tomcat 是一个免费开源的轻量级web服务器,在中小型企业和并发量不高的场合普遍使用,是开发和调试JSP程序的首选。
1)创建工作目录
创建完工作目录后,需要把jdk软件包下载解压到工作目录
# mkdir tomcat
# cd tomcat
# rz -E
把 jdk-8u91-linux-x64.tar.gz、apache-tomcat-8.5.16.tar.gz复制过来
# vim Dockerfile
#基础镜像centos:7
FROM centos:7
MAINTAINER tomcat
#把软件包解压并移动到相应位置
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
#设置环境变量
ENV JAVA_HOME=/usr/local/jdk1.8.0_91
ENV JRE_HOME=${JAVA_HOME}/jre
ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH=${JAVA_HOME}/bin:$PATH
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16/ tomcat
#开启80端口
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
3)用Dockfile生成镜像
# docker build -t tomcat:centos .
# docker run -d --name tomcat1 -p 8000:8080 tomcat:centos
# docker ps -a
MySQL是当下最流行的关系型数据库,所使用的SQL语言是用于访问数据库的 最常用标准化语言。MySQL具有体积小、速度快、成本低的优势,成为中小型企业首 选的数据库。下面使用Dockerfile文件的方式来创建带有MySQL服务的Docker镜像。
现在做的是mysql 5.7版本
1)创建工作目录
并把所需软件包拉取过来
# mkdir /root/mysql
# cd /root/mysql
# rz -E
把mysql-5.6.26.tar.gz包复制过来
2)创建Dockerfile文件
# vim Dockerfile
FROM centos:7
MAINTAINER mysql5.6
#安装环境依赖包
RUN yum install ncurses-devel ncurses autoconf cmake make -y
RUN yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
#复制 并解压源码包到容器中
ADD mysql-5.6.26.tar.gz /usr/local/
#cmake配置
WORKDIR /usr/local/mysql-5.6.26
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \
-DMYSQL_DATADIR=/home/mysql/ \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock
#编译及安装
RUN make && make install
RUN cp -f support-files/my-default.cnf /etc/my.cnf
ENV PATH=$PATH:/usr/local/mysql/bin
#创建程序用户mysql,并更改属主属组
RUN useradd -s /sbin/nologin mysql
RUN chown -R mysql:mysql /usr/local/mysql/
WORKDIR /usr/local/mysql/scripts/
#初始化数据库
RUN /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
#开放的端口
EXPOSE 3306
WORKDIR /usr/local/mysql-5.6.26
RUN cp support-files/mysql.server /etc/init.d/mysqld
RUN chmod 755 /etc/init.d/mysqld
RUN ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
# 修改配置文件,添加数据库和数据存放路径
RUN sed -i '46s/basedir=/basedir=\/usr\/local\/mysql/g' /etc/init.d/mysqld
RUN sed -i '46s/datadir=/datadir=\/home\/mysql/g' /etc/init.d/mysqld
#启动mysql
ENTRYPOINT ["/usr/local/mysql/bin/mysqld_safe"]
Dockerfile已经写好
3)用Dockfile生成镜像
# docker build -t mysql:centos .
# docker run --name mysql_server -d -P --privileged mysql.new
# docker images
# docker ps -a
可看到映射的端口
5)进入容器,设置权限
grant授权,方便别人远程连接,和自己本地连接数据库
# docker exec -it af8ebeab1964 /bin/bash
进入数据库 进行授权
# mysql -u root -p
回车
> grant all privileges on *.* to 'root'@'%' identified by '111';
> grant all privileges on *.* to 'root'@'localhost' identified by '111';
验证 测试:
在另外一台宿主系统中安装mariadb客户端 远程连接mysql容器
# yum -y install mariadb
# mysql -h 192.168.188.133 -u root -P 32772 -p111
可以登录
创建mysql 5.7版本的镜像
1)创建工作目录
并把所需软件包拉取过来
# mkdir mysql
# cd mysql
# rz -E
把mysql-boost-5.7.20.tar.gz 复制过来
2)创建Dockerfile文件
# vim Dockerfile
FROM centos:7
RUN yum -y install ncurses ncurses-devel bison cmake make gcc gcc-c++
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]
3)编写执行脚本内容
#vim my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
4)用Dockfile生成镜像
# docker build -t mysql:centos .
5)运行容器
# docker run --name=mysql_server -d -P --privileged mysql:centos
# dcoker ps -a
可看到映射的端口
6)进入容器,设置权限
# docker exec -it 容器ID /bin/bash
# mysql -u root -p
> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
> grant all privileges on *.* to 'root'@'localhost' identified by 'abc 123';
验证 测试:
在另外一台宿主系统中安装mariadb客户端 远程连接mysql容器
# yum -y install mariadb
# mysql -h 192.168.195.128 -u root -P 32772 -p111
可以登录