docker install
https://docs.docker.com/desktop/previous-versions/2.x-mac/
docker --versionDocker version 19.03.13, build 4484c46d9d
{
"experimental": false,
"features": {
"buildkit": true
},
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}

run nginx
docker run -d -p 80:80 --name webserver nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
01b5b2efb836: Pull complete
db354f722736: Pull complete
abb02e674be5: Pull complete
214be53c3027: Pull complete
a69afcef752d: Pull complete
625184acb94e: Pull complete
Digest: sha256:c54fb26749e49dc2df77c6155e8b5f0f78b781b7f0eadd96ecfabdcdfa5b1ec4
Status: Downloaded newer image for nginx:latest
36e3bba8da3eb8fc2fd728c737cf6ad1a352232a62b73e70ae49666eb6054604
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36e3bba8da3e nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp webserver
停止 Nginx 服务器并删除
docker stop webserver
docker rm webserver

run node app ,参考 https://docs.docker.com/get-started/02_our_app/
git clone https://github.com/docker/getting-started.git
cd getting-started/app
vim Dockerfile
Dockerfile 内容
# syntax=docker/dockerfile:1
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
build -t getting-started .
docker images
docker run -dp 3000:3000 --name node-app getting-started
http://localhost:3000/

golang app
go mod init docker.demo
vim main.go
package main
import (
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, "Hello, Docker! <3")
})
e.GET("/ping", func(c echo.Context) error {
return c.JSON(http.StatusOK, struct{ Status string }{Status: "OK"})
})
httpPort := os.Getenv("HTTP_PORT")
if httpPort == "" {
httpPort = "8080"
}
e.Logger.Fatal(e.Start(":" + httpPort))
}
vim Dockerfile
# syntax=docker/dockerfile:1
FROM golang:1.17-alpine
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
ENV GOPROXY "https://goproxy.cn,direct"
RUN go mod download
COPY *.go ./
RUN go build -o /docker-gs-ping
EXPOSE 8080
CMD [ "/docker-gs-ping" ]
docker build --tag docker-gs-ping-api .
docker run -dp 8080:8080 docker-gs-ping-api
curl http://localhost:8080/
vim docker-compose.yaml
version: "3.9"
services:
app:
ports:
- "8080:8080"
env_file: env
build:
dockerfile: Dockerfile
context: .
command: /docker-gs-ping
container_name: ping-api
vim env
GOPROXY="https://goproxy.cn,direct"
docker-compose build
docker-compose up
Try Docker Compose
ref
- https://docs.docker.com/
- https://docker_practice.gitee.io/install/mirror.html
- https://docs.docker.com/get-started/02_our_app/
- https://docs.docker.com/language/golang/build-images/
- https://hub.docker.com/r/docker/compose/#!
- https://github.com/docker/awesome-compose
- https://www.freecodecamp.org/news/the-docker-handbook/
- https://www.freecodecamp.org/news/author/farhanhasin/
- https://www.freecodecamp.org/news/the-kubernetes-handbook/
网友评论