Skip to content

Commit

Permalink
Merge pull request #247 from stakater/malformed-docs
Browse files Browse the repository at this point in the history
Fix indendation
  • Loading branch information
rasheedamir authored May 27, 2024
2 parents efaf1f9 + 4e17ff3 commit b8394e6
Showing 1 changed file with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,102 +24,102 @@ Lets create a Dockerfile inside the repository folder and delete any existing fi

1. Decide a base image for your application. Navigate to [RedHat Container Registry](https://catalog.redhat.com/software/containers/search) and Find a suitable image for your application. Since this application is java application. We use `maven` base image.

```Dockerfile
FROM maven:3.8.6-openjdk-11-slim AS build
```
```Dockerfile
FROM maven:3.8.6-openjdk-11-slim AS build
```

1. Use COPY and RUN commands to copy required content to containers filesystem and run commands in containers context.

```Dockerfile
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
```
```Dockerfile
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
```

1. We will use another FROM statement to create a multi-stage build for reducing the overall image size. More info [here](https://docs.docker.com/build/building/multi-stage/). With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.
```Dockerfile
FROM registry.access.redhat.com/ubi8/openjdk-11:1.14-10
```
```Dockerfile
FROM registry.access.redhat.com/ubi8/openjdk-11:1.14-10
```
1. Add labels to your image, if any.
```Dockerfile
LABEL name="inventory" \
maintainer="Stakater <hello@stakater. com>" \
vendor="Stakater" \
release="1" \
summary="Java Spring boot application"
```
```Dockerfile
LABEL name="inventory" \
maintainer="Stakater <hello@stakater. com>" \
vendor="Stakater" \
release="1" \
summary="Java Spring boot application"
```
1. Set an environment variable with `ENV` command and set it as working directory.
```Dockerfile
ENV HOME=/opt/app
WORKDIR $HOME
```
```Dockerfile
ENV HOME=/opt/app
WORKDIR $HOME
```
1. Use EXPOSE command to expose a container port, typically this corresponds to port on which application runs.
```Dockerfile
EXPOSE 8080
```
```Dockerfile
EXPOSE 8080
```
1. JAR files were generated as a result of `mvn package`. Copy the artifact generated from build stage.
```Dockerfile
COPY --from=build /usr/src/app/target/*.jar $HOME/artifacts/app.jar
```
```Dockerfile
COPY --from=build /usr/src/app/target/*.jar $HOME/artifacts/app.jar
```
1. Define user.
```Dockerfile
USER 1001
```
```Dockerfile
USER 1001
```
1. Set the Entrypoint
```Dockerfile
ENTRYPOINT exec java $JAVA_OPTS -jar artifacts/app.jar
```
```Dockerfile
ENTRYPOINT exec java $JAVA_OPTS -jar artifacts/app.jar
```
1. Finally, specify the command to be executed when container is created with this image, typically the command to run the application.
```Dockerfile
CMD ["java", "-jar", "artifacts/app.jar"]
```
```Dockerfile
CMD ["java", "-jar", "artifacts/app.jar"]
```
1. Run the following command to build the image.
```sh
docker build -t <app-name>:1.0.0 .
```
```sh
docker build -t <app-name>:1.0.0 .
```
1. Execute the following command to run the image.
!!! note
To run the application container you need Mongo_DB server running.
!!! note
To run the application container you need Mongo_DB server running.
```sh
# -p flag exposes container port 8080 on your local port 8080
# --env flag allows Mongo_DB necessary environment variables; e.g. MONGO_HOST, MONGO_DB_PASS
docker run -dt -p [<localhost-port>:<container-port>] --env <variable1>=<value> --env <variable2>=<value> <image-name>:1.0.0
```
```sh
# -p flag exposes container port 8080 on your local port 8080
# --env flag allows Mongo_DB necessary environment variables; e.g. MONGO_HOST, MONGO_DB_PASS
docker run -dt -p [<localhost-port>:<container-port>] --env <variable1>=<value> --env <variable2>=<value> <image-name>:1.0.0
```
!!! note
If Mongo_DB server is running on your local machine, replace **-p** flag and it's values with --network="host".
!!! note
If Mongo_DB server is running on your local machine, replace **-p** flag and it's values with --network="host".

```sh
docker run -dt --network="host" --env <variable1>=<value> --env <variable2>=<value> <image-name>:1.0.0
```
```

1. Run a curl command to verify that image is running.

```sh
curl localhost:8080/api/review/329199
```
```sh
curl localhost:8080/api/review/329199
```

![curl output](images/local-output.png)
![curl output](images/local-output.png)

Read the following articles for more information:

Expand Down

0 comments on commit b8394e6

Please sign in to comment.