![](/style/images/good.png)
![](/style/images/bad.png)
创建一个基于docker的http应用(二)
source link: https://www.tuicool.com/articles/EFRFZv7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
书接上文。
https://www.jianshu.com/p/1d67c9b5d7c7
我们已经成功在WSL上安装了docker环境,也成功的run了一个容器应用。接下来,我们想要自己开发一个容器的应用应该需要怎么做。
目标:
开发一个web-app应用,当我们请求这个service的时候,返回一行文字“Hello Docker”。
语言:Golang
正文
一、首先本地编写web app应用,保证在本地编译通过,并且运行以后可以通过本地8080端口访问。
package main import ( "io" "log" "net/http" ) func helloServer(w http.ResponseWriter, req *http.Request){ io.WriteString(w, "hello, docker! \n") } func main() { // 地址 http.HandleFunc("/helloDocker", helloServer) // 监听8080端口 err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("ListenAndServe :8080 err: ", err) } }
二、编写Dockerfile文件
在源码本地新建Dockerfile文件,编译运行镜像可都靠他了。
# 得到最新的 golang docker 镜像 FROM golang:latest # 在容器内部创建一个目录来存储我们的 web 应用,接着使它成为工作目录。 RUN mkdir -p /go/src/web-app WORKDIR /go/src/web-app # 复制 web-app 目录到容器中 COPY . /go/src/web-app # build app RUN go build -o http . # 设置 PORT 环境变量,容器对外映射的本地端口,需要在 docker run 的时候使用-p或者-P选项生效 ENV PORT 8080 # 给主机暴露 8080 端口,这样外部网络可以访问你的应用 EXPOSE 8080 # 告诉 Docker 启动容器运行的命令 CMD ["/go/src/web-app/http"]
在WSL上访问源码地址,到带有Dockerfile的目录下,创建刚才通过Dockerfile定制的镜像。
命令:docker image build -t helloworld .
其中helloworld是要创建image的名字。
执行结果如下:
Step 1/8 : FROM golang:latest ---> f50db16df5da Step 2/8 : RUN mkdir -p /go/src/web-app ---> Using cache ---> 94af41c746d7 Step 3/8 : WORKDIR /go/src/web-app ---> Running in a4bd4dc07e2b Removing intermediate container a4bd4dc07e2b ---> d5f1fafd2a78 Step 4/8 : COPY . /go/src/web-app ---> 8a1068ea109f Step 5/8 : RUN go build -o http . ---> Running in 6e00fe44a12f Removing intermediate container 6e00fe44a12f ---> 5da2988454af Step 6/8 : ENV PORT 8080 ---> Running in 92f5799b7d5b Removing intermediate container 92f5799b7d5b ---> c15a810c9326 Step 7/8 : EXPOSE 8080 ---> Running in 6881b5000f43 Removing intermediate container 6881b5000f43 ---> 2c42c70a4e4a Step 8/8 : CMD ["/go/src/web-app/http"] ---> Running in b491a16f661c Removing intermediate container b491a16f661c ---> 437a930f078d Successfully built 437a930f078d Successfully tagged helloworld:latest
从以上可以看到,我们在Dockerfile中的定制命令,在以上步骤中均有体现。关于Dockerfile,之后的文章会做详细讲解。
现在再执行: docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE helloworld latest 437a930f078d 2 minutes ago 781MB
可以看到我们刚才创建的helloworld镜像已经存在于docker的image中。
三、运行镜像容器
命令:docker container run --rm -p 8000:8080 -d helloworld
执行结果:
root@Cla:/mnt/h/goLand/helloWorld# docker container run --rm -p 8000:8080 -d helloworld 91a5396679ec8dfac4da7c89b5fdff1a1a1dd1c68b1851b9a26d31a00f3351c7
表示容器运行成功。
四、查看执行结果
切回windows,打开浏览器,输入:http://127.0.0.1:8000/helloDocker
可以看到浏览器打出:hello, docker!
![ryem2af.jpg!web](https://img1.tuicool.com/ryem2af.jpg!web)
result.JPG
说明我们的容器已经运行成功。
在WSL界面,查看运行中的container,输入: docker ps -a
可以看到运行中的容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91a5396679ec helloworld "/go/src/web-app/http" 9 minutes ago Up 9 minutes 0.0.0.0:8000->8080/tcp mystifying_mcnulty
五、结束容器,删除镜像
现在我们要删除之前的镜像。运行中的容器镜像是不能删除的。首先我们要先结束运行中的容器。这里要用container ID进行操作。
执行:docker stop 91a5396679ec
此时可以用 docker ps -a
查询是否停止成功。
docker image ls
命令,查到对应的image id为
437a930f078d
执行命令:
docker rmi 437a930f078d
即可以删除对应image,执行结果为:
root@Cla:/mnt/h/goLand/helloWorld# docker rmi 437a930f078d Untagged: helloworld:latest Deleted: sha256:437a930f078d91fd48a0e6d9e6e49aaabdeffb9adfd565306810e14dfd81138f Deleted: sha256:2c42c70a4e4a157ed8c14c5820bfee63544f967a0ef631ba16705de18426f495 Deleted: sha256:c15a810c932671737e090cef14c56e057dadf62e9067b986be70a0eb96535dea Deleted: sha256:5da2988454afd08df176d899297d32bffeabd482963af9ba89eca7eb8f92f61d Deleted: sha256:dfff25548ed8be231afe97c8084105cbed98624f10ee4757a09c506435ef2fdc Deleted: sha256:8a1068ea109ffc2eb4617c9a3f32b5c0c389cd2b2a21404225561c6035953be8 Deleted: sha256:f7906516a2acf4eca0c81efedc77e8346db651e9ea9d826371d4acbf540ab96f Deleted: sha256:d5f1fafd2a78471ec49b6130d672cb8d0833a9806156439feb8a2031df541429
有时候会遇到删除不了情况,有可能这个image已经被container使用。
执行 docker container ls
可以查看所有容器。
执行 docker rm xxx
可以删除对应的容器,xxx为容器ID
先删除容器,之后再删除image就不会出错了。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK