Dockerfile
运行一个ubuntu容器
docker run -i -t ubuntu /bin/bash
备份apt源
cp /etc/apt/sources.list /etc/apt/sources.list.bak
修改apt源
vi /etc/apt/sources.list
替换所有源为sohu源
deb http://mirrors.sohu.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.sohu.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.sohu.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.sohu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.sohu.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.sohu.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.sohu.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.sohu.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.sohu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.sohu.com/ubuntu/ trusty-backports main restricted universe multiverse
或网易源
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
修改保存后退出运行的容器
exit
查看最后一个运行的容器
docker ps -l
提交本次修改的镜像为 ubuntu/sohu
docker commit 698 ubuntu/sohu
使用修改后源的镜像做Dockerfile
# VERSION 0.0.1 FROM ubuntu/sohu MAINTAINER myemail "myemail@gmail.com" RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir -p /var/run/sshd
RUN echo "root:123456" | chpasswd#RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i "s/UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
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
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
EXPOSE 22
EXPOSE 8080
ENTRYPOINT service tomcat7 start && /usr/sbin/sshd -D
需要注意:
- ENTRYPOINT,表示镜像在初始化时需要执行的命令,不可被重写覆盖,需谨记
- CMD,表示镜像运行默认参数,可被重写覆盖
- ENTRYPOINT/CMD都只能在文件中存在一次,并且最后一个生效 多个存在,只有最后一个生效,其它无效!
- 需要初始化运行多个命令,彼此之间可以使用 && 隔开,但最后一个须要为无限运行的命令,需切记!
构建镜像
#. 代表Dockerfile在当前路径下
docker build -t shawn/ssh .
启动新建的容器
docker run -d -p 22 -p 8080:8080 saint/ssh
登录ssh镜像
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 789c45a53c6f shawn/ssh:latest "/bin/sh -c '/usr/sb 8 seconds ago Up 7 seconds 0.0.0.0:49153->22/tcp furious_pare eb738292efd9 ubuntu/ssh:latest "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago focused_yonath 4a714065db9a ubuntu/proxy:latest "/bin/bash" 15 minutes ago Exited (0) 7 minutes ago boring_goldstine 28491e2dec96 ubuntu:latest "/bin/bash" 2 hours ago Exited (2) 17 minutes ago berserk_fermat 4f23b24e247a 6311975ede96 "/bin/sh -c 'apt-get 2 hours ago Exited (100) 2 hours ago sleepy_lalande
登录容器
ssh root@127.0.0.1 -p 49153
停止所有容器
docker stop $(docker ps -a -q)
Dockerflie run tomcat
#tomcat java7 installed FROM saint/java MAINTAINER saintaxl "saintaxl@gmail.com" VOLUME ["/opt"] EXPOSE 8080 ENV JAVA_HOME /usr/local/jdk1.7.0_67 ENTRYPOINT /usr/local/tomcat7/bin/startup.sh && tail -f /usr/local/tomcat7/logs/catalina.out
构建镜像
docker build -t saint/runtomcat .
运行容器
#前台运行 docker run --rm -p 8080 saint/runtomcat #后台运行 docker run -d -p 8080:8080 saint/runtomcat
浏览器访问
#宿主机IP:端口 http://192.168.124.129:8080/
http://www.blogjava.net/yongboy/archive/2013/12/16/407643.html
Dockerfile 命令解释
http://blog.tankywoo.com/docker/2014/05/08/docker-2-dockerfile.html
Docker 笔记
http://fann.im/blog/2014/02/11/docker-notes/
查找apt软件
apt-cache search tomcat