定制镜像
做个测试服务器,testServer代码如下
package main import ( "net/http" ) func main() { http.Handle("/", http.FileServer(http.Dir("./"))) http.ListenAndServe(":12345", nil) }
我们的目录结构如下
$ tree
.
├── Dockerfile
└── files_to_add
└── testServer
编辑Dockerfile
From debian RUN rm /etc/apt/sources.list RUN echo "deb http://ftp.cn.debian.org/debian jessie main contrib non-free" >> /etc/apt/sources.list RUN echo "deb http://ftp.cn.debian.org/debian jessie-updates main contrib non-free" >> /etc/apt/sources.list RUN echo "deb http://ftp.cn.debian.org/debian-security/ jessie/updates main contrib non-free" >> /etc/apt/sources.list RUN apt-get -qq update;apt-get upgrade -y;apt-get install build-essential -y; EXPOSE 12345 ADD ./files_to_add/ /myServer RUN chmod +x /myServer/testServer CMD "/myServer/testServer"
根据Dockerfile建立docker镜像,由于建立完毕以后会自动运行,这里强行删除新起来的container
docker build --force-rm --tag="rico/my_server:v1" ./mydocker/
运行
docker run --name="rico_server" --rm -p 11111:12345 rico/my_server:v1 & 或者 docker run -d --name="rico_server" -d -p 11111:12345 rico/my_server:v1
打开浏览器
http://127.0.0.1:11111
看到效果
附加几个技巧
docker删除无名镜像
docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker rm docker images|grep none|awk '{print $3 }'|xargs docker rmi
docker删除已停止的容器
docker rm $(docker ps -a -q)
docker退出时删除容器
docker run --rm
docker run是根据镜像启动一个新容器
docker start是根据已有的容器id启动容器