肉饼博客

Talk is cheap. Show me the code.

0%

Docker初尝试

方式一:命令行启动(需cd到对应目录下)

1.编写命令行
1
2
3
4
5
6
7
8
docker container run \
-d \
-p 4000:80 \
--rm \
--name payrollCache \
--volume "$PWD/dist":/usr/share/nginx/html \
--volume "$PWD/nginx":/etc/nginx \
nginx
2.修改nginx配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ /api/ {
proxy_pass http://172.17.0.1:8097;// docker0接口ip
}

...
}

方式二:简单的Dockerfile

1.编写Dockerfile文件
1
2
3
4
5
6
7
8
9
10
11
FROM nginx:1.17.6

LABEL maintainer="robin.wang" date="2019-12-25"

LABEL description="payrollCache UI with nginx"

COPY ./dist/ /usr/share/nginx/html/

COPY ./nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
2.生成image
1
docker image build -t payroll_test:1.0.0 .
3.启动容器
1
docker container run -d -p 4000:80 --rm --name payrollCache payroll_test:1.0.0

方式三:使用多步骤构建

1.编写Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FROM node:11.14.0 AS builder

COPY package.json package-lock.json /app/

WORKDIR /app

RUN npm install --registry=https://registry.npm.taobao.org

COPY . .

RUN npm run build:prod


FROM nginx:1.17.6

LABEL maintainer="robin.wang" date="2019-12-25"

LABEL description="payrollCache UI with nginx"

COPY --from=builder /app/dist/ /usr/share/nginx/html/

COPY --from=builder /app/nginx/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
2.编写.dockerignore文件
1
2
3
4
5
6
7
8
9
10
11
.git
node_modules
npm-debug.log
dist
mock
tests
.eslintignore
.eslintrc.js
.gitignore
.travis.yml
jest.config.js
3.生成image
1
docker image build -t payroll_test:1.0.0 .
4.导出image
1
docker save -o payroll.tar payroll:1.0.0
5.导入image
1
docker load -i payroll.tar
6.启动容器
1
docker container run -d -p 4000:80 --rm --name payrollCache payroll:1.0.0

参考文章:
官方安装文档
Docker入门教程
Nginx容器教程
Docker容器访问宿主机网络
缓存与指令