docker使用Dockerfile定制go语言beego框架项目的镜像流程步骤
我有一个基于go语言beego框架的项目,目前在线上跑着,准备自己用Dockerfile制作一个镜像以后迁移到时候方便用及记录起来,方便自己及有兴趣的老铁查阅
Dockerfile是一个包含用于组合映像的命令的文本文档。
可以使用在命令行中调用任何命令。 Docker通过读取Dockerfile中的指令自动生成映像。
docker环境我已经安装了
https://www.zongscan.com/demo333/304.html
正式进入步骤
1.创建本地目录 /home/godocker/ 用于放项目及Dockerfile文件
[[email protected] ~]# cd /home/
[[email protected] home]# ll
总用量 0
drwxr-xr-x 3 root root 53 8月 10 18:05 godocker
2.创建本地项目 /home/godocker/src
我本地项目就在这里,等于项目代码已经迁移进来了,后面启动容器直接挂载就OK了
[[email protected] godocker]# cd src
[[email protected] src]# ll
总用量 0
drwxr-xr-x 10 root root 204 8月 11 10:56 gblog
[[email protected] src]# cd gblog/
[[email protected] gblog]# ll
总用量 29560
drwxr-xr-x 2 root root 22 8月 10 18:17 conf
drwxr-xr-x 3 root root 142 8月 10 18:17 controllers
-rwxr-xr-x 1 root root 14691751 8月 11 10:56 gblog
-rw-r--r-- 1 root root 15557120 8月 10 15:36 gblog.exe
-rw-r--r-- 1 root root 326 5月 6 14:12 go.mod
-rw-r--r-- 1 root root 6204 5月 6 14:12 go.sum
-rw-r--r-- 1 root root 470 3月 5 13:36 main.go
drwxr-xr-x 2 root root 89 8月 10 18:17 models
drwxr-xr-x 2 root root 23 8月 10 18:17 routers
drwxr-xr-x 5 root root 38 8月 10 18:17 static
drwxr-xr-x 2 root root 29 8月 10 18:17 tests
drwxr-xr-x 2 root root 26 8月 10 18:17 utils
drwxr-xr-x 3 root root 149 8月 10 18:17 views
3.创建Dockerfile文件及写入定制镜像信息
[[email protected] ~]# cd /home/godocker/
[[email protected] godocker]# ll
总用量 4
-rw-r--r-- 1 root root 746 8月 11 10:55 Dockerfile
drwxr-xr-x 3 root root 19 8月 11 11:16 src
写入定制镜像信息:看下图
4.构建镜像使用docker build
docker build命令用于从Dockerfile构建映像。
可以在docker build命令中使用-f标志指向文件系统中任何位置的Dockerfile。
[[email protected] godocker]# docker build -t goblog .
Sending build context to Docker daemon 27.79MB
Step 1/10 : FROM golang
---> 0821480a2b48
Step 2/10 : MAINTAINER Razil "houtizong"
---> Running in b2e287e1fa28
Removing intermediate container b2e287e1fa28
---> c067bdef725b
Step 3/10 : RUN mkdir -p /go/src/beego_server
---> Running in eef6d77f6bbc
Removing intermediate container eef6d77f6bbc
---> d8404ed48196
Step 4/10 : WORKDIR /go/src
---> Running in 614a899855bd
Removing intermediate container 614a899855bd
---> dffd56d742ec
Step 5/10 : ENV GO111MODULE=on
---> Running in 11074d82f038
Removing intermediate container 11074d82f038
---> 02932ffd6a21
Step 6/10 : ENV GOPROXY="https://goproxy.io"
---> Running in 165e1144eb47
Removing intermediate container 165e1144eb47
---> b2efd67dd8d4
Step 7/10 : RUN apt update
---> Running in d7eac67cb05d
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Get:1 http://deb.debian.org/debian buster InRelease [122 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [300 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [15.2 kB]
Fetched 8460 kB in 2min 36s (54.2 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
4 packages can be upgraded. Run 'apt list --upgradable' to see them.
Removing intermediate container d7eac67cb05d
---> 38493526c146
Step 8/10 : RUN apt install -y vim
---> Running in 27e849161ad0
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
libgpm2 vim-common vim-runtime xxd
Suggested packages:
gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
libgpm2 vim vim-common vim-runtime xxd
0 upgraded, 5 newly installed, 0 to remove and 4 not upgraded.
Need to get 7425 kB of archives.
After this operation, 33.8 MB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 xxd amd64 2:8.1.0875-5 [140 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 vim-common all 2:8.1.0875-5 [195 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 libgpm2 amd64 1.20.7-5 [35.1 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 vim-runtime all 2:8.1.0875-5 [5775 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 vim amd64 2:8.1.0875-5 [1280 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 7425 kB in 9s (820 kB/s)
Selecting previously unselected package xxd.
(Reading database ... 15421 files and directories currently installed.)
Preparing to unpack .../xxd_2%3a8.1.0875-5_amd64.deb ...
Unpacking xxd (2:8.1.0875-5) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a8.1.0875-5_all.deb ...
Unpacking vim-common (2:8.1.0875-5) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../libgpm2_1.20.7-5_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-5) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a8.1.0875-5_all.deb ...
Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:8.1.0875-5) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a8.1.0875-5_amd64.deb ...
Unpacking vim (2:8.1.0875-5) ...
Setting up libgpm2:amd64 (1.20.7-5) ...
Setting up xxd (2:8.1.0875-5) ...
Setting up vim-common (2:8.1.0875-5) ...
Setting up vim-runtime (2:8.1.0875-5) ...
Setting up vim (2:8.1.0875-5) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.28-10) ...
Processing triggers for mime-support (3.62) ...
Removing intermediate container 27e849161ad0
---> 792995c83f9e
Step 9/10 : RUN go get github.com/astaxie/beego
---> Running in 5eb600e5e1cc
go: downloading github.com/astaxie/beego v1.12.3
go: downloading github.com/hashicorp/golang-lru v0.5.4
go: downloading github.com/prometheus/client_golang v1.7.0
go: downloading golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/prometheus/common v0.10.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/golang/protobuf v1.4.2
go: downloading github.com/prometheus/procfs v0.1.3
go: downloading golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: downloading golang.org/x/net v0.0.0-20190620200207-3b0461eec859
go: downloading google.golang.org/protobuf v1.23.0
go: downloading golang.org/x/text v0.3.0
Removing intermediate container 5eb600e5e1cc
---> 827aa1296bcc
Step 10/10 : RUN go get github.com/beego/bee
---> Running in 2098faf348bc
go: downloading github.com/beego/bee v1.12.3
go: downloading gopkg.in/yaml.v2 v2.3.0
go: downloading github.com/fsnotify/fsnotify v1.4.9
go: downloading github.com/gadelkareem/delve v1.4.2-0.20200619175259-dcd01330766f
go: downloading github.com/gorilla/websocket v1.4.2
go: downloading github.com/go-sql-driver/mysql v1.5.0
go: downloading github.com/lib/pq v1.7.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915
go: downloading github.com/pelletier/go-toml v1.2.0
go: downloading github.com/smartwalle/pongo2render v1.0.1
go: downloading github.com/spf13/viper v1.7.0
go: downloading golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9
go: downloading github.com/cosiner/argv v0.1.0
go: downloading github.com/mattn/go-colorable v0.0.9
go: downloading github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/astaxie/beego v1.12.1
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/magiconair/properties v1.8.1
go: downloading github.com/mitchellh/mapstructure v1.1.2
go: downloading github.com/spf13/afero v1.1.2
go: downloading github.com/spf13/cast v1.3.0
go: downloading github.com/spf13/jwalterweatherman v1.0.0
go: downloading github.com/spf13/pflag v1.0.3
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading gopkg.in/ini.v1 v1.51.0
go: downloading go.starlark.net v0.0.0-20190702223751-32f345186213
go: downloading github.com/mattn/go-isatty v0.0.3
go: downloading golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4
go: downloading github.com/konsorten/go-windows-terminal-sequences v1.0.3
go: downloading golang.org/x/text v0.3.2
Removing intermediate container 2098faf348bc
---> 4b4b017a2545
Successfully built 4b4b017a2545
Successfully tagged goblog:latest
看到Successfully字样就说明制作好了,用docker images看一下
[[email protected] godocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
goblog latest 4b4b017a2545 3 minutes ago 1.16GB
golang latest 0821480a2b48 5 days ago 862MB
hyperf/hyperf 7.4-alpine-v3.11-swoole e14802af67f0 2 months ago 108MB
5.运行docker容器goblog
通过-v "/home/godocker/src":/go/src/将本地目录挂载到容器上的目录,容器销毁项目代码不影响
[[email protected] godocker]# docker run --init -p 8080:8080 -itd --rm --name goblogbeego -v "/home/godocker/src":/go/src/ -w /go/src/gblog goblog bee run
be8cf8bd2a78939914f4d719e8cb51baaf6f4c43136e1522f81de07a215c186a
[[email protected] godocker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be8cf8bd2a78 goblog "bee run" 3 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp goblogbeego
103fd0f26448 hyperf/hyperf:7.4-alpine-v3.11-swoole "/bin/sh" 2 months ago Exited (255) 3 weeks ago 0.0.0.0:9601->9601/tcp hyperf
制作镜像步骤到这就完成了,进容器里面看看
[[email protected] godocker]# docker exec -it be8cf8bd2a78 /bin/sh
# ls
conf controllers gblog gblog.exe go.mod go.sum main.go models routers static tests utils views
# cd /
# ls
bin boot devetc go home lib lib64 media mnt opt proc rootrun sbin srvsys tmp usr var
# cd go
# ls
bin pkg src
# cd src
# ls
gblog
# cd gblog
# ls
conf controllers gblog gblog.exe go.mod go.sum main.go models routers static tests utils views
用exit命令退出容器
OK,跑一下beego项目,看看效果
扩展知识
如果要启动多个容器的话可以通过shell脚本编写bash创建多个容器,例如:
#!/bin/bash
echo "start gobeego1"
docker run --init -p 8080:8080 -itd --rm --name goblogbeego1 -v "/home/godocker/src":/go/src/ -w /go/src/gblog goblog bee run
echo "start gobeego2"
docker run --init -p 8080:8080 -itd --rm --name goblogbeego2 -v "/home/godocker/src":/go/src/ -w /go/src/gblog goblog bee run
完
相关文章