1. 程式人生 > 其它 >docker映象例項

docker映象例項

一、Nginx映象

二、MySQL映象

三、ssh映象

四、systemctl映象

五、Tomcat映象

一、Nginx映象

mkdir /opt/nginx
cd /opt/nginx/
cp /opt/nginx-1.12.0.tar.gz /opt/nginx


FROM centos:7

MAINTAINER SDY

RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin 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
RUN make && make install

ENV PATH /usr/local/nginx/sbin:$PATH

EXPOSE 80
EXPOSE 443

ADD nginx.service /lib/systemd/system/
RUN chmod 754 /lib/systemd/system/nginx.service
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf

ADD rrun.sh /rrun.sh
RUN chmod 755 /rrun.sh
CMD ["/rrun.sh"]

vim rrun.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

//建立新映象
docker build -t nginx:centos7 .

docker run -d -P nginx:centos7

docker ps -a
5df9e4383b96        nginx:centos           "/rrun.sh"           15 seconds ago      Up 15 seconds       0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   silly_davinci

http://192.168.91.5:32769

二、MySQL映象

mkdir /opt/mysql
cd /opt/mysql

FROM centos:7

MAINTAINER SDY

RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin  mysql

ADD mysql-boost-5.7.20.tar.gz /opt/mysql/
WORKDIR /opt/mysql/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 -j2 && make install
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/my.cnf

RUN chown -R mysql:mysql /usr/local/mysql/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/bin/
RUN ./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
CMD ["/usr/sbin/init"]

vim /etc/my.cnf
[client]
port = 3306
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
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
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:centos7 .

//啟動容器,並進行初始化
docker run --name mysql1 -P --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup mysql:centos7
//進容器給許可權
docker ps -a
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                     NAMES
f9a4d8f6c65f   mysql:centos   "/usr/sbin/init"    17 seconds ago   Up 16 seconds   0.0.0.0:49153->3306/tcp   mysql1

//進入容器,授權遠端連線 mysql
docker exec -it f9a4d8f6c65f /bin/bash

mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by '411526';
grant all privileges on *.* to 'root'@'localhost' identified by '411526';
flush privileges;

//在客戶端連線mysql容器
mysql -h 192.168.91.5 -u root -P 49153 -p411526

三、ssh映象

mkdir /opt/ssh
cd /opt/ssh

vim Dockerfile
#第一行必須指明基於的基礎映象
FROM centos:7
#作者資訊
MAINTAINER this is ssh image <wl>
#映象的操作指令
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '411526' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config						#不使用PAM認證
RUN sed -ri '/^session\s+required\s+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd		#取消pam限制
RUN ssh-keygen -t rsa -A														#生成金鑰認證檔案
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D"]


//生成映象
docker build -t ssh:centos7 .

//啟動容器並修改root密碼
docker run -d -P ssh:centos7
docker ps -a
ssh localhost -p 49155

四、systemctl映象

mkdir /opt/systemctl
cd /opt/systemctl

vim Dockerfile

FROM sshd:centos
MAINTAINER this is systemctl image <wl>
ENV container docker
#除了systemd-tmpfiles-setup.service,刪除其它所有檔案
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 systemd:centos .

//啟動容器,並掛載宿主機目錄掛載到容器中,和進行初始化
docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init &
#--privileged:使container內的root擁有真正的root許可權。否則,container內的root只是外部的一個普通使用者許可權。

docker ps -a

//進入容器
docker exec -it a0d624d2bfa9 bash

systemctl status sshd

方法二:
docker run -d -P --privileged sshd:centos /usr/sbin/init &

五、Tomcat映象

mkdir /opt/tomcat
cd /opt/tomcat
cp /opt/jdk-8u91-linux-x64.tar.gz /opt/tomcat
cp /opt/apache-tomcat-8.5.16.tar.gz /opt/tomcat

vim Dockerfile

FROM centos:7
MAINTAINER SDY
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 JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
#ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]


//建立新映象
docker build -t tomcat:centos7 .

docker run -d --name tomcat01 -p 7321:8080 tomcat:centos7
 
http://192.168.91.5:7321