docker-compose安装

docker-compose安装

1.安装docker

# 查看有没有安装旧版本的
[root@localhost ~]# yum remove docker docker-common docker-selinux docker-engine
# 安装依赖软件包
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置yum源
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 查看所有docker版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
# 安装
[root@localhost ~]# yum install docker-ce -y
# 查看版本
[root@localhost ~]# docker version
# 启动
[root@localhost ~]# systemctl start docker

2.安装docker-compose

# 下载docker compose
[root@localhost ~]# curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 添加可执行权限
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
# 将文件copy到 /usr/bin/目录下
[root@localhost ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# 查看版本
[root@localhost ~]# docker-compose --version

3.编写Dockerfile文件

FROM java:8
ADD est.jar est.jar
RUN bash -c "touch /est.jar"
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "est.jar", "--spring.profiles.active=prod", "--server.port=8080", "> /log/est.log"]

4.编写docker-compose文件

version: '3'

services:
  nginx: #服务名称
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - /usr/local/docker/nginx/html:/usr/share/nginx/html  #挂载:前面是挂载到哪儿,后面是nginx得配置目录
      - /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /usr/local/docker/nginx/log:/var/log/nginx
    privileged: true   #解决nginx得文件调用权限的问题
  mysql:
    image: mysql:5.7
    ports:
      - 3306:3306    #前面是宿主机的端口,后边是容器自身的端口
    environment:
      - MYSQL_ROOT_PASSWORD= #输入自己的密码
  redis:
    image: redis:5.0.11
  est:
    image: est:latest
    build: .
    ports:
      - 8080:8080
    depends_on:
      - mysql
      - redis

5.项目打包

5.1 前端打包

1.右键单击项目,勾选npm脚本 docker-compose安装_第1张图片

2.在NPM脚本中build即可docker-compose安装_第2张图片
3.获取打包文件dist
docker-compose安装_第3张图片
打包好的文件放入 - > /usr/local/docker/nginx/html

5.2 后端打包

1.pom文件中导入依赖

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.在Maven中clean后install即可
docker-compose安装_第4张图片

5.2.1 新增配置文件application-prod.yml

- 复制application-dev.yml,新文件名改为application-prod.yml
- 修改mysql、redis地址
  - mysql: IP地址改为容器名
    - url: jdbc:mysql://mysql:3306/est?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false
  - redis:IP地址改为容器名
    - host: redis

6.编写nginx配置
文件位置:/usr/local/docker/nginx/conf/nginx.conf

worker_processes 1;

events {
 worker_connections 1024;
}

http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;

 server {
 	listen 80;
 	server_name www.pinzhi365;

 	location /wxlogin {
          proxy_pass http://192.168.1.150:8080;
	}
 }
 
 server {
	listen 80;
	server_name www.est.com;

	location / {
		try_files $uri $uri/ /index.html;
		root /usr/share/nginx/html;
		index index.html;
	}
	
	location /api {
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		
		proxy_pass http://192.168.1.150:8080/api;
	}
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root html;
	}
 }
}

7.启动

docker-compose up -d

你可能感兴趣的:(docker,容器,运维)