1. 创建Dockerfile
1.1. 创建Dockerfile
本例子用安装lnmp1.4的包
# Use parent image
FROM centos:centos7
USER root
# Set the working directory to /www
WORKDIR /www
# Copy the current directory contents into the container at /www
ADD . /www
# Install any needed packages
RUN rpm --rebuilddb && yum -y install wget tar
RUN wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz
RUN tar -xvf setuptools-1.4.2.tar.gz && cd setuptools-1.4.2 && python setup.py install && easy_install pip
RUN wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && pwd && ls -la
RUN pip install pexpect
RUN python pexpect_init.py
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME LNMP
# Run when the container launches
CMD ["/bin/bash"]
2. 自动安装lnmp脚本 pexpect_init.py
-用来自动安装lnmp1.4的包
#!/usr/bin/python
import sys
import pexpect
password = 'password'
expect_list = [
'You have 5 options for your DataBase install',
'Please setup root password of MySQL',
'Do you want to enable or disable the InnoDB Storage Engine',
'You have 6 options for your PHP install',
'You have 3 options for your Memory Allocator install',
'Press any key to install'
]
answer_list = [
'3',
'root',
'Y',
'7',
'1',
'y'
];
cmd = "cd /www/lnmp1.4/ && ./install.sh lnmp"
p = pexpect.spawn("/bin/bash", ["-c", cmd])
p.logfile_read = sys.stdout
p.timeout = 3600*50
p.maxread = 1
try:
while True:
idx = p.expect(expect_list)
print p.before + expect_list[idx],
print answer_list[idx]
p.sendline(answer_list[idx])
except pexpect.TIMEOUT:
print >>sys.stderr, 'timeout'
except pexpect.EOF:
print p.before
print >>sys.stderr, '<the end>'