目录

  • 创建SSHD自定义镜像
  • 创建systemctl镜像
  • 创建nginx自定义镜像
  • 创建tomcat自定义镜像
  • 创建mysql自定义镜像

创建SSHD自定义镜像

1、下载一个centos基础镜像

[root@docker ~]# docker pull centos:7

2、创建一目录用于存放Dockerfile

[root@docker ~]# mkdir ssh
[root@docker ~]# cd ssh/

3、创建Dockerfiel

[root@docker ssh]# vim Dockerfile
#基于创建的镜像
FROM centos:7
#说明信息
MAINTAINER This is ssh
#更新yum
RUN yum -y update
#安装ssh依赖包
RUN yum -y install openssh* net-tools lsof telnet passwd
#面交互给容器root账号设置密码
RUN echo '123456'| passwd --stdin root
#关闭sshPAM认证
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
#关闭PAM认证
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
#创建一个目录用于存放ssh公钥并设置宿主、宿组、设置执行权限
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
#开放端口号
EXPOSE 22
#设置启动容器运行的命令
CMD ["/usr/sbin/sshd", "-D"]

4、生成自定义镜像

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

5、查看自定义镜像是否生成

[root@docker ssh]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sshd                new                 6d472995d4a9        8 seconds ago       540MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

6、使用sshd镜像创建一个sshd的容器

[root@docker ssh]# docker run -d -P sshd:new 
26528889041edf9ad964c9dbf43d32dfb6bf1cde4010591bd4b7cc80b4b6de32
-d :放在后台持续运行
-P:生成一个随机端口

7、查看容器是否生成

[root@docker ssh]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
26528889041e        sshd:new            "/usr/sbin/sshd -D"   5 seconds ago       Up 4 seconds        0.0.0.0:32768->22/tcp   angry_booth

8、测试ssh登录

方法一:
[root@docker ssh]# ssh localhost -p 32768
-p:指定端口
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
RSA key fingerprint is SHA256:YGQGTHCef7l81kcfdnZF0AAsP4hbYWqlFOwTwJ2nnMw.
RSA key fingerprint is MD5:2b:ad:35:0b:7a:30:aa:13:7c:fb:5b:7f:0d:a5:c6:6b.
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@docker ssh]# ssh 127.0.0.1 -p 32768
-p:指定端口
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
RSA key fingerprint is SHA256:YGQGTHCef7l81kcfdnZF0AAsP4hbYWqlFOwTwJ2nnMw.
RSA key fingerprint is MD5:2b:ad:35:0b:7a:30:aa:13:7c:fb:5b:7f:0d:a5:c6:6b.
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:

创建systemctl镜像

1、在上面的sshd镜像继续操作

[root@docker ~]# docker pull centos:7

2、创建一目录用于存放Dockerfile

[root@docker ~]# mkdir systemctl
[root@docker ~]# cd systemctl/

3、创建Dockerfiel

[root@docker systemctl]# vim Dockerfile
#基于创建的镜像(镜像名称选的是上面的sshd镜像可以自定义选择其他镜像)
FROM sshd:new
#定义一个环境变量值为docker
ENV container docker
#清除容器内部的systemd文件
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"]

4、生成自定义镜像

[root@docker systemctl]# docker build -t system:new .
[root@docker systemctl]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
system              new                 a80f38986947        3 minutes ago       540MB
sshd                new                 6d472995d4a9        About an hour ago   540MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

5、创建容器

[root@docker systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro system:new  /sbin/init &
--privateged:container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限

6、查看容器是否生成

[root@docker systemctl]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
b192093d52aa        system:new          "/sbin/init"          17 seconds ago      Up 16 seconds       22/tcp                  eager_chandrasekhar
26528889041e        sshd:new            "/usr/sbin/sshd -D"   About an hour ago   Up About an hour    0.0.0.0:32768->22/tcp   angry_booth

7、进入容器测试systemctl工具

[root@docker systemctl]# docker exec -it b192093d52aa bash
[root@b192093d52aa /]# systemctl       
UNIT                           LOAD   ACTIVE     SUB       DESCRIPTION
dev-sda3.device                loaded activating tentative /dev/sda3
-.mount                        loaded active     mounted   /
dev-mqueue.mount               loaded active     mounted   POSIX Message Queue File System
etc-hostname.mount             loaded active     mounted   /etc/hostname
etc-hosts.mount                loaded active     mounted   /etc/hosts
etc-resolv.conf.mount          loaded active     mounted   /etc/resolv.conf
systemd-journald.service       loaded active     running   Journal Service
systemd-tmpfiles-setup.service loaded active     exited    Create Volatile Files and Directories

创建nginx自定义镜像

1、下载一个centos基础镜像

[root@docker ~]# docker pull centos:7

2、创建一目录用于存放Dockerfile

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

3、创建Dockerfiel

[root@docker nginx]# vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <wu>
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#创建一下程序用户
RUN useradd -M -s /sbin/nologin nginx
#下载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
#指定http和https端口
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]

4、创建run.sh执行脚本

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

5、在nginx目录下面放入nginx数据包

