Dockerfile姒�杩�

��Dockerfile��docker涓�������浠剁������杩版��浠讹�璇寸���寸�界�瑰氨��������浠跺�板����变�涔�涓�瑗夸�姝ユ�ユ��������
��岛�濡�锛�浣��ㄦ�疏浚�涓�涔颁�涓�涓�琛f�讹�浣�����疏浚跺苟娌℃��缁�浣���涓�涓�疏浚��寸��琛f�讹�����涓�河渠�缁�浠跺��涓�寮��剧焊锛�浣����ц�涓��剧焊涓�姝ヤ�姝ュ�琛f�剁�瑁�璧锋�ワ�广大辨��河渠�浣�����瑕����山�����d� Dockerfile 广大辨��杩�寮��剧焊锛�������浠跺氨��浣���瑕���杩�涓�琛f�讹�Dockerfile 涓�寤鸿����岛垮�藉��锛�广大辩�� Dockerfile��
����姝わ�Dockerfile�跺���ㄥ����河渠�涓��℃�$����浠わ�姣�涓��℃��浠ゆ��寤轰�灞�锛���姝ゆ��涓��℃��浠ょ����疏浚癸�广大辨����杩拌�ュ�搴�褰�濡�浣���寤恒��

Docker �ц� Dockerfile ��澶ц�存�绋�锛�

锛�1锛�docker浠��雾�����杩�琛�涓�涓�疏浚瑰��锛�
锛�2锛��ц�涓��℃��浠ゅ苟瀵瑰�瑰�ㄤ��轰慨�戊寅�
锛�3锛��ц�绫讳技docker commit����浣���河渠や�涓��扮演������灞�锛�
锛�4锛�docker���轰�����河渠ょ������杩�琛�涓�涓��板�瑰��锛�
锛�5锛��ц�dockerfile涓���涓�涓��℃��浠ょ�村�版������浠ら�芥�ц�疏浚�����

涓����舵�碉�

1��Dockerfile 锛���潘�浠剁��������锛���瑕�疏浚�涔�涓�涓�Dockerfile锛�Dockerfile疏浚�涔�河渠�杩�绋���瑕���涓���涓�瑗裤��Dockerfile娑�������疏浚瑰�����ц�浠g����������浠躲����澧�������岛�璧�����杩�琛��剁��澧����ㄦ���炬�ュ�绛�绛�锛�

2��Docker���� 锛���潘�浠剁��河渠や���锛���Dockerfile疏浚�涔�涓�涓���浠朵���锛�docker build�朵�河渠х��涓�涓�Docker����锛�褰�杩�琛� Docker�����讹�浼���姝e�彭���岛����★�

3��Docker疏浚瑰�� 锛�����浠ヨ�や负��潘�浠剁��杩�琛���锛�疏浚瑰�ㄦ���存�ユ��岛����$����

Dockerfile �����拌��瑙o�

��浠� ��涔�
FROM ����
��疏浚��伴�������轰�������锛�绗�涓��℃��浠ゅ�椤讳负FROM��浠�,姣���寤轰�涓�����广大遍��瑕�涓���FROM��浠�
MAINTAINER ��海洋�
璇存���伴������缁存�や汉淇℃��
RUN �戒护
�ㄦ���轰�������涓��ц��戒护锛�骞舵��河渠ゅ�版�扮演������涓�
CMD [��瑕�杩�琛���绋�搴���,������1��,������2��]
��浠ゅ���ㄥ�瑰�ㄦ�惰�杩�琛����戒护��������锛�Dockerfile���芥��涓���CMD�戒护,濡�����疏浚�澶��″�����芥����涓��¤��ц�
EXPOSE 绔��e��
��疏浚��伴������潘藉��Docker�惰�寮�����绔���
ENV ��澧����� ������
璁剧疆涓�涓���澧��������硷�浼�琚����㈢��RUN浣跨��
ADD 婧���浠�/��褰� ������浠�/��褰�
广大�婧���浠跺��跺�扮演������浠讹�婧���浠惰�涓�Dockerfile浣�河渠��稿����褰�涓�锛� ������涓�涓�URL
COPY 婧���浠�/��褰� ������浠�/��褰�
广大����颁富�轰�����浠�/��褰�澶��跺�扮演�����扮演�戊寅�婧���浠�/��褰�瑕�涓�Dockerfile�ㄧ�稿������褰�涓�
VOLUME [����褰���]
�ㄥ�瑰�ㄤ腑��寤轰�涓���潘界��
USER �ㄦ�山��/UID
��疏浚�杩�琛�疏浚瑰�ㄦ�剁���ㄦ��
WORKDIR 璺�寰�
涓哄��缁���RUN��CMD��ENTRYPOINT��疏浚� 宸ヤ���褰�
ONBUILD �戒护
��疏浚�������������浣�涓轰�涓��雾������舵��瑕�杩�琛����戒护
HEALTHCHECK
�ュ悍妫���

Dockerfile疏浚���

