文章目录 Dockerfile镜像实例 一、构建SSH镜像 1、建立工作目录 2、生成镜像 3、启动容器并修改root密码 二、构建systemd镜像 1、建立工作目录 2、生成镜像 3、运行镜像容器 4、测试容器systemd 三、构建Nginx镜像 1、建立工作目录 2、编写Dockerfile脚本 3、编写run.sh启动脚本 4、生成镜像 5、运行镜像容器 6、验证 四、构建Tomcat镜像 1、建立工作目录 2、编辑Dockerfile脚本 3、生成镜像 4、运行镜像容器 5、验证 五、构建MySQL镜像 1、建立工作目录 2、编辑Dockerfile脚本 3、编辑my.cnf文件 4、编辑运行脚本并启动 5、生成镜像 6、启动镜像容器,并进行初始化 7、进入容器,授权远程连接 mysql 8、在客户端连接mysql容器
Dockerfile镜像实例
一、构建SSH镜像
1、建立工作目录
mkdir /opt/sshd
cd /opt/sshd
vim Dockerfile
FROM centos:7
MAINTAINER this is ssh image < test>
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 sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd
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" ]
2、生成镜像
docker build -t sshd:centos .
docker images
3、启动容器并修改root密码
docker run -itd --name sshd -P sshd:centos
docker ps -a
ssh localhost -p 32768
echo 'abc123' | passwd --stdin root
二、构建systemd镜像
1、建立工作目录
mkdir /opt/systemctl
cd /opt/systemctl/
vim Dockerfile
FROM sshd:centos
MAINTAINER this is systemctl image < hmj>
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" ]
2、生成镜像
docker build -t systemd:centos .
docker images
3、运行镜像容器
docker run --privileged -d -P --name systemd -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
docker ps -a
4、测试容器systemd
docker exec -it 50d0fe6fc8ba bash
systemctl status sshd
systemctl start sshd
systemctl status sshd
三、构建Nginx镜像
1、建立工作目录
mkdir /opt/nginx
cd /opt/nginx/
2、编写Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image < test>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.22.0.tar.gz /opt/
WORKDIR /opt/nginx-1.22.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
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD [ "/run.sh" ]
3、编写run.sh启动脚本
vim run.sh
/usr/local/nginx/sbin/nginx
4、生成镜像
docker build -t nginx:centos .
docker images
5、运行镜像容器
docker run -itd --name nginx -p nginx:centos
docker ps -a
6、验证
curl 192.168 .10.13:32771
四、构建Tomcat镜像
1、建立工作目录
mkdir /opt/tomcat
cd /opt/tomcat/
2、编辑Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is tomcat image < test>
ADD jdk-8u291-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_291 /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
ENTRYPOINT [ "/usr/local/tomcat/bin/catalina.sh" ,"run" ]
CMD [ "/usr/local/tomcat/bin/startup.sh" ,"start" ]
3、生成镜像
docker build -t tomcat:centos .
docker images
4、运行镜像容器
docker run -d --name tomcat -P tomcat:centos
docker ps -a
5、验证
五、构建MySQL镜像
1、建立工作目录
mkdir /opt/mysqld
cd /opt/mysqld
2、编辑Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image < test>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make ; useradd -M -s /sbin/nologin mysql
ADD mysql-boots-5.7.17.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.17/
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_EXTRA_CHARSETS = all \
-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 -j4; make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/; chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user = mysql \
--basedir = /usr/local/mysql \
--datadir = /usr/local/mysql/data; cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/; systemctl enable mysqld
ENV PATH = /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD [ "/usr/sbin/init" ]
3、编辑my.cnf文件
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
4、编辑运行脚本并启动
vim run.sh
/usr/local/mysql/bin/mysqld
5、生成镜像
docker build -t mysqld:centos .
docker images
6、启动镜像容器,并进行初始化
docker run --name = mysql_server -d -P --privileged mysqld:centos /usr/sbin/init
docker ps -a
7、进入容器,授权远程连接 mysql
docker exec -it mysql_server /bin/bash
mysql -u root -p
grant all privileges on *.* to 'root' @'%' identified by '123456' ;
grant all privileges on *.* to 'root' @'localhost' identified by '123456' ;
flush privileges;
8、在客户端连接mysql容器
mysql -h 192.168 .10.13 -u root -P 49153 -p123456