●Dockerfile是由一組指令組成的檔案
●Dockerfile結構四部分
基礎映象資訊
維護者資訊
映象操作指令
容器啟動時執行指令
●Dockerfile每行支援一條指令,每條指令可攜帶多個引數,支援使用以「#」號開頭的註釋
●Dockerfile操作指令
CMD與ENTRYPOINT 指令對比:
CMD指令可以指定容器啟動時預設執行的命令,但它可以被docker run命令的引數覆蓋掉。
ENTRYPOINT 指令和CMD類似,它也是使用者指定容器啟動時要執行的命令,但如果dockerfile中也有CMD指令,CMD中的引數會被附加到ENTRYPOINT指令的後面。 如果這時docker run命令帶了引數,這個引數會覆蓋掉CMD指令的引數,並也會附加到ENTRYPOINT 指令的後面。
這樣當容器啟動後,會執行ENTRYPOINT 指令的引數部分。
可以看出,相對來說ENTRYPOINT指令優先順序更高。
[root@localhost ~]# mkdir sshd/
[root@localhost ~]# cd sshd/
[root@localhost sshd]# vim Dockerfile
FROM centos:7
MAINTAINER The chen
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 build -t sshd:new
#啟動容器
docker run -d -P sshd:new
docker ps -a #查詢系統自動對映的埠號
ssh localhost -p 32770 #宿主機通過埠連線ssh容器
[root@localhost systemctl]# mkdir systemctl
[root@localhost systemctl]# cd systemctl
[root@localhost 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"]
[root@localhost systemctl]# docker build -t systemd:new .
#privileged container內的root擁有真正的root許可權。否則,container內的root只是外部的一個普通使用者許可權。
[root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
[1] 22579
[root@localhost systemctl]# docker exec -it 1931731ffe2d bash
[root@1931731ffe2d /]# systemctl status sshd
[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx
[root@localhost nginx]# vim Dockerfile
FROM centos:7
MAINTAINER The chen
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#ADD既能把壓縮包複製到容器中,同時還能把壓縮包進行解壓
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
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
#需要關閉nginx的守護行程,否則會和容器的守護行程衝突
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
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
[root@localhost nginx]# docker build -t nginx:new .
存取http://14.0.0.10:32770/驗證
[root@localhost ~]# mkdir tomcat
[root@localhost ~]# cd tomcat
apache-tomcat-9.0.16.tar.gz和jdk-8u201-linux-x64.rpm上傳到/root/tomcat
目錄下
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER The chen
ADD jdk-8u201-linux-x64.rpm /root
WORKDIR /root
RUN rpm -ivh jdk-8u201-linux-x64.rpm
ENV JAVA_HOME /usr/java/jdk1.8.0_201-amd64
ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-9.0.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
#建立tomcat映象並利用tomcat映象建立容器
[root@localhost tomcat]# docker build -t tomcat:new .
[root@localhost tomcat]# docker run -d --name tomcat1 -p 1226:8080 tomcat:new
存取http://14.0.0.10:1226/驗證
[root@localhost ~]# mkdir mysql
[root@localhost ~]# cd mysql #將mysql-boost-5.7.20.tar.gz軟體包上傳到指定位置
[root@localhost mysql]# ll
總用量 47696
-rw-r--r--. 1 root root 48833145 10月 23 2017 mysql-boost-5.7.20.tar.gz
[root@localhost mysql]# vim Dockerfile
FROM centos:7
MAINTAINER build image mysql
EXPOSE 3306
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20
RUN useradd mysql -M -s /sbin/nologin
RUN yum -y install gcc \
gcc-c++ \
make \
ncurses \
ncurses-devel \
bison \
cmake
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
RUN make -j4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
RUN /usr/local/mysql/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 /lib/systemd/system/
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[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/mysql.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 run.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld
#建立mysql映象並利用mysql映象建立容器
[root@localhost tomcat]# docker build -t mysql:new .
[root@localhost tomcat]# docker run -d --name mysql --privileged -p 1226:8080 mysql:new