1����寤�sshd����

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir sshd  ##��寤虹��褰�
[root@localhost opt]# cd sshd/
[root@localhost sshd]# vim Dockerfile  ##缂���dockerfile��浠�

FROM centos    ##涓�杞介����
MAINTAINER this is sshd <xu>   ##��杩颁俊��
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"]
[root@localhost sshd]# docker build -t sshd:new .   ##��寤洪����
89432272695ab560b18de75a064428e4a7c4a52dfce223afd2e85132ae6c3c72
[root@localhost sshd]# docker run -d -P sshd:new  ##��寤烘��广大���疏浚瑰��
[root@localhost sshd]# docker ps -a  ##�ョ��疏浚瑰�ㄧ�舵��
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
89432272695a        sshd:new            "/usr/sbin/sshd -D"   7 seconds ago       Up 6 seconds        0.0.0.0:32768->22/tcp   sad_fermi
[root@localhost sshd]# ssh localhost -p 32768  ##��ssh�诲�����

2����寤�systemctl����

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir systemctl   ##��寤虹��褰�
[root@localhost opt]# cd systemctl/
[root@localhost systemctl]# vim Dockerfile   ##缂���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"]
[root@localhost systemctl]# docker build -t systemd:lasted .   ##��寤洪����
[root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro systemd:lasted /sbin/init
##privateged container ����root�ユ����姝g��root����锛�����锛�container����root����澶��ㄧ��涓�涓������ㄦ�锋������
[root@localhost ~]# docker exec -it 23a50d568c75 bash  ##杩��ュ�瑰��
[root@23a50d568c75 /]# systemctl status sshd   ##�ョ���舵��

3����寤�Nginx����

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir nginx   ##��寤�Nginx��褰�
[root@localhost opt]# cd nginx/
[root@localhost nginx]# vim Dockerfile

FROM centos:7
MAINTAINER The is nginx <xu>
RUN yum install -y proc-devel gcc gcc-c++ zlib zlib-devel make openssl-devel wget
ADD nginx-1.12.2.tar.gz /usr/local
WORKDIR /usr/local/nginx-1.12.2/
RUN ./configure --prefix=/usr/local/nginx && make && make install
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[root@localhost nginx]# vim run.sh

#!/bin/bash
/usr/local/nginx/sbin/nginx   ##寮���Nginx����
[root@localhost nginx]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/  ##��杞介����
Password for root@//192.168.100.3/LNMP-C7:  
[root@localhost nginx]# cp /mnt/nginx-1.12.2.tar.gz ./   ##澶��跺�板�����褰�涓�
[root@localhost nginx]# docker build -t nginx:new .   ##��寤洪����
[root@localhost nginx]# docker run -d -P nginx:new    ##��寤哄�瑰��
228c1f5b8070d52c6f19d03159ad93a60d682a586c0b1f944dc651ee40576a3e
[root@localhost nginx]# docker ps -a   ##�ョ��疏浚瑰��
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                                           NAMES
228c1f5b8070        nginx:new           "/run.sh"                9 seconds ago       Up 8 seconds                  0.0.0.0:32770->80/tcp, 0.0.0.0:32769->443/tcp   busy_booth

�ㄦ�瑙��ㄨ�块��缃�椤�

Dockerfile��璁�+疏浚���锛�锛�锛�

4����寤�Tomcat����

[root@localhost opt]# mkdir tomcat
[root@localhost opt]# cd tomcat
[root@localhost tomcat]# cp /mnt/tomcat/jdk-8u91-linux-x64.tar.gz ./ ##澶��跺�板�����褰�
[root@localhost tomcat]# cp /mnt/Tomcat1/tomcat/apache-tomcat-9.0.16.tar.gz ./
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat
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 JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat8
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"]
[root@localhost tomcat]# docker build -t tomcat:centos .  ##��寤洪����
[root@localhost tomcat]# docker run --name tomcat01 -p 1234:8080 -it  tomcat:centos /bin/bash
##��寤哄�瑰��

�╃�ㄦ�瑙��ㄨ�块��

Dockerfile��璁�+疏浚���锛�锛�锛�

5����寤�MySQL����

[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql
[root@localhost mysql]# cp /mnt/mysql-boost-5.7.20.tar.gz ./  ##广大���缂╁��澶��跺�板�����褰�涓�
[root@localhost 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
[root@localhost mysql]# vim Dockerfile   ##缂���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/mysql/mysql.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"]
[root@localhost mysql]# docker build -t centos:mysql .   ##��寤洪����
[root@localhost mysql]# docker run --name=mysql_server -d -P --privileged centos:mysql 
##��寤哄�瑰��
[root@localhost mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                     NAMES
998dc9797102        centos:mysql        "init"              About a minute ago   Up About a minute   0.0.0.0:32768->3306/tcp   mysql_server
[root@localhost mysql]# docker exec -it 998dc9797102 /bin/bash

[root@998dc9797102 mysql]# mysql
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
[root@localhost ~]# mysql -h 192.168.13.128 -u root -P 32768 -pabc123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>