由于编写的jsp代码需要在各个环境中测试,同时又不想让网络成为一大障碍,起初一直在用虚拟机安装fedora,但近来越发觉得虚拟机启动操作系统和保存操作系统状态太慢了,
以前一直有用docker,像docker搭建的迅雷远程下载等等,docker优势很明显,秒启动是我最中意的,镜像体积小,可个性化的程度还是很高的,下面我们就来构建一个镜像。

首先创建一个文件夹,里面放上我们构建镜像的文件Dockerfile

1
2
3
mkdir fedora
touch fedora/Dockerfile
vim fedora/Dockerfile

文件内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM fedora:latest
MAINTAINER: ourfor.top
RUN dnf -y install systemd systemd-libs
#RUN dnf -y update
RUN dnf clean all; \
(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"]

使用下面的命令构建:

1
docker build --rm -t fedora:systemd fedora

使用exec链接shell,而不是attach

1
docker exec -it $container_id /bin/bash

  • 构建一个用于部署jsp页面的服务器:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    FROM fedora:systemd
    MAINTAINER ourfor.top
    RUN dnf install -y tomcat tomcat-webapps tomcat-admin-webapps;systemctl enable tomcat.service;
    RUN dnf -y install mariadb-server mariadb; systemctl enable mariadb.service
    RUN dnf install -y net-tools openssh-server openssh-clients passwd;
    RUN dnf clean all
    EXPOSE 80

    CMD ["/usr/sbin/init"]

启动:

1
2
3
4
docker run --privileged -d \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-h docker.server -p 4040:80 \
-t fedora:tomcat

  • 构建sshd服务镜像
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    FROM fedora:latest
    MAINTAINER ourfor@foxmail.com
    ENV UPDATE_DATE 2019-03-13
    RUN dnf -y install systemd systemd-libs
    #RUN dnf -y update
    RUN dnf clean all; \
    (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/*;
    RUN dnf install -y tomcat;systemctl enable tomcat.service;
    RUN dnf install -y tomcat tomcat-webapps tomcat-admin-webapps;systemctl enable tomcat.service;
    RUN dnf -y install mariadb-server mariadb; systemctl enable mariadb.service
    RUN dnf install -y net-tools openssh-server openssh-clients passwd;

    VOLUME [ "/sys/fs/cgroup" ]
    EXPOSE 8080
    CMD ["/usr/sbin/init"]
1
2
3
4
5
docker run --privileged -d \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-v $PWD/webapps:/var/lib/tomcat/webapps:rw \
-h docker.server -p 4040:8080 -p 2020:22 \
-t fedora:tomcat