1、构建SSh镜像

mkdir sshd
cd sshd

vim Dockerfile
FROM centos:7
MAINTAINER gt
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
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/session\s+required\s+pam_loginuid.so/s//#/' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_java
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_tomcat_02
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_tomcat_03

2、Systemctl镜像

vim Dockerfile

基于上一题的生成的sshd镜像
FROM sshd:new

设置变量 container 值为docker
ENV container docker

切换到/lib/systemd/system/sysinit.target.wants/目录下,使用for循环删除除了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多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_mysql_04
docker build -t systemd:new .

//privateged container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
docker run --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &

//进入容器
docker exec -it 9856c636c38a bash
systemctl status sshd
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_05
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_mysql_06
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_tomcat_07
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_docker_08

3、Tomcat镜像

mkdir tomcat
cd tomcat/
拷贝apache-tomcat-9.0.16.tar.gz和jdk-8u201-linux-x64.rpm

docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_09

vim Dockerfile

基于基础镜像

FROM centos:7

用户信息

MAINTAINER gt

复制rpm包,并安装

COPY jdk-8u201-linux-x64.rpm /
RUN rpm -ivh jdk-8u201-linux-x64.rpm

配置环境变量

ENV JAVA_HOME /usr/java/jdk1.8.0_201-amd64
ENV CLASSPATH /usr/java/jdk1.8.0_201-amd64/lib/tools.jar:/usr/java/jdk1.8.0_201-amd64/lib/dt.jar
ENV PATH /usr/java/jdk1.8.0_201-amd64/bin:$PATH

解压tomcat包到容器,并改名为tomcat

ADD apache-tomcat-9.0.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat

开启tomcat服务,ENTRYPOINT用于设置容器开启时的第一条命令,tomcat的启动脚本是catalina.sh

ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_10
docker build -t tomcat:centos7 .
docker run -d --name tomcat1 -p 1212:8080 tomcat:centos7

http://192.168.148.10:1212
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_tomcat_11
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_java_12
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_13

4、MYSQL镜像

mkdir mysql
cd mysql/
将mysql-boost-5.7.20.tar.gz放入
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_mysql_14

编写my.cnf文件用于替换容器内部的

vim my.cnf
[client]
port = 3306
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
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
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
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_15
vim Dockerfile

基础镜像

FROM centos:7

作者信息

MAINTAINER li

安装环境依赖包

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 /opt
WORKDIR /opt/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_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 -j4 && make install

修改mysql 配置文件

RUN rm -f /etc/my.cnf
COPY my.cnf /etc

更改mysql安装目录和配置文件的属主属组

RUN chown -R mysql:mysql /usr/local/mysql/
RUN chown mysql:mysql /etc/my.cnf

设置路径环境变量

ENV PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

初始化数据库

WORKDIR /usr/local/mysql/
RUN bin/mysqld
--initialize-insecure
--user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data

添加mysqld系统服务

RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

开放端口

EXPOSE 3306

制作启动脚本,开始mysql服务

RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" >/run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]
///////////////////////////////////////////////

docker build -t mysql:centos7 .
docker images
docker run --name=mysql_server -d -P --privileged mysql:centos7
docker ps -a
docker exec -it fe467047ad6b /bin/bash
mysql -uroot -p
回车,此时没有密码

GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'abc123';
GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' IDENTIFIED BY 'abc123';
flush privileges;

另开终端,用于测试
yum -y install mariadb*
docker ps -a
mysql -h 192.168.163.100 -u root -P 49157 -pabc123
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_16
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_tomcat_17
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_mysql_18
docker多个镜像部署(ssh,systemctl,nginx,tomcat,mysql)_centos_19