指令 | 含義 |
---|---|
FROM 映象 | 指定新映象所基於的映象,第一條指令必須為FROM指令, 每建立一個映象就需要一條FROM指令 |
MAINTAINER 名字 | 說明新映象的維護人資訊 |
RUN 命令 | 在所基於的映象執行命令,並提交到新的映象中 |
CMD [ 「要執行的程式」,「引數1」,「引數2」] | 指令啟動容器時要執行的命令或者指令碼,Dockerfile只能 有一條CMD命令,如果指定多條則只能執行最後一條 |
EXPOSE 埠號 | 指定新映象載入到Docker時要開啟的埠 |
ENV 環境變數 變數值 | 設定一個環境變數的值,會被後面的RUN使用 |
ADD 原始檔/目錄 目標檔案/目錄 | 將原始檔複製到目標檔案,原始檔要與Dockerfile位於 相同目錄中,或者是一個URL |
COPY 原始檔/目錄 目標檔案/目錄 | 將本地主機上的檔案/目錄複製到目標地點,原始檔/目錄 要與Dockerfile在相同的目錄中 |
VOLUME [「目錄」] | 在容器中建立一個掛載點 |
USER 使用者名稱/UID | 指定執行容器時的使用者 |
WORKDIR 路徑 | 為後續的RUN、CMD、ENTRYPOINT指定工作目錄 |
ONBUILD 命令 | 指定所生成的映象作為一個基礎映象時所要執行的命令 |
HEALTHCHECK | 健康檢查 |
環境構建
建立目錄,編寫Dockerfile檔案
[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir sshd
[root@localhost opt]# cd sshd
[root@localhost sshd]# vim Dockerfile
FROM centos:7 '指定基礎映象'
MAINTAINER The Centos Project '描述資訊,後面可以自行編寫'
RUN yum -y update '更新yum源'
RUN yum -y install openssh* net-tools lsfo telnet passwd '安裝環境工具,分別為ssh服務軟體包,網路管理工具,檢視埠命令'
RUN echo '123456' | passwd --stdin root '設定root登陸密碼'
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config '關閉ssh中的pam驗證'
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 '禁用pam的ssh的pam對談模組'
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh '建立ssh工作目錄和許可權設定'
EXPOSE 22 '暴露22埠'
CMD ["/usr/sbin/sshd","-D"] '容器載入時啟動sshd服務,-D表示守護行程'
[root@localhost opt]# docker build -t sshd:new .
[root@localhost opt]# docker run -d -P sshd:new
7feffba8f590eb17fda8a0bd03a11344d365f0ccb41c03187ccab093f88539ae
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7feffba8f590 sshd:new "/usr/sbin/sshd -D" 15 seconds ago Up 15 seconds 0.0.0.0:32768->22/tcp relaxed_wozniak
[root@localhost opt]# ssh localhost -p 32768
mkdir systemctl
cd systemctl
[root@localhost systemctl]# vim Dockerfile
FROM sshd:new
ENV container docker '設定環境變數,container和docker'
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [$i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done);\ '執行先到指定目錄,進行for迴圈遍歷目錄下所有檔案並刪除指定的檔案。之後進行一系列的刪除'
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"] 'init指執行初始化'
docker build -t systemd:new .
[root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
'–privateged 使container內的root擁有真正的root許可權,不進行降權處理。否則,container內的使用者只是外部的一個普通使用者'
cd /opt
mkdir nginx
cd nginx/
'拷貝原始碼包到nginx中'
[root@localhost nginxl]# vim Dockerfile
FROM centos:7
#使用者資訊
MAINTAINER this is nginx
#新增環境包
RUN yum -y update
RUN yum install gcc gcc-c++ pcre pcre-devel zlib-devel make -y
RUN useradd -M -s /sbin/nologin nginx
#下載nginx軟體包
ADD nginx-1.12.0.tar.gz /usr/local/src
#指定工作目錄
WORKDIR /usr/local/src/nginx-1.12.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
#指定http和https埠
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf '指關閉守護行程啟動'
#新增宿主機中run.sh到容器中
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 .
[root@localhost nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx new d2273e9320d2 About a minute ago 516MB
centos 7 7e6257c9f8d8 5 weeks ago 203MB
[root@localhost nginx]# docker run -d -P nginx:new
e096d6848ff50cb304c81a4ceaa05e77e7764f500f638678e03087136062bd8e
[root@localhost nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e096d6848ff5 nginx:new "/run.sh" 6 seconds ago Up 6 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp pedantic_knuth
mkdir /opt/tomcat
cd /opt/tomcat
[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"]
'此處除了使用CMD,還可以使用ENTRYPOINT'
'CMD與ENTRYPOINT的區別'
1、ENRYPOINT指開啟容器前映象就已經執行了括號內的命令
2、CMD是開啟容器時,要執行的指令,設定容器啟動後預設執行的命令及其引數,但 CMD 能夠被 docker run 後面跟的命令列引數替換
3、基於Dockerfile內有CMD或者ENTRYPOINT建立映象時,docker run 後面就不要加指令(/bin/bash)了,會覆蓋掉Dockerfile中的指令或者語法報錯
docker build -t tomcat:centos .
docker run -d --name tomcat01 -p 1234:8080 tomcat:centos
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql
RUN yum -y update
RUN yum -y install gcc \
gcc-c++ \
make \
ncurses \
ncurses-devel \
bison \
cmake
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/bash \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN chmod +x /run.sh
RUN sh /run.sh
CMD ["init"]
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
docker build -t mysql:new
docker run --name=mysql_server -d -P --privileged mysql:new
'privileged表示不降權'
[root@localhost mysql]# docker exec -it b4c5c45620e9 /bin/bash
[root@b4c5c45620e9 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
mysql> exit
在另外一臺節點伺服器上
yum install -y mariadb
mysql -u root -P 32772 -p
密碼:abc123
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