一、构建Apache镜像
cd /opt/
#建立工作目录
mkdir /opt/apache
cd apache/vim Dockerfile
#基于的基础镜像
FROM centos:7
#维护镜像的用户信息
MAINTAINER this is apache image <cyj>
#镜像操作指令安装Apache软件
RUN yum install -y httpd
#开启80端口
EXPOSE 80
#复制网站首页文件
ADD index.html /var/www/html/
#将执行脚本复制到镜像中
ADD run.sh /run.sh
RUN chmod +x /run.sh
#启动时执行脚本
CMD ["/run.sh"]
##准备执行脚本
vim run.sh
#!/bin/bash
rm -rf /run/httpd/* #清理httpd的缓存
/usr/sbin/apachectl -D FOREGROUND #指定为前台运行
#因为Docker容器仅在它的1号进程(PID为1) 运行时,会保持运行。如果1号进程退出了,Docker容器也就退出了。//准备网站页面
echo "this is test dockerfile web" > index.html//生成镜像
docker build -t httpd:centos . #注意!!!末尾的“.”不要忘记//新镜像运行容器
docker run -d -p 1314:80 httpd:centos #大写P随机端口,小写P指定端口//浏览器访问测试
http://192.168.226.13:1314
二、构建SSH镜像
systemctl stop firewalld
setenforce mkdir /opt/sshd
cd /opt/sshdvim Dockerfile
#第一行必须指明基于的基础镜像
FROM centos:7
#作者信息
MAINTAINER this is ssh image <cyj>
#镜像的操作指令
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123456' | 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 root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"]
//生成镜像
docker build -t sshd:centos .//启动容器并修改root密码
docker run -d -P sshd:centos
docker ps -a
ssh localhost -p
三、构建Systemctl镜像
mkdir /opt/systemctl
cd /opt/systemctlvim DockerfileFROM sshd:centos
MAINTAINER this is systemctl image <cyj>
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 -dit -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemctl:centos /sbin/init
#--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。docker ps -a//进入容器
docker exec -it 32ba87622cfa bashsystemctl status sshd方法二:
docker run -d -P --privileged sshd:centos /usr/sbin/init &
四、构建Nginx镜像
mkdir /opt/nginx
cd /opt/nginx/
cp /opt/nginx-1.22.0.tar.gz /opt/nginxvim Dockerfile#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <cyj>
#添加环境包
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.22.0.tar.gz /usr/local/src/
#指定工作目录
WORKDIR /usr/local/src/nginx-1.22.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"]
#CMD ["/usr/local/sbin/nginx", "-g", "daemon off;"]
vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx//创建新镜像
docker build -t nginx:centos .docker run -d -P nginx:centosdocker ps -ahhttp://192.168.91.106:32771
浏览器访问
五、构建Tomcat镜像
mkdir /opt/tomcat
拷贝jdk-8u91-linux-x64.tar.gz
apache-tomcat-9.0.16.tar.gz 到/opt/tomcat下cd /opt/tomcatvim DockerfileFROM centos:7
MAINTAINER this is tomcat image <hmj>
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"]
CMD ["/usr/local/tomcat/bin/startup.sh","start"]
//创建新镜像
docker build -t tomcat:centos .docker run -d --name tomcat01 -p 1216:8080 tomcat:centos http://192.168.91.106:32772
六、构建Mysql镜像
mkdir /opt/mysqld
cd /opt/mysqldvim DockerfileFROM centos:7ADD mysql-boost-5.7.17.tar.gz /usr/localRUN yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
gcc \
gcc-c++ \
makeRUN useradd -M -s /sbin/nologin mysqlWORKDIR /usr/local/mysql-5.7.17/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_EXTRA_CHARSETS=all \
-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 installWORKDIR /opt/kiki/mysql
ADD my.cnf /etcRUN chown -R mysql:mysql /usr/local/mysql/
RUN chown mysql:mysql /etc/my.cnfENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/libWORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/dataRUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" >/run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]
[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 = 1sql_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
#!/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/bashmysql -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