[root@docker nginx]# ll
总用量 968
-rw-r--r-- 1 root root    729 11月 27 15:42 Dockerfile
-rw-r--r-- 1 root root 981687 10月 16 08:14 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root     40 11月 27 15:35 run.sh
[root@docker nginx]#

6、生成自定义镜像

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

7、查看自定义镜像是否生成

[root@docker nginx]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               new                 fa60deff3221        5 minutes ago       537MB
system              new                 a80f38986947        About an hour ago   540MB
sshd                new                 6d472995d4a9        2 hours ago         540MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

8、创建容器

[root@docker nginx]# docker run -d -P nginx:new 
-P:随机生成一个端口(端口号32768开始)

9、查看容器是否生成

[root@docker nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                                           NAMES
9fc000e93664        nginx:new           "/run.sh"             22 seconds ago      Up 21 seconds       0.0.0.0:32770->80/tcp, 0.0.0.0:32769->443/tcp   busy_galois
b192093d52aa        system:new          "/sbin/init"          56 minutes ago      Up 56 minutes       22/tcp                                          eager_chandrasekhar
26528889041e        sshd:new            "/usr/sbin/sshd -D"   2 hours ago         Up 2 hours          0.0.0.0:32768->22/tcp                           angry_booth
[root@docker nginx]#

10、测试nginx主页

docker根据image_id创建镜像 docker 创建镜像_docker

创建tomcat自定义镜像

1、下载一个centos基础镜像

[root@docker ~]# docker pull centos:7

2、创建一目录用于存放Dockerfile

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

3、创建Dockerfiel

[root@docker tomcat]# vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is tomcat image <wu>
#下载jdk软件包
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/fjava/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /usr/local
WORKDIR /usr/local/
#下载tomcat软件包
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat8
EXPOSE 8080
#CMD ["/usr/local/tomcat8/bin/catalina.sh","run"]
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"]

4、下载tomcat、jdk包

[root@docker tomcat]# ll
总用量 186324
-rw-r--r-- 1 root root   9417469 11月 16 10:27 apache-tomcat-8.5.16.tar.gz
-rw-r--r-- 1 root root       625 11月 27 16:16 Dockerfile
-rw-r--r-- 1 root root 181367942 10月 19 15:35 jdk-8u91-linux-x64.tar.gz

5、创建自定义镜像

[root@docker tomcat]# docker build -t tomcat:centos .

6、查看自定义镜像是否生成

[root@docker tomcat]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              centos              b2d0960f8b4e        52 seconds ago      960MB
nginx               new                 fa60deff3221        27 minutes ago      537MB
system              new                 a80f38986947        About an hour ago   540MB
sshd                new                 6d472995d4a9        2 hours ago         540MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

7、创建容器

[root@docker tomcat]# docker run -d --name tomcat01 -p 1999:8080 tomcat:centos

8、查看容器是否创建

[root@docker tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
92aaa18626ad        tomcat:centos       "/usr/local/tomcat8/…"   48 seconds ago      Up 48 seconds       0.0.0.0:1999->8080/tcp                          tomcat01
9fc000e93664        nginx:new           "/run.sh"                22 minutes ago      Up 22 minutes       0.0.0.0:32770->80/tcp, 0.0.0.0:32769->443/tcp   busy_galois
b192093d52aa        system:new          "/sbin/init"             About an hour ago   Up About an hour    22/tcp                                          eager_chandrasekhar
26528889041e        sshd:new            "/usr/sbin/sshd -D"      2 hours ago         Up 2 hours          0.0.0.0:32768->22/tcp                           angry_booth
[root@docker tomcat]#

9、测试访问tomcat

docker根据image_id创建镜像 docker 创建镜像_nginx_02

创建mysql自定义镜像

1、下载一个centos基础镜像

[root@docker ~]# docker pull centos:7

2、创建一目录用于存放Dockerfile

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

3、创建Dockerfiel

[root@docker mysql]# vim Dockerfile
FROM centos:7
MAINTAINER build image mysql
EXPOSE 3306
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20
RUN useradd mysql -M -s /sbin/nologin
RUN yum -y install gcc \
gcc-c++ \
make \
ncurses \
ncurses-devel \
bison \
cmake
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
RUN 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
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /lib/systemd/system/
RUN echo -e "#!/bin/bash \nsystemctl enable mysqld" > /run.sh
RUN chmod 755 /run.sh
RUN sh /run.sh
CMD ["init"]

4、创建mysql.cnf配置文件

[root@docker 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

5、上次mysql数据包

[root@docker mysql]# ll
-rw-r--r-- 1 root root 48833145 10月 23 2017 mysql-boost-5.7.20.tar.gz

6、创建mysql自定义镜像

[root@docker mysql]# docker build -t centos:mysql .

7、创建mysql自定义镜像

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

8、查看容器

[root@docker mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
djre52jga2e4        mysql:new           "init"              3 seconds ago       Up 2 seconds        0.0.0.0:32768->3306/tcp   mysql

9、进入mysql容器

[root@docker mysql]# docker exec -it csnvnsnn /bin/bash

10、登录数据库

[root@djre52jga2e4 mysql-5.7.20]# mysql -uroot -p