ubuntu docker安装与部署java,mysql,nginx镜像

docker 安装与部署java,mysql,nginx

docker 配置

安装docker

$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
$ sudo docker run hello-world

配置阿里云镜像

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://wrb0v2zy.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装部分

java

docker pull java:8u111

MySQL

docker pull mysql:5.7
docker run -p 3306:3306 --name mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

jar

  1. 在docker文件夹中放入jar文件并创建Dockerfile,写入如下信息
FROM java:8
ADD questionnaire-0.0.1-SNAPSHOT.jar questionnaire.jar
RUN bash -c 'touch /questionnaire.jar'
ENTRYPOINT ["java","-jar","/questionnaire.jar"]
  1. 构建docker镜像
docker build -t springbootdemo4docker .
  1. 启动容器

docker run    --net=host  --name=name  -d -p 8081:8081 projectname

前端vue

  1. 在docker文件夹中放入dist文件夹,创建Dockerfile文件以及nginx.conf配置文件
  2. Dockerfile内容如下
FROM nginx
COPY ./dist/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
  1. nginx.conf
#user  nobody;
worker_processes  1;
  
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
  
#pid        logs/nginx.pid;
  
  
events {
    worker_connections  1024;
}
  
  
http {
    include       mime.types;
    default_type  application/octet-stream;
  
    #access_log  logs/access.log  main;
  
    sendfile        on;
    #tcp_nopush     on;
  
    #keepalive_timeout  0;
    keepalive_timeout  65;
  
    #gzip  on;
  
    client_max_body_size   20m;
    server {
        listen       80;
        server_name  www.aaaaaa.com;
  
        #charset koi8-r;
  
        #access_log  logs/host.access.log  main;
     location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
 
    }
  
}
  1. 打包
docker build -t nginx-test .
  1. 运行
docker run --name nginx-docker -p 8050:80 -d nginx-test

注意事项

  1. 由于测试时前后端项目都是在本地开发与调试,数据库与后台url写的都是localhost,要想在docker中部署需要改成服务器对应的ip.
  2. 启动java后台需要加上–net=host,不然无法访问数据库

你可能感兴趣的:(后端)