Docker创建镜像实例(ssh、systemctl、nginx、tomcat、mysql)

一、构建ssh服务镜像

1、创建目录

[root@server1 ~]# mkdir sshd
[root@server1 ~]# cd sshd

2、创建Dockerfile文件

[root@server1 sshd]# vim Dockerfile
FROM centos:7
MAINTAINER project
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"]

3、生成镜像

[root@server1 sshd]# docker build -t sshd:new .

4、启动容器并登录

[root@server1 sshd]# docker run -d -P sshd:new

[root@server1 sshd]# docker ps -a
33fe023e9e3d        sshd:new            "/usr/sbin/sshd -D"      1 hours ago         Up 1 hours          0.0.0.0:32769->22/tcp

[root@server1 sshd]# ssh localhost -p 32769   #容器的映射端口
root@localhost's password:
Last login: Fri Nov 27 14:46:57 2020 from gateway

二、构建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 .
[root@server1 systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init & 
##privileged container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。

[root@server1 systemctl]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
e6fac7b2e795        systemd:new         "/sbin/init"          25 seconds ago       Up 25 seconds        22/tcp                  angry_visvesvaraya
38a895b7ad73        sshd:new            "/usr/sbin/sshd -D"   43 minutes ago      Up 43 minutes       0.0.0.0:32768->22/tcp   zealous_spence

[root@server1 systemctl]# docker exec -it e6fac7b2e795 bash
[root@e6fac7b2e795 /]# 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@e6fac7b2e795 /]# systemctl start sshd
[root@e6fac7b2e795 /]# 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 14:56:03 UTC; 1s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 51 (sshd)
   CGroup: /docker/e6fac7b2e795d442d25aea2631fd246c57888282be85a6981ea1ac79597c7c09/system.slice/sshd.service
           └─51 /usr/sbin/sshd -D

三、构建nginx镜像

创建目录

[root@server1 ~]# mkdir nginx
[root@server1 ~]# cd nginx/

生成Dockerfile文件

[root@server1 nginx]# vim Dockerfile
FROM centos:7
MAINTAINER nginx
RUN yum -y update
RUN yum -y install gcc gcc-c++ make pcre-devel zlib-devel
ADD nginx-1.12.2.tar.gz /opt
WORKDIR /opt/nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
RUN useradd -M -s /sbin/nologin nginx
ENV PATN /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"]

编写run.sh脚本

[root@server1 nginx]# vi run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

构建镜像

[root@server1 nginx]# docker build -t nginx:new .

运行并查看

[root@server1 nginx]# docker run -d -P nginx:new
5483c1716b91        nginx:new           "/run.sh"                2 hours ago         Up 2 hours          0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   funny_easley

验证效果
在这里插入图片描述

四、构建tomcat镜像

创建目录

[root@server1 ~]# mkdir tomcat
[root@server1 ~]# cd tomcat/

生成Dockerfile文件

[root@server1 tomcat]# vi Dockerfile   
FROM centos:7
MAINTAINER 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@server1 tomcat]# docker build -t tomcat:new .

运行容器并查看

[root@server1 tomcat]# docker build -t tomcat:new .
[root@server1 tomcat]# docker run -dit --name tomcat -p 1218:8080 tomcat:new
cf957aa211f9cdc9e57e901edfdac4a644f7f5ca7d81baa8a972d1cd9cef8f4f
[root@server1 tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
cf957aa211f9        tomcat:new          "/usr/local/tomcat8/…"   7 seconds ago       Up 6 seconds        0.0.0.0:1218->8080/tcp                          tomcat

输入宿主机IP,验证查看
在这里插入图片描述

五、构建mysql镜像

创建目录

[root@server1 ~]# mkdir mysql
[root@server1 ~]# cd mysql/

生成Dockerfile文件

[root@server1 mysql]# vim Dockerfile
FROM centos:7
MAINTAINER mysql
RUN yum -y update
RUN yum -y install ncurses ncurses-devel bison cmake make gcc gcc-c++
RUN groupadd mysql
RUN useradd -M -s /sbin/nologin mysql -g 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
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
ADD run.sh /run.sh
RUN sh /run.sh
CMD ["/usr/sbin/init"]

创建run.sh

[root@localhost mysql]# vim run.sh
#!/bin/bash
systemctl enable mysqld

新建my.cnf文件

[root@server1 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/mysql.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 mysql]# docker build -t mysql:zjy .

运行镜像

[root@server1 mysql]# docker run -dit --name=mysql -P --privileged mysql:zjy

查看容器状态
在这里插入图片描述

进入容器

在这里插入图片描述

登录数据库

在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页