(三)將容器變成鏡像

提交保存鏡像命令java

docker commit <container> [repo:tag]

當咱們在製做本身的鏡像的時候,會在container中安裝一些工具、修改配置,若是不作commit保存 起來,那麼container中止之後再啓動,這些更改就消失了python

一.經過命令來建立鏡像

這種作法的優勢: 最方便 最快速web

                  缺點: 不規範 沒法自動化docker

手動構建一個 myjava3的鏡像 過程以下:ubuntu

[root@docker ~]# docker create --name myjava3 -it java /bin/bash 0814ffc5a92b66d017a620648205b1031b810ec1cb9da2ca411571dd75f4daac [root@docker ~]# docker start myjava3 myjava3 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0814ffc5a92b java "/bin/bash"         20 seconds ago      Up 6 seconds                            myjava3

[root@docker ~]# docker exec -it 0814ffc5a92b /bin/bash
root@0814ffc5a92b:/# vim

root@0814ffc5a92b:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usrtomcat


root@0814ffc5a92b:/# mkdir createimagebash

#退出容器 提交保存鏡像服務器

root@0814ffc5a92b:/# exit
exitoracle

[root@docker ~]# docker commit 0814ffc5a92b myjava3:v1.0
sha256:c7644e42d29b5938518e624ce8c1033ff8973d1b73501de7cc288c0f4dabbb76
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myjava3 v1.0 c7644e42d29b 16 seconds ago 643MB

[root@docker ~]# docker run -it myjava3:v1.0 ls
bin createimage etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var

二.Buildfile自動化建立鏡像

1.一個ubuntu下的Java鏡像的buildfile

FROM nimmis/ubuntu:14.04 MAINTAINER nimmis <kjell.havneskold@gmail.com> # disable interactive functions ENV DEBIAN_FRONTEND noninteractive # set default java environment variable ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64 RUN apt-get install -y software-properties-common && \ add-apt-repository ppa:openjdk-r/ppa -y && \ apt-get update && \ apt-get install -y --no-install-recommends openjdk-8-jre && \ rm -rf /var/lib/apt/lists/*

 執行Dockerfile

[root@docker ~]# docker build -t benjamin/java .

 

2.Dockfile  複雜案例實戰:製做ubuntu+java+tomcat+ssh server鏡像

mkdir test cd test vi Dockfile FROM ubuntu MAINTAINER yongboy "yongboy@gmail.com" # 更新源,安裝ssh server RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir -p /var/run/sshd # 設置root ssh遠程登陸密碼爲123456 RUN echo "root:123456" | chpasswd # 添加orache java7源,一次性安裝vim,wget,curl,java7,tomcat7等必備軟件 RUN apt-get install python-software-properties RUN add-apt-repository ppa:webupd8team/java RUN apt-get update RUN apt-get install -y vim wget curl oracle-java7-installer tomcat7 # 設置JAVA_HOME環境變量 RUN update-alternatives --display java RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/environment RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle">> /etc/default/tomcat7 # 容器須要開放SSH 22端口 EXPOSE 22 # 容器須要開放Tomcat 8080端口 EXPOSE 8080 # 設置Tomcat7初始化運行,SSH終端服務器做爲後臺運行 ENTRYPOINT service tomcat7 start && /usr/sbin/sshd -D
相關文章
相關標籤/搜索