You must realize that following this tutorial will not get you any closer to understanding the FHIR standard … but it WILL give you your own environment where you can inspect every aspect of FHIR, from issuing REST API calls to examining the data model for storage that HAPI chose to use. This setup provides me total transparency from input to process to output, all with technology I am comfortable with. Your goals may be different, I just hope the guide provides value whatever they may be.
Create Dockerfiles for Tomcat and PostgrSQL.
FROM tomcat:8-jre8
MAINTAINER gmoran
RUN echo "export JAVA_OPTS=\"-Dapp.env=staging\"" > /usr/local/tomcat/bin/setenv.sh
COPY ./hapi-fhir-jpaserver-example.war /usr/local/tomcat/webapps/fhir.war
CMD ["catalina.sh", "run"]
FROM postgres:9.4
MAINTAINER gmoran
ENV POSTGRES_USER gmoran
ENV POSTGRES_PASSWORD XXXXXXXXX
ENV POSTGRES_DB fhirdata
Create a Docker-Compose file to orchestrate and launch the system.
app-web:
build: ./web
ports:
- "8080:8080"
links:
- app-db
app-db:
build: ./db
expose:
- "5432"
ports:
- "5432:5432"
volumes_from:
- app-db-data
app-db-data:
image: cogniteev/echo
command: echo 'Data Container for PostgreSQL'
volumes:
- /var/lib/postgresql/data
Our next chore is to build the HAPI FHIR JPA example server. If you are familiar with Git and Maven, it is easy.
Download HAPI source from GIT.
$ git clone https://github.com/jamesagnew/hapi-fhir.git
Modify the FhirServerConfig.java to wire the database configuration.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-SNAPSHOT</version>
</dependency>
In the source code navigate to the FhirServerConfig.java class. Navigate first to the hapi-fhir-jpaserver-example folder, then the class is nested down here:
src/main/java/ca/uhn/fhir/jpa/demo/FhirServerConfig.javaUsing your favorite code editor, make the changes shown in red as follows:
public DataSource dataSource() {
BasicDataSource retVal = new BasicDataSource();
retVal.setDriver(new org.postgresql.Driver());
retVal.setUrl("jdbc:postgresql://app-db:5432/fhirdata");
retVal.setUsername("gmoran");
retVal.setPassword("XXXXXXXX");
return retVal;
}
@Override
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal =
super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
retVal.setDataSource(dataSource());
retVal.setJpaProperties(jpaProperties());
return retVal;
}
private Properties jpaProperties() {
Properties extraProperties = new Properties();
extraProperties.put("hibernate.dialect",
org.hibernate.dialect.PostgreSQL94Dialect.class.getName());
Note what we changed.
The driver class must be the PostgreSQL driver class (org.postgresql.Driver()).
The URL is the standard JDBC URL for connecting to a database, comprising the protocol, the host, the port number and the name of the database. The host is an interesting value: app-db. If you look back at our docker-compose.yml file, you will see that we named our container app-db, and therefore, we can reference that name as the hostname for the database. The rest of the values in the URL MUST match the values we set in the docker-compose and Dockerfile configurations.
host: app-db
port: 5432
database: fhirdata
username: gmoran
password: XXXXXXXX
Also note that we changed the hibernate dialect (org.hibernate.dialect.PostgreSQL94Dialect.class.getName()). If for some reason you change the version of PostgreSQL used in the Dockerfile, you will want to make sure this dialect class matches the version you chose.
Once you are satisfied your changes match the configuration, save this file. We will now return to the command line to build the server using Maven.
Build the HAPI FHIR JPA Server example .war file.
Return to a command line, and navigate in the HAPI source code to the hapi-fhir-jpaserver-example folder. Run the following command from that location:
$ mvn install
Locate the target folder in the hapi-fhir-jpaserver-example folder. There should be a hapi-fhir-jpaserver-example.war file created.
Copy the hapi-fhir-jpaserver-example.war file into the /web subfolder you created or downloaded with the Dockerfiles.
Spin up an AWS EC2 instance (I used Ubuntu free tier).
Be certain to allow access to SSH and port 8080 (or whatever port you may have used for the web application server) in your AWS Security Group for your server. Allow access to port 5432 as well, if you want to use psql or another database management utility with your PostgrSQL server. You will be given the opportunity to set this access in the EC2 Launch Wizard.
Install Docker & Docker-Compose on your EC2 instance.
The Docker guides have great instructions on how to install Docker and Docker-Compose on Ubuntu.
Note that you will want to add the Ubuntu user to the Docker group on your EC2 server so you aren't having to sudo all the time.
Move your Dockerfiles, Docker-Compose script and .war file to your EC2 instance.
$ tar cvzf web.tar.gz
scp -i xxx.pem web.tar.gz ubuntu@my_aws_ec2.compute-1.amazonaws.com:/home/ubuntuThe xxx.pem file in the command above should be the .pem file that you saved from AWS when you created your EC2 instance. The hostname should match the hostname of your EC2 server instance.
Once the files are uploaded to your EC2 instance, SSH into the EC2 server once again. Find the folder that holds your web.tar.gz file. If you followed along exactly as I did it, the file should be in the /home/ubuntu folder.
Use the tar utility once again to extract your files:
$ tar xvf web.tar.gzRun Docker-Compose to launch your containers.
$ docker-compose upNow, with luck, you should have the HAPI FHIR example server up and running. You can test talking to your server by navigating to it in your browser:
http://my_aws_ec2.compute-1.amazonaws.com:8080/fhir/




