Dockerfile example - (hands-on!)

Dockerfile Overview

 Dockerfile docker in the image file is a description file, said bluntly point is the image file in the end is what constituted a step by step.
 For example: You buy on Taobao a hanger, but the seller does not give you a complete hanger, but some components and a drawing, you step up the hanger assembly in accordance with the drawing step, it would be what you need look. So Dockerfile is this drawing, the image file is what you need this hanger, Dockerfile not recommended for casual name, use Dockerfile.
 Thus, Dockerfile inside a section comprising instructions, each instruction to build a layer, and therefore the content of each instruction, is to describe how to construct the layer should be.

Docker implementation of the general process of Dockerfile:

(1)docker从基础镜像运行一个容器;

(2)执行一条指令并对容器作出修改;

(3)执行类似docker commit的操作提交一个新的镜像层;

(4)docker再基于刚提交的镜像运行一个新容器;

(5)执行dockerfile中的下一条指令直到所有指令都执行完成。

different stage:

1, Dockerfile: raw materials software, you need to define a Dockerfile, Dockerfile defines everything required for the process. SUMMARY Dockerfile relates to execute code or include files, environment variables, dependencies, runtime environment, dynamic link libraries and the like;

2, Docker Mirror: is the software deliverables, define a file after using Dockerfile, Docker will produce a mirror image when docker build, when you run Docker mirror, will really begin to provide services;

3, Docker containers: you can think of software running state, the container is directly providing services.

Dockerfile explain the parameters:

FROM: specify the new image which is constructed from the base image;
MAINTAINER: indicates mirroring defenders and their contact information;
RUN: execute any command;
CMD: Specifies the command to start a container to run, Dockerfile can have multiple CMD command, but only the last one will work, the CMD will be replaced by the parameter after Docker rUN;
EXPOSE: service port declarations vessel operation;
ENV: Construction mirroring process set environment variables;
the ADD: copy directory or a file on the host to mirroring in (will help you automatically extract, without additional operation);
COPY: Function and ADD is similar, but does not support automatic download and unzip;
ENTRYPOINT: Specifies the command to start a container to run, similar to the use of CMD, just have to start from the ENTRYPOINT the program will not be overwritten docker run command line parameters specified, and these parameters will be used as command line parameters passed to the program specified ENTRYPOINT;
the vOLUME: a container data volume, the container designated mount point to host auto-generated catalog or other containers (data retention and persistent work, but a Generally not used in Dockerfile, the more common command or when docker run -v specifies data volume);.
WORKDIR: the equivalent of cd command, change directory path;

Dockerfile example - (hands-on!)

1, the mirror constructed 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, constructed mirror 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拥有真正的root权限,否则,container内的root只是外部的一个普通用户权限。
[root@localhost ~]# docker exec -it 23a50d568c75 bash  ##进入容器
[root@23a50d568c75 /]# systemctl status sshd   ##查看状态

3, the mirror constructed 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 example - (hands-on!)

4, the mirror constructed 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 example - (hands-on!)

5, image build 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)]> 

thanks for reading!

Guess you like

Origin blog.51cto.com/14080162/2464376