Docker неправильно составлять контейнеры - PullRequest
0 голосов
/ 15 апреля 2020

Мне нужно запустить контейнер загрузочного приложения Spring с контейнером mysql docker, используя docker compose в Ubuntu. У меня есть простой контроллер REST, настроенная база данных в файле application.properties. Также я создал образ загрузочного приложения Dockerfile для Spring и файл docker -compose.yml.

Но когда я запускаю его с docker -compose up Журналы контейнера приложения Spring показывают, что Tomcat запущен на портах: 8086 (http) с контекстным путем '' , но я установил порт 10222 в docker -compose.yml и application.properties. Когда я пытаюсь запустить http://localhost: 8086 / all / create или http://localhost: 10222 / all / create не работает.

Я не понимаю, почему docker устанавливает собственный порт вместо моего значения, и я не могу проверить запрос в Postman. Я настроил docker для работы без sudo в Ubuntu.

Код:

Ресурс. java

@RestController
@RequestMapping("/all")
public class Resource {
  @Autowired
  private UserRepository userRepository;

  @GetMapping("/")
  public List<Users> all() {
      return userRepository.findAll();
  }

  @GetMapping("/create")
  public List<Users> users() {
      Users users = new Users();
      users.setId(1);
      users.setName("Sam");
      users.setSalary(3400);
      users.setTeamName("Development");
      userRepository.save(users);
      return userRepository.findAll();
  }
}

application.properties

spring.datasource.url=jdbc:mysql://docker-mysql:3306/test_docker

spring.datasource.username = root
spring.datasource.password = password

server.port=10222

# spring.datasource.initialization-mode= always

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = update

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Dockerfile

FROM openjdk:8
COPY target/users-mysql.jar users-mysql.jar
ENTRYPOINT ["java","-jar","users-mysql.jar"]

docker -compose.yml

version: "3"
services:
  docker-mysql:
    restart: always
    container_name: docker-mysql
    image: mysql
    environment:
      MYSQL_DATABASE: test_docker
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    volumes:
      - ./sql:/docker-entrypoint-initdb.d
    ports:
      - "6033:3306"


  users-mysql:
    restart: on-failure
    build: ./
    expose:
      - "10222"
    ports:
      - 10222:10222
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/test_docker?autoReconnect=true
    depends_on:
      - docker-mysql

pom. xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ru.aplaksin</groupId>
    <artifactId>docker-mysql-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>docker-mysql-spring-boot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>users-mysql</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

пользователей. sql

DROP TABLE IF EXISTS `author`;

CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `salary` int(11) NOT NULL,
  `team_name` varchar(45) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Upd. журналы:

Building users-mysql
Step 1/3 : FROM openjdk:8
 ---> 0d54b885dc70
Step 2/3 : COPY target/users-mysql.jar users-mysql.jar
 ---> Using cache
 ---> aa455cd3a786
Step 3/3 : ENTRYPOINT ["java","-jar","users-mysql.jar"]
 ---> Using cache
 ---> 13bbd75347b6
Successfully built 13bbd75347b6
Successfully tagged docker-mysql-spring-boot_users-mysql:latest
Starting docker-mysql ... done
Starting docker-mysql-spring-boot_users-mysql_1 ... done
Attaching to docker-mysql, docker-mysql-spring-boot_users-mysql_1
docker-mysql    | 2020-04-15 19:48:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
docker-mysql    | 2020-04-15 19:48:13+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
docker-mysql    | 2020-04-15 19:48:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian10 started.
docker-mysql    | 2020-04-15T19:48:15.128671Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-mysql    | 2020-04-15T19:48:15.175038Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
docker-mysql    | 2020-04-15T19:48:24.153392Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
docker-mysql    | 2020-04-15T19:48:24.300883Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
docker-mysql    | 2020-04-15T19:48:24.943425Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
docker-mysql    | 2020-04-15T19:48:25.237539Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
users-mysql_1   | 
users-mysql_1   |   .   ____          _            __ _ _
users-mysql_1   |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
users-mysql_1   | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
users-mysql_1   |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
users-mysql_1   |   '  |____| .__|_| |_|_| |_\__, | / / / /
users-mysql_1   |  =========|_|==============|___/=/_/_/_/
users-mysql_1   |  :: Spring Boot ::        (v2.2.6.RELEASE)
users-mysql_1   | 
users-mysql_1   | 2020-04-15 19:48:27.616  INFO 1 --- [           main] r.a.d.DockerMysqlSpringBootApplication   : Starting DockerMysqlSpringBootApplication v0.0.1-SNAPSHOT on d44ee3620b39 with PID 1 (/users-mysql.jar started by root in /)
users-mysql_1   | 2020-04-15 19:48:27.650  INFO 1 --- [           main] r.a.d.DockerMysqlSpringBootApplication   : No active profile set, falling back to default profiles: default
users-mysql_1   | 2020-04-15 19:48:30.179  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
users-mysql_1   | 2020-04-15 19:48:30.307  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 111ms. Found 1 JPA repository interfaces.
users-mysql_1   | 2020-04-15 19:48:32.111  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8086 (http)
users-mysql_1   | 2020-04-15 19:48:32.196  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
users-mysql_1   | 2020-04-15 19:48:32.197  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
users-mysql_1   | 2020-04-15 19:48:32.458  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
users-mysql_1   | 2020-04-15 19:48:32.458  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4509 ms
users-mysql_1   | 2020-04-15 19:48:33.101  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
users-mysql_1   | 2020-04-15 19:48:33.421  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
users-mysql_1   | 2020-04-15 19:48:34.120  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
users-mysql_1   | 2020-04-15 19:48:34.459  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
users-mysql_1   | 2020-04-15 19:48:36.213  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
users-mysql_1   | 2020-04-15 19:48:36.315  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
docker-mysql    | mbind: Operation not permitted
users-mysql_1   | 2020-04-15 19:48:37.913  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
users-mysql_1   | 2020-04-15 19:48:37.922  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
users-mysql_1   | 2020-04-15 19:48:38.501  WARN 1 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
users-mysql_1   | 2020-04-15 19:48:38.714  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
users-mysql_1   | 2020-04-15 19:48:39.063  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path ''
users-mysql_1   | 2020-04-15 19:48:39.066  INFO 1 --- [           main] r.a.d.DockerMysqlSpringBootApplication   : Started DockerMysqlSpringBootApplication in 13.634 seconds (JVM running for 26.846)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...