docker安装
下载docker
yum install docker
设置docker随系统启动
chkconfig docker on
启动docker服务
service docker start
启动后查看docker状态
systemctl status docker:
[root@dev-k8s ~]# systemctl status docker● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-06-13 15:50:16 CST; 7min ago
Docs: http://docs.docker.com
Main PID: 12197 (dockerd-current)
CGroup: /system.slice/docker.service
├─12197 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec...
└─12207 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --s...
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.283640021+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.323651305+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.324181137+08:00" level=info msg="Loading containers: start."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.327645142+08:00" level=info msg="Firewalld running: false"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.421269250+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 192.168.0.0/24. Daemon optio...d IP address"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.452900303+08:00" level=info msg="Loading containers: done."
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572134045+08:00" level=info msg="Daemon has completed initialization"
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.572172608+08:00" level=info msg="Docker daemon" commit="7d71120/1.13.1" graphdriver=overlay2 version=1.13.1
Jun 13 15:50:16 dev-k8s dockerd-current[12197]: time="2022-06-13T15:50:16.585165447+08:00" level=info msg="API listen on /var/run/docker.sock"
Jun 13 15:50:16 dev-k8s systemd[1]: Started Docker Application Container Engine.
go-helloworld代码
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.")
target := "Welcome to go"
fmt.Fprintf(w, "Hello, %s!\n", target)
}
func main() {
log.Print("Hello world sample started.")
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8009"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
go项目打包
CMD中进入main方法目录设置linux运行环境 后 打包
F:\私人代码\go-helloword>set GOARCH=amd64
F:\私人代码\go-helloword>set GOOS=linux
环境问题导致打包出错,使用go env -w GO111MODULE=off命令解决下
F:\私人代码\go-helloword>go build -o helloworld
go: cannot find main module, but found .git/config in F:\私人代码\go-helloword
to create a module there, run:
go mod init
F:\私人代码\go-helloword>go mod init
go: cannot determine module path for source directory F:\私人代码\go-helloword (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
F:\私人代码\go-helloword>go env -w GO111MODULE=off
重新打包后成功
F:\私人代码\go-helloword>go build -o helloworld
F:\私人代码\go-helloword>dir
驱动器 F 中的卷是 文档及代码
卷的序列号是 DB44-E624
F:\私人代码\go-helloword 的目录
2022/06/13 16:37 <DIR> .
2022/06/13 16:37 <DIR> ..
2022/06/10 16:43 6,148 .DS_Store
2022/06/13 10:33 <DIR> .idea
2022/06/13 16:20 425 Dockerfile
2022/06/13 16:37 6,066,257 helloworld
2022/06/10 17:50 448 helloworld.go
4 个文件 6,073,278 字节
3 个目录 307,233,075,200 可用字节
dockerfile编写,需要加上RUN chmod +x /build/helloworld命令,否则导致容器启动时对go二进制文件helloworld没有使用权限
FROM golang:alpine
WORKDIR /build
COPY helloworld .
EXPOSE 8009
RUN chmod +x /build/helloworld
CMD ["/build/helloworld"]
使用dockerfile创建镜像,然后使用镜像创建容器
使用dockerfile创建镜像
[root@dev-k8s docker-go]# docker build . -t go-helloworld:0.0.4
Sending build context to Docker daemon 6.069 MB
Step 1/6 : FROM golang:alpine
---> 155ead2e66ca
Step 2/6 : WORKDIR /build
---> Using cache
---> 6b6f9f40297e
Step 3/6 : COPY helloworld .
---> 44fefa2c08df
Removing intermediate container f2ed9e5c80b6
Step 4/6 : EXPOSE 8009
---> Running in 3dff69240776
---> b7ce1ff0c201
Removing intermediate container 3dff69240776
Step 5/6 : RUN chmod +x /build/helloworld
---> Running in 344e235c6ee8
---> 2dea00206ded
Removing intermediate container 344e235c6ee8
Step 6/6 : CMD /build/helloworld
---> Running in a8a56639f2fb
---> df2ca4071b8c
Removing intermediate container a8a56639f2fb
Successfully built df2ca4071b8c
用镜像创建容器
root@dev-k8s ~]# docker run -d -it -p 8009:8009 go-helloworld:0.0.4
29ea4e02b8319f564b6cf7073792f275c754fcbeb08bb6062683c1524f9cbc7c
访问helloworld项目
root@dev-k8s docker-go]# netstat -antp |grep 8009
tcp 0 0 10.6.0.11:33522 172.22.254.3:8009 ESTABLISHED 17128/n9e-agent
tcp6 0 0 :::8009 :::* LISTEN 12318/docker-proxy-
[root@dev-k8s docker-go]# curl localhost:8009
Hello, Welcome to go!