Dockerfile构建镜像实战(SSHsystemctlnginxtomcatmysqlPHP镜像)
Posted 王大雏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dockerfile构建镜像实战(SSHsystemctlnginxtomcatmysqlPHP镜像)相关的知识,希望对你有一定的参考价值。
一、构建SSH镜像
mkdir /opt/sshd
cd /opt/sshd
vim Dockerfile
#第一行必须指明基于的基础镜像
FROM centos:7
#作者信息
MAINTAINER this is ssh image <wdc>
#镜像的操作指令
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo 'abc1234' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #不使用PAM认证
RUN sed -ri '/^session\\s+required\\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd #取消pam限制
RUN ssh-keygen -t rsa -A #生成密钥认证文件
RUN mkdir -p /root/.ssh && chown roiot.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"] #/usr/sbin/sshd -D用于前台启动sshd服务
//生成镜像
docker build -t sshd:centos .
//启动容器并修改root密码
docker run -d -p sshd:centos
docker ps -a
ssh localhost -p 49155
二、生成Systemctl镜像
mkdir /opt/systemctl
cd /opt/systemctl
vim Dockerfile
FROM sshd:centos
MAINTAINER this is systemctl image <wdc>
ENV container docker
#除了systemd-tmpfiles-setup.service,删除其它所有文件
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i;done); \\
rm -f /lib/systemd/system/multi-user.target.wants/*; \\
rm -f /etc/systemd/system/*.wants/*; \\
rm -f /lib/systemd/system/local-fs.target.wants/*; \\
rm -f /lib/systemd/system/sockets.target. wants/*udev*; \\
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \\
rm -f /lib/systemd/system/basic.target.wants/*;\\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
#CMD ["/usr/sbin/init"]
//生成镜像
docker build -t systemd:centos
//启动容器,并挂载宿主机目录挂载到容器中,和进行初始化
docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
#--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
docker ps -a
//进入容器
docker exec -it a0d624d2bfa9 bash
systemctl status sshd
方法二:
docker run --privileged -it -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init &
三、nginx镜像
mkdir /opt/nginx
cd /opt/nginx/
cp /opt/nginx-1.12.0.tar.gz /opt/nginx
vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <wdc>
#添加环境包
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 /opt/
#指定工作目录
WORKDIR /opt/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 #关闭nginx 在后台运行
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
docker ps -a
5df9e4383b96 nginx:centos "/run. sh" 15 seconds ago Up 15 seconds 0.0.0.0:32769->80/tcp,0.0.0.0:32768->443/tcp silly_ davinci
http://192.168.80.10:32769
四、tomcat 镜像
mkdir /opt/tomcat
cd /opt/tomcat
cp /opt/jdk-8u91-linux-x64.tar.gz /opt/tomcat
cp /opt/apache-tomcat-8.5.16.tar.gz /opt/tomcat
vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat image <wdc>
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
#CMD ["/usr/local/ tomcat/bin/catalina.sh","run"]
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
//创建新镜像
docker build -t tomcat:centos .
docker run -d --name tomcat01 -p 1216:8080 tomcat:centos
http://192.168.80.10:1216
五、mysql镜像
mkdir /opt/mysqld
cd /opt/mysqld
vim Dockerfile
FROM centos: 7
MAINTAINER this is mysql image <Wdc>
RUN yum -y install gcc gcc-C++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysq1-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 -j4 && 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 \\
--initial ize- 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
ADD run.sh /usr/local/src
RUN chmod 755 /usr/local/src/run.sh
RUN sh /usr/local/src/run.sh
#CMD ["/usr/sbin/init"]
vim my.cnf
[client]
port = 3306
de fault-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/mysq1d.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
vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld
//创建新镜像
docker build -t mysql:centos .
//启动容器,并进行初始化
docker run --name=mysql_server -d -P -privileged mysql:centos /usr/sbin/init
//进容器给权限
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9a4d8f6c65f mysql:centos "/usr/sbin/init" 17 seconds ago Up 16 seconds 0.0.0.0:49153->3306/tcp mysql_server
//进入容器,授权远程连接mysql
docker exec -it f9a4d8f6c65f /bin/bash
mysql -u root -P
grant all privileges on *.* to 'root'@'' identified by 'abc123' ;
grant all privileges on *.* to ' root'@' localhost' identified by 'abc123' ;
flush privileges;
//在客户端连接mysql容器
mysql -h 192.168.80.10 -u root -P 49153 -pabc123
六、php镜像
FROM centos:7.4.1708
ENV TIME_ZOME Asia/Shanghai
ARG WJ="php-7.1.10"
ADD $WJ.tar.bz2 /opt
RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison \\
&& mkdir -p /usr/local/php/etc \\
&& cd /opt/$WJ \\
&& ./configure --prefix=/usr/local/php \\
--with-config-file-path=/usr/local/php/etc \\
--with-gd --with-mysqli \\
--with-openssl --with-zlib --with-curl \\
--with-jpeg-dir --with-png-dir --with-iconv \\
--enable-fpm --enable-zip --enable-mbstring \\
&& make -j 2 \\
&& make install \\
&& cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \\
&& cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \\
&& sed -i '/;daemonize/a\\daemonize = no' /usr/local/php/etc/php-fpm.conf \\
&& sed -i 's/127.0.0.1/0.0.0.0/g' /usr/local/php/etc/php-fpm.d/www.conf \\
&& echo "${TIME_ZOME}" > /etc/timezone \\
&& ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \\
&& rm -rf /opt/local/php* \\
&& yum clean all \\
&& yum -y remove gcc gcc-c++ make \\
&& mkdir -p /usr/local/nginx/html \\
&& echo -e "<?php\\nphpinfo();\\n?>" > /usr/local/nginx/html/index.php
WORKDIR /usr/local/php/
EXPOSE 9000
CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]
查看镜像构建情况
docker image ls
以上是关于Dockerfile构建镜像实战(SSHsystemctlnginxtomcatmysqlPHP镜像)的主要内容,如果未能解决你的问题,请参考以下文章