The goal of this project is to use Docker Compose to wrap a Java application container and MySQL container. The Java application is a CRUD app built with Dropwizard using jdbi3 mapper to MySQL.
After cloning, build the jar
file.
$ ./gradlew clean build
Using a custom task called copyApp
, copy the jar
and config.yml
file to the directory with the Dockerfile
.
$ ./gradlew copyApp
Move to the directory with the docker-compose.yml
file.
$ cd docker/compose
Run Docker Compose.
$ docker-compose up
Confirm the running containers with $ docker ps
At this time, the database table does not exist. Open a separate terminal and bash into the running MySQL container.
$ docker exec -t -i location-services-mysql /bin/bash
root@2fbfb34dd83b:/#
Run MySQL client using the credentials from the docker-compose
file:
root@2fbfb34dd83b:/# mysql -u <useranme> -p
Create tables and add necessary data.
mysql> use locations;
mysql> show tables;
Empty set (0.01 sec)
TODO: I'm working on the docker-entrypoint-initdb.d
file for database table creation.
Visit resource path localhost:8080/api/v1/locations
Example POST request for creating data using postman.
Sample json content for @POST to http://localhost:8080/api/v1/locations/
{
"id": null,
"createdOn": "1414602645009",
"modifiedOn": "1414602645009",
"country": "US",
"state": "COLORADO",
"type": "Ups",
"code": "232121122222",
"name": "it's cold"
}
In the config.yml
file, make sure the JDBC url reflects the local MySQL datasource.
# the JDBC URL
url: jdbc:mysql://localhost:3306/locations?serverTimezone=UTC
Then, build the jar.
$ ./gradlew clean build
Run the application.
$ ./gradlew run
Visit the resource path localhost:8080/api/v1/locations
after adding data via MySQL or by using Postman.
This is the process of creating the initial image without Compose.
Create the jar file.
$ ./gradlew clean build
Copy the jar
and config.yml
file to /location-services/docker/location-services
with our custom task.
$ ./gradlew copyApp
Move to the directory with the Dockerfile and our recently copied jar
and config.yml
file.
$ cd location-services/docker/location-services
Build the image.
$ docker build -t location-services .
The docker create
command creates a writeable container layer over the specified image and prepares it for running
the specified command. The container ID is then printed to STDOUT
. This is similar to docker run -d
except the
container is never started. You can then use the docker start <container_id>
command to start the container at any
point. This is useful when you want to set up a container configuration ahead of time so that it is ready to start
when you need it. The initial status of the new container is created
.
$ docker create --name=location-services-container -p 8080:8080 -p 8081:8081 location-services
List all containers running and stopped.
$ docker ps -a
Start the container.
$ docker start location-services-container
List all running containers.
$ docker ps
View logs
$ docker logs -f location-services-container
Allow you to tail logs
$ docker logs -ft <container-id>
Shut down
$ docker stop location-services-container
Remove the container
$ docker rm location-services-container
Remove the image
$ docker rmi <image-id>
Remove all images if necessary
$ docker rmi $(docker images -a)