最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Centos 7.x 利用 Dockerfile 来创建Beego Framework
时间:2022-06-30 20:50:00 编辑:袖梨 来源:一聚教程网
Dockerfile包含创建镜像所需要的全部指令。基于在Dockerfile中的指令,我们可以使用Docker build命令来创建镜像。通过减少镜像和容器的创建过程来简化部署
Dockerfile 基本的语法是
使用#来注释
FROM 指令告诉 Docker 使用哪个镜像作为基础,这里我从自己的私有镜像库获取
MAINTAINER:设置该镜像的作者
RUN开头的指令会在创建中运行,比如安装一个软件包,在这里使用 yum 来安装了一些软件,在shell或者exec的环境下执行的命令。RUN指令会在新创建的镜像上添加新的层面,接下来提交的结果用在Dockerfile的下一条指令中
ADD:复制文件指令。它有两个参数
[root@docker data]# cat Dockerfile
# install beego
# version 1.0
FROM 127.0.0.1:5000/centos:latest
MAINTAINER ivonlee "[email protected]"
ENV GOPATH /data/golang
ENV PATH $GOPATH/bin:$PATH
RUN
mkdir -p /data/golang &&
yum install golang vim golang-vim git supervisor -y &&
go get github.com/astaxie/beego &&
go get github.com/beego/bee
ADD ./hello.go /data/golang/hello.go
RUN go build -o /data/golang/hello /data/golang/hello.go
RUN chmod 755 /data/golang/hello
WORKDIR /data/golang
EXPOSE 22 8080
CMD ["/data/golang/hello"]
beego框架测试文件hello.go,和Dockerfile放在同一目录
[root@docker data]# cat hello.go
package main
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) Get() {
this.Ctx.WriteString("hello world")
}
func main() {
beego.Router("/", &MainController{})
beego.Run()
}
bulid镜像,这里可以看到build的过程,是不是和打RPM包很相似。
[root@docker data]# docker build -t ivonlee/beego:0.2 .
Sending build context to Docker daemon 121.8 MB
Sending build context to Docker daemon
Step 0 : FROM 127.0.0.1:5000/centos:latest
---> 7322fbe74aa5
Step 1 : MAINTAINER ivonlee "[email protected]"
---> Using cache
---> 5e0641b86a2e
Step 2 : ENV GOPATH /data/golang
---> Using cache
---> a24c7200756f
Step 3 : ENV PATH $GOPATH/bin:$PATH
---> Using cache
---> 5c3318517537
Step 4 : RUN mkdir -p /data/golang && yum install golang vim golang-vim git supervisor -y && go get github.com/astaxie/beego && go get github.com/beego/bee
---> Using cache
---> e3d7183077bd
Step 5 : ADD ./hello.go /data/golang/hello.go
---> Using cache
---> 38353c802079
Step 6 : RUN go build -o /data/golang/hello /data/golang/hello.go
---> Using cache
---> ff18e9b3001a
Step 7 : RUN chmod 755 /data/golang/hello
---> Using cache
---> 3d047ec8add6
Step 8 : WORKDIR /data/golang
---> Running in 358fddd90750
---> d8c363846643
Removing intermediate container 358fddd90750
Step 9 : EXPOSE 22 8080
---> Running in ac57f3f00c8a
---> 032dae53209b
Removing intermediate container ac57f3f00c8a
Step 10 : CMD /data/golang/hello
---> Running in 85f17b350eba
---> ef5ba42cf19c
Removing intermediate container 85f17b350eba
Successfully built ef5ba42cf19c
-t: 为构建的镜像制定一个标签,便于记忆/索引等
. : 指定Dockerfile文件在当前目录下
查看生成的镜像,我上面bulid的是0.2这个版本
[root@docker data]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ivonlee/beego 0.2 ef5ba42cf19c 4 minutes ago 539.1 MB
ivonlee/beego 0.1 6b4c8db1ee6d 10 minutes ago 539.1 MB
如果想删除某个镜像,可以用 docker rmi ef5ba42cf19c
基于生成的镜像运行一个容器
[root@docker data]# docker run -d -P -i --name beegotest2 ivonlee/beego:0.2
a69634223a4cfced7667c6e1667117b63fb714782c57a8ed0f64099183b0a90e
–name 是给容器取名,方便自己识别
-P 是自动映射容器里面端口,宿主机会随机映射一个端口
查看容器
[root@docker data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a69634223a4c ivonlee/beego:0.2 "/data/golang/hello" 11 seconds ago Up 10 seconds 0.0.0.0:32788->22/tcp, 0.0.0.0:32789->8080/tcp beegotest2
6553473b32ed ivonlee/beego:0.1 "/bin/bash" 5 minutes ago Up 5 minutes 0.0.0.0:32786->22/tcp, 0.0.0.0:32787->8080/tcp beegotest1
如果想删除某个容器的话,可以docker rm a69634223a4c, 如果只有一个a6开头的话,你甚至可以直接 docker rm a6,强制删除可以-f
可以看到容器开放了22和8080端口,宿主机将本地的32788映射到容器的22端口,32789映射到容器的8080端口,如果你想手动指定映射端口可以采用
docker run -d -p 8080:8080 -i --name beegotest2 ivonlee/beego:0.2
然后我们浏览器访问下容器里面的beego
92E7DD82-08F4-4A28-BB2E-DAEAD1C5C36B
如果想看容器内运行日志可以用docker logs
[root@docker data]# docker logs a69634223a4c
2015/07/17 03:53:39 [config.go:314] [W] open /data/golang/conf/app.conf: no such file or directory
2015/07/17 03:53:39 [asm_amd64.s:2232] [I] http server Running on :8080
2015/07/17 03:54:07 [router.go:854] [D] | GET | / | 69.504µs | match | / |
2015/07/17 03:54:11 [router.go:854] [D] | GET | / | 56.677µs | match | / |
2015/07/17 04:01:53 [router.go:854] [D] | GET | / | 257.78µs | match | / |
2015/07/17 06:36:26 [router.go:854] [D] | GET | / | 472.511µs | match | / |
如果想停止重启容器可以用 docker stop和docker restart
[root@docker data]# docker stop a69634223a4c
a69634223a4c
[root@docker data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a69634223a4c ivonlee/beego:0.2 "/data/golang/hello" 2 hours ago Exited (2) 2 seconds ago beegotest2
6553473b32ed ivonlee/beego:0.1 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:32786->22/tcp, 0.0.0.0:32787->8080/tcp beegotest1
可以看到a69634223a4c 这个容器已经是exit状态
如果你想将容器保存为镜像,可以用docker commit
docker commit a69634223a4c newbeego
如果你想进一步对容器进行修改,可以ssh进去,前提是你的镜像里面设置过ssh密码,或者在制作镜像的时候,加入
RUN echo "root:123456" | chpasswd
或者通过docker exec无密码进入
docker exec -it a69634223a4c /bin/bash
中间是容器的CONTAINER ID,通过docker ps -a可以看到
测试通过后,我们将镜像提交到我们私有镜像库
[root@docker data]# docker tag ef5ba42cf19c 127.0.0.1:5000/beego
[root@docker data]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ivonlee/beego 0.2 ef5ba42cf19c 29 minutes ago 539.1 MB
127.0.0.1:5000/beego latest ef5ba42cf19c 29 minutes ago 539.1 MB
ivonlee/beego 0.1 6b4c8db1ee6d 35 minutes ago 539.1 MB
docker.io/registry latest ddfbba49d0b6 2 days ago 423.6 MB
docker.io/centos latest 7322fbe74aa5 4 weeks ago 172.2 MB
127.0.0.1:5000/centos latest 7322fbe74aa5 4 weeks ago 172.2 MB
[root@docker data]# docker push 127.0.0.1:5000/beego
The push refers to a repository [127.0.0.1:5000/beego] (len: 1)
Sending image list
Pushing repository 127.0.0.1:5000/beego (1 tags)
Image f1b10cd84249 already pushed, skipping
Image 7322fbe74aa5 already pushed, skipping
Image c852f6d61e65 already pushed, skipping
5e0641b86a2e: Image successfully pushed
a24c7200756f: Image successfully pushed
5c3318517537: Image successfully pushed
e3d7183077bd: Image successfully pushed
38353c802079: Image successfully pushed
ff18e9b3001a: Image successfully pushed
3d047ec8add6: Image successfully pushed
d8c363846643: Image successfully pushed
032dae53209b: Image successfully pushed
ef5ba42cf19c: Image successfully pushed
Pushing tag for rev [ef5ba42cf19c] on {http://127.0.0.1:5000/v1/repositories/beego/tags/latest}
[root@docker data]# curl 127.0.0.1:5000/v1/search
{"num_results": 2, "query": "", "results": [{"description": "", "name": "library/centos"}, {"description": "", "name": "library/beego"}]}
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21