如何使用docker-compose安装软件

2023-05-13

这篇文章主要讲解了“如何使用docker-compose安装软件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用docker-compose安装软件”吧!

 创建docker-compose基础目录

mkdir -p /usr/local/docker

1、安装mysql

    在/usr/local/docker/目录下创建mysql目录

mkdir -p /usr/local/docker/mysql

   在/usr/local/docker/mysql目录编写docker-compose.yml文件

   注:vi编辑器在命令模式输入 set paste ,再输入i 可以带格式粘贴内容到文件

version: '3.1'
services:
  db: 
   image: mysql
   restart: always
   environment:
    MYSQL_ROOT_PASSWORD: root
   command:
    --default-authentication-plugin=mysql_native_password
    --character-set-server=utf8mb4
    --collation-server=utf8mb4_general_ci
    --explicit_defaults_for_timestamp=true
    --lower_case_table_names=1
   ports:
    - 3306:3306
   volumes:
    - ./data:/var/lib/mysql
  adminer:
   image: adminer
   restart: always
   ports:
    - 9999:8080

   运行容器:docker-compose -f docker-compose.yml up -d

   销毁容器   docker-compose down

2、安装gitlab

version: '3.1'
services:
  web:
    image: 'twang2218/gitlab-ce-zh'
    restart: always
    hostname: '192.168.100.102'
    environment:
      TZ: 'Asia/Shanghai'
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://192.168.100.102'
        gitlab_rails['gitlab_shell_ssh_port'] = 2222
        unicorn['port'] = 8888
        nginx['listen_port'] = 80
    ports:
      - '8000:80'
      - '443:443'
      - '2222:22'
    volumes:
      - ./config:/etc/gitlab
      - ./data:/var/opt/gitlab
      - ./logs:/var/log/gitlab

启动后访问:http://192.168.100.102

3、安装nexus3

version: '3.1'
services: 
  nexus: 
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
     - 8081:8081
    volumes:
     - nexus-data:/nexus-data
volumes:
  nexus-data:

启动后访问:http://192.168.100.102:8081

maven使用nexus3

在setting.xml配置认证信息,在<servers>节点添加如下配置:

<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

在pom.xml添加部署配置

<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Nexus Release Repository</name>
        <url>http://192.168.100.101:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://192.168.100.101:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

在pom.xml设置代理仓库

<repositories>
     <repository>
         <id>nexus</id>
         <name>Nexus Repository</name>
         <url>http://192.168.100.101:8081/repository/maven-public/</url>
         <snapshots>
             <enabled>true</enabled>
         </snapshots>
         <releases>
             <enabled>true</enabled>
         </releases>
     </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://192.168.100.101:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

配置pom.xml的build信息

<build>
    <finalName>myshop</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 资源文件拷贝插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!--Java 编译插件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- 打包时跳过测试 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

nexus 3中,已经没有了能对maven私服进行deploy的默认用户,只有admin和匿名用户。为了让一个角色能够对maven进行deploy,需要向它添加匿名角色所有权限和如下几个权限:

4、安装harbor

    4.1、下载安装包:https://github.com/goharbor/harbor/releases

    4.2、上传到 /usr/local/docker/目录

    4.3、解压 tar -zxvf harbor-offline-installer-v1.9.1-rc1.tgz

    4.4、进入到harbor目录修改harbor.yml文件

hostname: 192.168.100.102
http:
  port: 8090
harbor_admin_password: Harbor12345

    4.5、执行 install.sh 

    启动后访问:http://192.168.100.101:8090 用户名:admin 密码:Harbor12345

    4.6、配置客户端

##没有此文件就创建
vim /etc/docker/daemon.json

{
  "registry-mirrors":[
     "https://registry.docker-cn.com"
  ],
  "insecure-registries":[
   "192.168.100.101:8090"
  ]
}
注意:该文件必须符合JSON规范,否则docker将不能启动

4.7、重启服务

 systemctl daemon-reload
 systemctl restart docker

4.8、检查私服配置

docker info

4.9 、制作镜像提交到harbor仓库

##拉取nginx
docker pull nginx
## 在项目中标记镜像   
## docker tag SOURCE_IMAGE[:TAG] 192.168.100.101:8090/itchao-saas/IMAGE[:TAG]
docker tag nginx:latest 192.168.100.101:8090/itchao-saas/nginx:latest
## 登录
docker login 192.168.100.101:8090 -u admin -p Harbor12345
## 推送镜像到当前项目
## docker push 192.168.100.101:8090/itchao-saas/IMAGE[:TAG]
docker push 192.168.100.101:8090/itchao-saas/nginx:latest

5、docker-compose设置网络

   预先创建一个网络

##创建网络
docker network create <Network Name>
##查看已存在的网络
docker network list

在docker-compose.yml中指定网络

networks:
  default:
    external:
      name: myapp

6、nginx安装

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    ports:
      - 8080:80
      - 80:80
      - 443:443
    volumes:
      - ./conf.d:/etc/nginx/conf.d
      - ./log:/var/log/nginx
      - ./www:/var/www
      - ./etc/letsencrypt:/etc/letsencrypt

感谢各位的阅读,以上就是“如何使用docker-compose安装软件”的内容了,经过本文的学习后,相信大家对如何使用docker-compose安装软件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

《如何使用docker-compose安装软件.doc》

下载本文的Word格式文档,以方便收藏与打印。