Spring boot와 Docker Postgresql
이번엔 docker와 posrgresql에 대해 알아보겠다.
1.
centos 6에 docker를 설치해보자
2.
Spring boot와 Docker
3.
Spring boot와 Docker Mysql
mysql과 소스는 동일하다.
@SpringBootApplication
public class SpringBootDockerPostgresqlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDockerPostgresqlApplication.class, args);
}
@Autowired
private AccountRepository repository;
@Bean
CommandLineRunner runner() {
return args -> Arrays.asList(
new Account(1L,"wonwoo","wonwoo@test.com"),
new Account(2L,"kevin","kevin@test.com"),
new Account(3L,"mink","mink@test.com")
).forEach(repository::save);
}
}
Account class
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {
@Id
@GeneratedValue
private Long id;
private String username;
private String email;
}
AccountController class
@Slf4j
@RestController
public class AccountController {
@Autowired
private AccountRepository repository;
@RequestMapping(value = "/accounts", method = RequestMethod.GET)
public ResponseEntity<?> getAccount() {
List<Account> accounts = repository.findAll();
log.debug("accounts : {} ", accounts);
return new ResponseEntity<>(accounts, HttpStatus.OK);
}
}
AccountRepository class
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
}
따로 설명은 하지 않겠다.
mysql과 다른점은 yml 파일과 maven이다.
application.yml
spring:
jpa:
hibernate:
ddl-auto: create
database-platform: org.hibernate.dialect.PostgreSQLDialect
datasource:
url: jdbc:postgresql://${POSTGRES_PORT_5432_TCP_ADDR}:${POSTGRES_PORT_5432_TCP_PORT}/docker
username: dbuser
password: dbpassword
driver-class-name: org.postgresql.Driver
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc4</version>
</dependency>
일단 postgresq pull 하자
docker run --name spring-boot-postgres -e POSTGRES_PASSWORD=dbpassword -e POSTGRES_DB=docker -d postgres
latest: Pulling from postgres
00b8e8227087: Pull complete
1ad190f65f04: Pull complete
60f2f5fb5bf9: Pull complete
2d0c73368a74: Pull complete
a786535117f1: Pull complete
dd182ad340e4: Pull complete
c05a4dbabb61: Pull complete
d147dcf8581e: Pull complete
358e57fb3884: Pull complete
9c4b8b3535ba: Pull complete
adf658a6b808: Extracting [==================================================>] 41.23 MB/41.23 MB
0fc677feeba3: Download complete
0dceace87c23: Download complete
23b5786d3dce: Download complete
9b00a7295803: Download complete
f88f356d246d: Download complete
d6fb5e6e70f6: Download complete
07e6c1333fa3: Download complete
6f960967c8bf: Download complete
d8bd0657b25f: Already exists
a582cd499e0f: Already exists
위와 같이 이미지를 생성한다.
docker images
이미지가 잘 있나 보자
postgres latest 6f960967c8bf 5 days ago 264.5 MB
잘있다.
다음으로 메이븐 빌드를 하자!
mvn clean package docker:build
빌드가 잘 되었다면
docker를 실행시키자
docker run --name spring-boot-docker-postgresql --link spring-boot-postgres:postgres -p 9000:8080 -d wonwoo/spring-boot-docker-postgresql
로그를 확인하기 위해 아래와 같이 입력하자
docker logs $CONTAINER_ID
잘되었다면 브라우저를 열어 확인해보자
http://localhost:8080/accounts
그리고 postgres 클라이언트 연결도 가능하다.
docker run -it --link spring-boot-postgres:postgres --rm postgres sh -c exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres
위와 같이하면 접속도 가능하다.
postgres=# \\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
docker | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
디비 목록이다.
postgres=# \\connect docker
선택을 해보자
docker=# \\d
테이블을 보자. account 테이블이 잘 있다.
List of relations
Schema | Name | Type | Owner
--------+--------------------+----------+----------
public | account | table | postgres
public | hibernate_sequence | sequence | postgres
(2 rows)
한번씩 해보자!