Dockerfile制作各种主流服务镜像
Dockerfile创建ssh服务并运行
[root@server1 ~]# mkdir sshd
[root@server1 ~]# cd sshd
[root@server1 ~]# vim Dockerfile
FROM centos:7
MAINTAINER the centos project
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123123' | 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@server1 ssh]#docker build -t systemd:new
Successfully built b44f1ce4c742
Successfully tagged sshd:new
[root@server1 ssh]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd new b44f1ce4c742 18 seconds ago
[root@server1 ssh]# docker run -d -P sshd:new
97854c592cadae4257549b2caab439222a7fd7d95ab07aebdc7ba721d0f9f4ee
[root@server1 ssh]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97854c592cad sshd:new "/usr/sbin/sshd -D" 3 seconds ago Up 2 seconds 0.0.0.0:32768->22/tcp tender_lichterman
[root@server1 ssh]# ssh localhost -p 32768
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
RSA key fingerprint is SHA256:XBmDJe+Se5HOwTQBRv0uavzTuecBWC1KQNFT/XNO4fo.
RSA key fingerprint is MD5:00:6a:d0:bd:ec:9b:88:e2:5a:ef:ca:16:e0:84:fa:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32768' (RSA) to the list of known hosts.
root@localhost's password:
[root@97854c592cad ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 54 bytes 6373 (6.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 31 bytes 5171 (5.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions
Dockerfile构建systemctl服务并运行
[root@server1 ~]# mkdir systemctl
[root@server1 ~]# cd systemctl/
[root@server1 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@server1 systemctl]# docker build -t systemd:new
Successfully built 1056b9c36b28
Successfully tagged systemd:new
[root@server1 systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
[1] 8815
[root@server1 systemctl]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
systemd new 1056b9c36b28 2 minutes ago 540MB
[root@server1 systemctl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eecf67294783 systemd:new "/sbin/init" 46 seconds ago Up 45 seconds 22/tcp wizardly_archimedes
[root@server1 systemctl]# docker exec -it eecf67294783 bash
[root@eecf67294783 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:sshd(8)
man:sshd_config(5)
[root@eecf67294783 /]# systemctl start sshd
[root@eecf67294783 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2020-11-27 02:37:33 UTC; 2s ago
Docs: man:sshd(8)
man:sshd_config(5)
Dockerfile构建Nginx服务并运行
[root@server1 ~]#mkdir nginx
[root@server1 ~]#cd nginx
[root@server1 ~]#vim Dockerfile
FROM centos:7
MAINTAINER this is nginx project
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.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
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
拖入nginx-1.12.2包
[root@server1 nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@server1 nginx]# docker build -t nginx:new .
Successfully built d87d60676fb2
Successfully tagged nginx:new
[root@server1 nginx]# docker run -d -P nginx:new
32f8f571dc1ab31b154d167070c77da66b2946b6c633a5556831b2d11013b114
[root@server1 nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32f8f571dc1a nginx:new "/run.sh" 6 seconds ago Up 5 seconds 0.0.0.0:32788->80/tcp, 0.0.0.0:32787->443/tcp optimistic_jennings
本机浏览器输入ip对应端口
Dockerfile构建Tomcat服务并运行
[root@server1 ~]# mkdir tomcat
[root@server1 ~]# cd tomcat/
[root@server1 tomcat]# vim Dockerfile
From centos:7
MAINTAINER this is Tomcat image
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/lib
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命令可以覆盖dockerfile中的CMD,而外部的CMD命令无法覆盖ENTRYPOINT
[root@server1 Tomcat]# docker build -t tomcat:new .
Successfully built eee009aa5b10
Successfully tagged tomcat:new
[root@server1 Tomcat]# docker run -d --name tomcat -p 1216:8080 tomcat:new
f934670ee291906cc5c94dff853f4b46e3c1b262cb35091f69b698fc3d54f544
[root@server1 Tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f934670ee291 tomcat:new "/usr/local/tomcat8/…" 4 seconds ago Up 2 seconds 0.0.0.0:1216->8080/tcp tomcat
Dockerfile构建mysql服务
[root@server1 ~]# mkdir mysqld
[root@server1 ~]# cd mysqld
[root@server1 ~]# vim Dockerfile
FROM centos:7
MAINTAINER zy
RUN yum -y update
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/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
EXPOSE 3306
[root@server1 mysqld]# vi 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@server1 mysqld]# vim run.sh
#!/bin/bash
systemctl enable mysqld
将mysql-boost-5.7.20.tar.gz包放至与Dockerfile文件相同路径下
[root@server1 mysqld]# docker build -t mysql:new .
Successfully built e3021dcac829
Successfully tagged mysql:new
[root@server1 mysqld]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server1 mysqld]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql new e3021dcac829 3 minutes ago 10GB
centos 7 8652b9f0cb4c 2 weeks ago 204M
[root@server1 mysqld]# docker run -d --privileged -P mysql:new
6607b4ac34122fcd989dac035dd95508ec1b99ee2dff5f44deb7bfafb5eb3818
[root@server1 mysqld]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6607b4ac3412 mysql:new "/usr/sbin/init" 2 minutes ago Up 2 minutes 0.0.0.0:32768->3306/tcp unruffled_chebyshev
[root@server1 mysqld]# docker exec -it 6607b4ac3412 /bin/bash
[root@6607b4ac3412 mysql-5.7.20]# mysql
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> create database zy;
Query OK, 1 row affected (0.00 sec)
数据库正常使用