这里是后面可能用到的一些命令,后面还会提到
进入容器:
docker exec -i -t fee2bbb7c9ee /bin/bash
查看进程:
docker ps
查看日志:
docker logs -f [container-id]
杀死容器:
docker kill
查看构建好的镜像
sudo docker images
强制删除镜像:
docker rmi -f
首先服务器要有安装docker,具体安装方法自行百度,或者宝塔安装更方便
在后端项目创建Dockerfile
Dockerfile 用于指定构建 Docker 镜像的方法
Dockerfile 一般情况下不需要完全从 0 自己写,建议去 github、gitee 等托管平台参考同类项目(比如 springboot)
Dockerfile 编写:
FROM maven:3.5-jdk-8-alpine as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Run the web service on container startup.
CMD ["java","-jar","/app/target/user-center-backend-0.0.1-SNAPSHOT.jar","--spring.profiles.active=prod"]
在项目根目录当中新建Dockerfile文件,把上述代码复制进去
然后把项目文件用git clone到服务器的文件夹下(比如用了宝塔的就是/www/wwwroot)
运行命令:(后面-t是给镜像打标签,具体docker命令参考docker 菜鸟教程
//后端(没权限用sudo)
user-center-backend>> sudo docker build -t user-center-backend:v0.0.1 .
后端镜像构建成功
一个供参考的前端Dockerfile配置
可能的踩坑点:注意下面需要有dist文件夹和docker文件夹,这两个必须得先有(于是我们待会直接把构建好的dist文件夹放上去再交给docker构建),但好像有方法不用实现构建出dist,由于博主不了解Docker构建过程,所以这里留个坑。
FROM nginx
WORKDIR /usr/share/nginx/html/
USER root
COPY ./docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY ./dist /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
在前端项目新建Dockerfile文件,复制粘贴上述内容
nginx.conf参考配置:
server {
listen 80;
# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
location / {
try_files $uri /index.html;
}
}
新建docker文件夹,然后在docker文件夹下面新建nginx.conf文件夹,复制粘贴上述内容,比较重要的是最后的location,找不到url降级寻找index.html
然后参考后端,将项目文件拉取到服务器中(比如用git,注意git拉取http加sudo),构建前端镜像
user-center-backend>> cd ..
wwwroot>> cd user-center-frontend
//前端(没有权限用sudo)
user-center-frontend>> sudo docker build -t user-center-frontend:v0.0.1 .
//可以看下已经安装的镜像(没有权限用sudo)
user-center-frontend>> sudo docker images
#REPOSITORY TAG IMAGE ID CREATED SIZE
#user-center-frontend v0.0.1 b0bfb35288eb 27 seconds ago 144MB
#user-center-backend v0.0.1 ec4c8279aed2 49 minutes ago 217MB
#nginx latest fa5269854a5e 4 days ago 142MB
#maven 3.5-jdk-8-alpine fb4bb0d89941 3 years ago 119MB
有兴趣的话可以进行构建优化:减少大小、减小构建时间(多阶段构建),在Dockerfile进行一定配置
注意:记得把其他占用80端口和8080端口的进程停了,如果以前有站点在运行需要停用,但是以前的nginx服务器,它还是占用了80端口,所以我们还要把nginx也关了
关键的启动命令:(先别急着执行)
# 前端
docker run -p 80:80 -d user-center-frontend:v0.0.1
# 后端
docker run -p 8080:8080 user-center-backend:v0.0.1
虚拟化介绍
//前端(没有权限用sudo) -d以后台的方试运行
~>> docker run -p 80:80 -d user-center-frontend:v0.0.1
//用netstat -ntlp看看80端口是否在监听
netstat -ntlp
此时应该可以访问公网ip
//后端(没有权限用sudo)
~>> docker run -p 8080:8080 user-center-backend:v0.0.1
访问api端口,看是否返回json信息
//查看进程(可以查看已经启动的容器ID,没有权限用sudo)
//容器ID不是固定的,不要盲目复制命令哦
~>> docker ps
#CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#61444cbea251 user-center-frontend:v0.0.1 "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp recursing_cori
//查看日志 docker logs 容器ID
//这些都是咱们的访问记录,可以看到哪个用户,哪个IP,在什么时间访问了你的网页的哪些文件
~>> docker logs 61444cbea251
#/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
#/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
#/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
#10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
#10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
#/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
#/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
#/docker-entrypoint.sh: Configuration complete; ready for start up
#2022/04/28 03:35:23 [notice] 1#1: using the "epoll" event method
#2022/04/28 03:35:23 [notice] 1#1: nginx/1.21.6
#2022/04/28 03:35:23 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
#2022/04/28 03:35:23 [notice] 1#1: OS: Linux 3.10.0-1160.49.1.el7.x86_64
#2022/04/28 03:35:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
#2022/04/28 03:35:23 [notice] 1#1: start worker processes
#2022/04/28 03:35:23 [notice] 1#1: start worker process 30
#2022/04/28 03:35:23 [notice] 1#1: start worker process 31
#1.1.1.1 - - [28/Apr/2022:03:37:28 +0000] "GET / HTTP/1.1" 200 1919 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" "-"
...(省略)...
//-f跟踪日志输出(没有权限用sudo)
~>> docker logs 容器ID -f
//ctrl+c退出 杀死掉这个容器 docker kill 容器ID
~>> docker kill 61444cbea251
#61444cbea251
//查看进程,没有在启动的容器了
~>> docker ps
#CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//查看镜像(没有权限用sudo)
~>> docker images
#REPOSITORY TAG IMAGE ID CREATED SIZE
#user-center-frontend v0.0.1 9722984f9385 20 hours ago 144MB
#user-center-backend v0.0.1 ec4c8279aed2 3 days ago 217MB
#nginx latest fa5269854a5e 7 days ago 142MB
#maven 3.5-jdk-8-alpine fb4bb0d89941 3 years ago 119MB
//强制删除镜像 docker rmi -f 镜像ID
//举个例子可以下helloworld镜像,删除试试,我没把user-center-frontend删掉[doge]保命
~>> docker rmi -f 9722984f9385