class Hello { public static void main(String[] args) { System.out.println("Hello, it's great to be running inside docker image"); } }
Create Dockerfile without any extension and populate with below contents
FROM java:8 COPY . /var/www/java WORKDIR /var/www/java RUN javac Hello.java CMD ["java", "Hello"]
Run this command to build the image.
$ docker build -t java-app .
This command will build the image with the name java-app
$ docker run java-app
You should see the console output.
Create a file with name docker-compose.yaml Update its content to this.
version: '2' services: java: build: .
The build parameter informs about the location of Dockerfile. Here . signifies, it’s in the current directory.
docker-compose build
docker-compose up
You should see the same console output again.
We can pass or set environment variables in docker-compose file. Update docker-compose.yaml file with one more detail.
version: '2' services: java: build: . environment: - ENV_TYPE = "test"
Now update the java code to read this environment variable.
class Hello { public static void main(String[] argss){ System.out.println("Hello, it is nice to be running inside docker image"); if(System.getenv("ENV_TYPE").equals("prod")){ System.out.println("Running on prod like env"); } else if (System.getenv("ENV_TYPE").equals("test")){ System.out.println("Running on test like env"); } else { System.out.println("This is something other than prod or test"); } } }
Build the image again and then run it.
$ docker-compose build $ docker-compose up
The console should print the value set in the compose file.
$ docker-compose run -e ENV_TYPE=test java $ docker-compose run -e ENV_TYPE=prod java $ docker-compose run -e ENV_TYPE=stag java
Now, you should see the console output depending upon the environment variable passed.
Quick Links
Legal Stuff