Lab 7 Simple Container
Lab 7 Simple Container
Here, we are creating a Java application and running by using the docker. This example
includes the following steps.
1. Create a directory
1. mkdir java-docker-app
2. Create a Java File
// Hello.java
1. class Hello{
2. public static void main(String[] args){
3. System.out.println("This is java app \n by using Docker");
4. }
5. }
Save it inside the directory java-docker-app as Hello.java.
3. Create a Dockerfile
After creating a Java file, we need to create a Dockerfile which contains instructions
for the Docker. Dockerfile does not contain any file extension. So, save it simple
with Dockerfile name.
// Dockerfile
1. FROM openjdk
2. COPY . /var/www/java
3. WORKDIR /var/www/java
4. RUN javac Hello.java
5. CMD ["java", "Hello"]
FROM : this specifies the base image to use for docker image.
COPY . : this copies files from local system to docker image.
WORKDIR :this sets the working directory for subsequent commands in the docker file
RUN:this runs a command inside the docker container during build process.
CMD : this specifies the command to run when the docker container starts.
Write all instructions in uppercase because it is convention. Put this file inside java-
docker-app directory. Now we have Dockerfile parallel to Hello.java inside the java-
docker-app directory.
1. cd java-docker-app
See, the screen shot.
Now, create an image by following the below command. we must login as root in order
to create an image. In this example, we have switched to as a root user. In the following
command, java-app is name of the image. We can have any name for our docker
image.
After creating image successfully. Now we can run docker by using run command. The
following command is used to run java-app.
You can check the name and list of containers in docker desktop