安装tomcat
从docker hub 上pull tomcat
root@mysheep:/home/mysheep# docker pull tomcat
查看镜像
root@mysheep:/home/mysheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 6408fdc94212 4 days ago 507MB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
运行一个容器
root@mysheep:/home/mysheep# docker run -it ubuntu:15.10 /bin/bash
由于数据源没有更改,所以不能执行apt-get update,需要修改/etc/apt/sources.list,vi指令也是不能执行的,所以可以用echo来修改
root@79c763dd1a06:/etc/apt# echo deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse > sources.list
root@79c763dd1a06:/etc/apt# echo deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse >> sources.list
root@79c763dd1a06:/etc/apt# echo deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse >> sources.list
root@79c763dd1a06:/etc/apt# echo deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse >> sources.list
在后执行数据源更新
root@79c763dd1a06:/etc/apt# apt-get update
然后安装vim
root@79c763dd1a06:/etc/apt# apt-get install vim
这样就能打开文件了
更新镜像
将刚才的镜像根据容器ID安装了vim进行提交
root@mysheep:/home/mysheep# docker commit -m="install vim" -a="mysheep" 79c763dd1a06 mysheep/ubuntu:v2
sha256:bee24519dc6334dd1534de52776a6f4ec972cf9f1c95b5194f6d911171900810
查看镜像
root@mysheep:/home/mysheep# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysheep/ubuntu v2 bee24519dc63 12 seconds ago 201MB
tomcat latest 6408fdc94212 4 days ago 507MB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
查看运行情况,然后移除容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
79c763dd1a06 ubuntu:15.10 "/bin/bash" 26 minutes ago Exited (130) About a minute ago gracious_sanderson
root@mysheep:/home/mysheep# docker rm 79c763dd1a06
79c763dd1a06
root@mysheep:/home/mysheep# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
重新运行一个容器,使vim,会报错
root@mysheep:/home/mysheep# docker run -it ubuntu:15.10 /bin/bash
root@7011d21821df:/# vim /etc/apt/sources.list
bash: vim: command not found
运行刚才提交更新后的镜像,就可以使用
root@mysheep:/home/mysheep# docker run -it mysheep/ubuntu:v2
root@dba6b816bec0:/# vim /etc/apt/sources.list
创建镜像
在/usr/local/下创建一个docker文件夹,再在该文件夹里创建一个myubuntu的文件夹,新疆一个Dockerfile文件
root@mysheep:/usr/local/docker/myubuntu# vi Dockerfile
必须以FROM开头,然后设置作者,运行一个Hello World,再在当前目录新建一个test.txt的文件,将其复制到/home里面,CMD只能执行一次,有多个时只执行最后一个,EXPOSE暴露端口
FROM ubuntu:15.10
MAINTAINER 825625183@qq.com
RUN /bin/bash -c 'echo "Hello World"'
COPY test.txt /home
CMD ["/bin/echo","this is a bad girl"]
EXPOSE 80
EXPOSE 8080
然后运行
root@mysheep:/usr/local/docker/myubuntu# docker build -t mysheep/myubuntu .
Sending build context to Docker daemon 3.072kB
Step 1/7 : FROM ubuntu:15.10
---> 9b9cb95443b5
Step 2/7 : MAINTAINER 825625183@qq.com
---> Running in 904667088e92
Removing intermediate container 904667088e92
---> ed6367eec3c7
Step 3/7 : RUN /bin/bash -c 'echo "Hello World"'
---> Running in bb4f41018b4a
Hello World
Removing intermediate container bb4f41018b4a
---> 7715c836c41a
Step 4/7 : COPY test.txt /home
---> ef511f08b122
Step 5/7 : CMD ["/bin/echo","this is a bad girl"]
---> Running in 490f00dc9a1f
Removing intermediate container 490f00dc9a1f
---> ea2d99d8e3d9
Step 6/7 : EXPOSE 80
---> Running in dbecec5991da
Removing intermediate container dbecec5991da
---> 4026c07d57a0
Step 7/7 : EXPOSE 8080
---> Running in 19ead01f2987
Removing intermediate container 19ead01f2987
---> 19d2eb743e99
Successfully built 19d2eb743e99
Successfully tagged mysheep/myubuntu:latest
查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysheep/myubuntu latest 19d2eb743e99 8 minutes ago 137MB
mysheep/ubuntu v2 bee24519dc63 49 minutes ago 201MB
tomcat latest 6408fdc94212 4 days ago 507MB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
重新在该镜像上运行一个容器,进入/home,查看test.txt
root@mysheep:/usr/local/docker/myubuntu# docker run -it mysheep/myubuntu /bin/bash
root@178b38ab8f14:/# cd /home
root@178b38ab8f14:/home# cat test.txt
hello docker
更改镜像标签
root@mysheep:/usr/local/docker/myubuntu# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysheep/myubuntu latest 19d2eb743e99 10 minutes ago 137MB
mysheep/ubuntu v2 bee24519dc63 51 minutes ago 201MB
tomcat latest 6408fdc94212 4 days ago 507MB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
根据ID更改
root@mysheep:/usr/local/docker/myubuntu# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysheep/myubuntu latest 19d2eb743e99 11 minutes ago 137MB
mysheep/myubuntu v2 19d2eb743e99 11 minutes ago 137MB
mysheep/ubuntu v2 bee24519dc63 52 minutes ago 201MB
tomcat latest 6408fdc94212 4 days ago 507MB
ubuntu 15.10 9b9cb95443b5 3 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
运行tomcat,映射8080端口,进行访问
root@mysheep:/usr/local/docker/myubuntu# docker run --name tomcat -p 8080:8080 -d tomcat
b2217a15d1621860cbff9106b9064f6346074eb9d09146672c652e8267821c48
root@mysheep:/usr/local/docker/myubuntu# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2217a15d162 tomcat "catalina.sh run" 7 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp tomcat
访问成功
然后交互一个容器,查看目录
root@mysheep:/usr/local/docker/myubuntu# docker exec -it tomcat /bin/bash
root@b2217a15d162:/usr/local/tomcat# ls -l
total 152
-rw-r--r-- 1 root root 19318 Nov 17 18:48 BUILDING.txt
-rw-r--r-- 1 root root 5407 Nov 17 18:48 CONTRIBUTING.md
-rw-r--r-- 1 root root 57011 Nov 17 18:48 LICENSE
-rw-r--r-- 1 root root 1726 Nov 17 18:48 NOTICE
-rw-r--r-- 1 root root 3255 Nov 17 18:48 README.md
-rw-r--r-- 1 root root 7136 Nov 17 18:48 RELEASE-NOTES
-rw-r--r-- 1 root root 16262 Nov 17 18:48 RUNNING.txt
drwxr-xr-x 2 root root 4096 Nov 23 16:37 bin
drwxr-sr-x 1 root root 4096 Nov 28 13:21 conf
drwxr-sr-x 2 root staff 4096 Nov 23 16:37 include
drwxr-xr-x 2 root root 4096 Nov 23 16:36 lib
drwxrwxrwx 1 root root 4096 Nov 28 13:21 logs
drwxr-sr-x 3 root staff 4096 Nov 23 16:37 native-jni-lib
drwxrwxrwx 2 root root 4096 Nov 23 16:36 temp
drwxr-xr-x 7 root root 4096 Nov 17 18:46 webapps
drwxrwxrwx 1 root root 4096 Nov 28 13:21 work
进入webapps,创建一个test文件夹,在里面再创建一个index.html
root@b2217a15d162:/usr/local/tomcat/webapps/test# echo "hello docker tomcat" > index.html
再进行访问
访问成功