<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">关于Docker的下载和安装网上很多教程,大家可以自行百度..</span>

这里记载一下我在使用docker中遇到的问题


============================================学习的分割线========================================

OS:osx

第一步:因为是osx的系统,所以先下载安装了boot2docker。

zengzhuomin:~$ boot2docker init
Virtual machine boot2docker-vm already exists
zengzhuomin:~$ boot2docker up
Waiting for VM and Docker daemon to start...
..........................................................................o
Started.
Writing /Users/zengzhuomin/.boot2docker/certs/boot2docker-vm/ca.pem
Writing /Users/zengzhuomin/.boot2docker/certs/boot2docker-vm/cert.pem
Writing /Users/zengzhuomin/.boot2docker/certs/boot2docker-vm/key.pem
Your environment variables are already set correctly.

启动boot2docker后,就可以正常使用docker命令了。


第二步:搭建一个有java环境的镜像。其中选用jdk7

1. 创建ubuntu-java7文件夹,并在下面创建Dockerfile

cd ~/workspace/docker/ubuntu-java7
vim Dockerfile

Dockerfile内容如下:

# Version: 0.0.1
FROM ubuntu:14.04
MAINTAINER Griselda "zhuomin_girl@126.com"
ENV REFRESHED_AT 2015-08-18
RUN (echo "deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse" > /etc/apt/sources.list && echo "deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list)
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y openjdk-7-jre-headless
RUN apt-get clean
RUN update-alternatives --display java
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64" >> /etc/environment

2. 执行Dockerfile,创建镜像

docker build -t ubuntu-java7 .

3. 测试镜像

zengzhuomin:~/workspace/docker/ubuntu-java7$ docker run -ti ubuntu-java7 java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

第三步:搭建一个有tomcat服务器的环境,要在ubuntu-java7的镜像上建立

1. 创建ubuntu-tomcat7文件夹,并在下面创建Dockerfile

cd ~/workspace/docker/ubuntu-tomcat7
vim Dockerfile

Dockerfile内容如下:

# Version: 0.0.1
FROM javaweb/dockerfile
MAINTAINER Griselda "zhuoming_girl@126.com"
ENV REFRESHED_AT 2014-08-18
ENV TOMCATVER 7.0.63
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y wget
RUN (wget -O /tmp/tomcat7.tar.gz http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v${TOMCATVER}/bin/apache-tomcat-${TOMCATVER}.tar.gz && \
    cd /opt && \
    tar zxf /tmp/tomcat7.tar.gz && \
    mv /opt/apache-tomcat* /opt/tomcat && \
    rm /tmp/tomcat7.tar.gz)
ADD ./settom.sh /usr/local/bin/run
EXPOSE 8080
CMD ["/bin/sh", "-e", "/usr/local/bin/run"]

这个Dockerfile中提到:要把settom.sh文件加载到容器里,因此我们需要编写一个settom.sh文件。这个文件的作用是tomcat的部署脚本

#!/bin/bash
ADMIN_USER=${ADMIN_USER:-admin}
ADMIN_PASS=${ADMIN_PASS:-tomcat}
MAX_UPLOAD_SIZE=${MAX_UPLOAD_SIZE:-52428800}
CATALINA_OPTS=${CATALINA_OPTS:-"-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m"}
export CATALINA_OPTS="${CATALINA_OPTS}"

cat << EOF > /opt/tomcat/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<user username="${ADMIN_USER}" password="${ADMIN_PASS}" roles="admin-gui,manager-gui"/>
</tomcat-users>
EOF

if [ -f "/opt/tomcat/webapps/manager/WEB-INF/web.xml" ]
then
  sed -i "s#.*max-file-size.*#\t<max-file-size>${MAX_UPLOAD_SIZE}</max-file-size>#g" /opt/tomcat/webapps/manager/WEB-INF/web.xml
  sed -i "s#.*max-request-size.*#\t<max-request-size>${MAX_UPLOAD_SIZE}</max-request-size>#g" /opt/tomcat/webapps/manager/WEB-INF/web.xml
fi
/bin/sh -e /opt/tomcat/bin/catalina.sh run
~

2. 构建Dockerfile镜像

zengzhuomin:~/workspace/docker/ubuntu-tomcat$ docker build -t ubuntu-tomcat .

3. 测试镜像

zengzhuomin:~/workspace/docker/ubuntu-tomcat$ docker run -ti ubuntu-tomcat java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

第三步:搭建fetch环境

1. 创建ubuntu-fetch文件夹,并在下面创建Dockerfile

cd ~/workspace/docker/ubuntu-fetch
vim Dockerfile
Dockerfile文件内容如下:

#Version: 0.0.1
FROM ubuntu:14.04
MAINTAINER Griselda "zhuomin_girl@126.com"
ENV REFRESHED_AT 2014-08-18
RUN apt-get -yqq update
RUN apt-get -yqq install wget
VOLUME [ "/opt/tomcat/webapps/" ]
WORKDIR /opt/tomcat/webapps/
ENTRYPOINT [ "wget" ]
CMD [ "-?" ]

2. 构建Dockerfile镜像

zengzhuomin:~/workspace/docker/ubuntu-fetch$ docker build -t tomcat-webapp .

3. 加载war文件

zengzhuomin:~/workspace/docker/ubuntu-fetch$ docker run -ti --name fetch-app feuyeux/tomcat-webapp http://tomcat.apache.org/tomcat-8.0-doc/appdev/sample/sample.war

4. 加载数据卷

zengzhuomin:~/workspace/docker/ubuntu-fetch$ docker run --name tom-app --volumes-from fetch-app -d -P ubuntu-tomcat

第四步:查看效果

1. 查看映射的端口号

zengzhuomin:~/workspace/docker/ubuntu-fetch$ docker port tom-app 8080
0.0.0.0:32768

2. 因为我是使用虚拟机运行docker的,所以登录的时候应该使用boot2docker的ip地址,(ubuntu直接使用localhost:32768即可登录)否则会出现connection refused

zengzhuomin:~/workspace/docker/ubuntu-fetch$ boot2docker ip
192.168.59.103



======================================================总结的分界线=================================================

在学习docker过程中,搭建过ssh的环境,但是使用ssh连接的时候也是被connection refused了。后来发现果然ip又错了~改正后登录时需要填写密码,密码不正确三次后又被refused了..

后来使用docker exec -ti 登录容器后,用

useradd zengzhuomin

passwd ...

添加了账号,最后终于成功搭建了ssh服务器

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