目录
[1. 建立工作目录](#1. 建立工作目录)
[2. 生成镜像](#2. 生成镜像)
[3. 启动容器并修改root密码](#3. 启动容器并修改root密码)
[1. 建立工作目录](#1. 建立工作目录)
[2. 生成镜像](#2. 生成镜像)
[3. 运行镜像容器](#3. 运行镜像容器)
[编辑 4. 测试容器systemct](#编辑 4. 测试容器systemct)
[1. 建立工作目录](#1. 建立工作目录)
[2. 编写Dockerfile脚本](#2. 编写Dockerfile脚本)
[3. 编写run.sh启动脚本](#3. 编写run.sh启动脚本)
[4. 生成镜像](#4. 生成镜像)
[5. 运行镜像容器](#5. 运行镜像容器)
[6. 验证](#6. 验证)
[1. 建立工作目录](#1. 建立工作目录)
[2. 编写Dockerfile脚本](#2. 编写Dockerfile脚本)
[3. 生成镜像](#3. 生成镜像)
[4. 运行镜像容器](#4. 运行镜像容器)
[5. 测试](#5. 测试)
一、构建SSH镜像
1. 建立工作目录
[root@docker ~]# mkdir /opt/sshd
[root@docker ~]# cd /opt/sshd
[root@docker sshd]# vim Dockerfile
FROM centos:7
#第一行必须指明基于的基础镜像
MAINTAINER this is ssh image <test>
RUN yum -y update;\
yum install -y openssh* net-tools lsof telnet passwd;\
echo '123456' | passwd --stdin root;\
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config;\
#不使用PAM认证
sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd;\
#取消pam限制
ssh-keygen -t rsa -A;\
#生成密钥认证文件
mkdir -p /root/.ssh;\
chown root.root /root;\
chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
#/usr/sbin/sshd -D用于前台启动sshd服务
2. 生成镜像
[root@docker sshd]# docker build -t sshd:test .
[root@docker sshd]# docker images
3. 启动容器并修改root密码
启动容器
[root@docker sshd]# docker run -itd --name sshd -P sshd:test
#创建并启动名叫sshd的容器,基于sshd:test的镜像
[root@docker sshd]# docker ps -a
#查看所有容器
ssh登录后修改root密码
[root@docker sshd]# ssh localhost -p 32785
root@localhost's password:
[root@ec51447ac2a3 ~]# echo '654321' | passwd --stdin root
二、systemctl镜像
1. 建立工作目录
[root@docker ~]# mkdir /opt/systemctl
[root@docker ~]# cd /opt/systemctl
[root@docker systemctl]# vim Dockerfile
FROM sshd:test
MAINTAINER this is systemctl iamge <test>
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 /lib/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" ]
2. 生成镜像
docker build -t systemd:centos .
docker images
3. 运行镜像容器
方法一:docker run -itd --privileged -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
##启动容器,并挂载宿主机目录挂载到容器中,和进行初始化
##--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
方法二: docker run --privileged -it -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init &
docker ps -a
4. 测试容器systemct
[root@docker01 systemctl]# docker exec -it systemctl /bin/bash
[root@edfc51d0e379 /]# systemctl status sshd
三、Nginx镜像
1. 建立工作目录
[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx/
[root@docker nginx]# rz -E
#传入nginx安装包nginx-1.22.0.tar.gz
rz waiting to receive.
2. 编写Dockerfile脚本
[root@docker nginx]# vim Dockerfile
FROM centos:7
#基于基础镜像
MAINTAINER this is nginx image <test>
#用户信息
RUN yum -y update;yum install -y pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.22.0.tar.gz /opt/
#添加环境包,上传nginx软件压缩包并解压
WORKDIR /opt/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
EXPOSE 80
EXPOSE 443
#指定http和https端口
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
#添加宿主机中run.sh
RUN chmod 755 /run.sh
CMD [ "/run.sh" ]
3. 编写run.sh启动脚本
[root@docker nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
4. 生成镜像
[root@docker nginx]# docker build -t nginx:test .
[root@docker nginx]# docker images
5. 运行镜像容器
[root@docker nginx]# docker run -itd --name nginx -p 10000:80 nginx:centos
[root@docker nginx]# docker ps -a
6. 验证
[root@docker nginx]# curl http://192.168.80.115:10000
四、Tomcat镜像
1. 建立工作目录
[root@docker ~]# mkdir /opt/tomcat
[root@docker ~]# cd /opt/tomcat
[root@docker tomcat]# rz -E
rz waiting to receive.
#传入jdk安装包jdk-8u91-linux-x64.tar.gz
[root@docker tomcat]# rz -E
rz waiting to receive.
#传入tomcat安装包apache-tomcat-8.5.16.tar.gz
2. 编写Dockerfile脚本
[root@docker tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat image <test>
ADD jdk-8u291-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_291 /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
ENTRYPOINT [ "/usr/local/tomcat/bin/catalina.sh","run" ]
3. 生成镜像
[root@docker tomcat]# docker build -t tomcat:test .
[root@docker tomcat]# docker images
4. 运行镜像容器
[root@docker tomcat]# docker run -d --name tomcat -P tomcat:test
[root@docker tomcat]# docker ps -a
5. 测试
五、mysql镜像
1、创建工作目录
mkdir /opt/mysqld
cd /opt/mysqld
vim Dockerfile
FROM centos:7 MAINTAINER this is mysql image <test>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
ADD boost_1_59_0.tar.gz /usr/local/src/
ADD mysql-5.7.17.tar.gz /usr/local/src/
WORKDIR /usr/local/src/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 -j4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]
2、编写mysql的配置文件
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
3、编写运行文件
vim run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld
4、生成镜像
//创建新镜像
docker build -t mysql:centos .
5、运行镜像容器
//启动容器,并进行初始化
docker run --name=mysql_server -d -P --privileged mysql:centos /usr/sbin/init
6、测试
//进入容器,授权远程连接
mysql docker exec -it f9a4d8f6c65f /bin/bash
ysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
//在客户端连接mysql容器
mysql -h 192.168.80.115 -u root -P 32773 -p123456