简单的Dockerfile书写(实例!!!)
一、Apache
mkdir apache
cd apache
vim Dockerfile
#基于的基础镜像
FROM centos
#维护镜像的用户信息
MAINTAINER The porject
#镜像操作指令安装apache软件
RUN yum -y update
RUN yum -y install httpd
#开启80端口
EXPOSE 80
#复制网站首页文件
ADD index.html /var/www/html/index.html
#将执行脚本复制到镜像中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
#启动容器时执行脚本
CMD ["/run.sh"]
vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND
echo "web test" > index.html
#生成镜像
docker build -t httpd:centos .
#新镜像运行容器
docker run -d -p 1216:80 httpd:centos
二、MySQL 5.7.20
mount.cifs //192.168.142.1/LNMP /mnt
cd /mnt
cp mysql-boost-5.7.20.tar.gz /opt
cd /opt
mkdir mysql
mv mysql-boost-5.7.20.tar.gz /opt/mysql
cd 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
vim Dockerfile
FROM centos:7
RUN yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
make \
gcc \
gcc-c++
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysq1/mysq1.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 && 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 \
--initialize-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
RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]
docker build -t centos:mysql .
docker run --name=mysql_server -d -P --privileged centos:mysql
#进容器给权限
docker exec -it 1fbcbbfec033 /bin/bash
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
#宿主系统安装mairdb客户端连接mysql容器
mysql -h 192.168.142.128 -u root -P 32770 -pabc23
三、Tomcat
mount.cifs //192.168.142.1/tomcat /mnt
cd /mnt
cp jdk-8u91-linux-x64.tar.gz /opt
cp apache-tomcat-8.5.16.tar.gz /opt
cd /opt
mkdir /tomcat
#拷贝jdk-8u91-linux-x64.tar.gz
#apache-tomcat-8.5.16.tar.gz到/opt/tomcat下
mv apache-tomcat-8.5.16.tar.gz /opt/tomcat/
mv jdk-8u91-linux-x64.tar.gz /opt/tomcat/
cd /opt/tomcat
vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat image
ADD jdk-8u91-linux-x64.tar.gz /root
RUN mv /root/jdk1.8.0_91/ /usr/local/jdk1.8
ENV JAVA_HOME=/usr/local/jdk1.8
ENV CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH=$JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /root
RUN mv /root/apache-tomcat-8.5.16/ /usr/local/tomcat
RUN ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/;ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && /bin/bash && tail -f /usr/local/tomcat/logs/catalina.out
docker build -t tomcat:centos .
docker run -d --name tomcat01 -p 1216:8080 -it tomcat:centos
四、Ngnix1.9.7
mkdir nginx
cd nginx
vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER The is nginx
#添加环境包
RUN yum install -y proc-devel gcc gcc-c++ zlib zlib-devel make openssl-devel wget
#下载nginx软件包
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz
RUN tar zxvf nginx-1.9.7.tar.gz
#指定工作目录
WORKDIR nginx-1.9.7/
RUN ./configure --prefix=/usr/local/nginx && make && make install
#指定http和https端口
EXPOSE 80
EXPOSE 443
#关闭守护进程
RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
WORKDIR /root/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 build -t nginx:new .
docker run -d -P nginx:new
五、ssh
mkdir sshd
cd sshd
vim Dockerfile
FROM centos
MAINTAINER The CentOS Project
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\+repuired\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 build -t sshd:new .
#启动容器并修改root密码
docker run -d -P sshd:new
ssh localhost -p 32770
六、systemctl
mkdir systemctl
cd systemctl
vim Dockerfile
FROM sshd:new
ENV container docker
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 local/c7-systemd:latest .
#privateged container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
docker run --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro local/c7-systemd:latest /sbin/init
#进入容器
docker exec -it 容器ID bash
systemctl status sshd
七、删除冗余镜像
#!/bin/bash
docker ps -a | grep "Exited" | awk '{print $1 }' | xargs docker stop
docker ps -a | grep "Exited" | awk '{print $1 }' | xargs docker rm
docker images | grep none | awk '{print $3 }' | xargs docker rmi
分享名称:简单的Dockerfile书写(实例!!!)
文章URL:http://pwwzsj.com/article/gsesjp.html