0% found this document useful (0 votes)
2 views

Lab 7 Simple Container

Uploaded by

shireeshasri2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 7 Simple Container

Uploaded by

shireeshasri2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 7

Develop a simple containerized application using Docker

Here, we are creating a Java application and running by using the docker. This example
includes the following steps.

1. Create a directory

Directory is required to organize files. Create a director by using the following


command.

1. mkdir java-docker-app
2. Create a Java File

Now create a Java file. Save this file as Hello.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.

See, your folder inside must look like the below.

4. Build Docker Image

After creating Dockerfile, we are changing working 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.

1. docker build -t java-app .


See, the screen shot of the above command.
Run Docker Image

After creating image successfully. Now we can run docker by using run command. The
following command is used to run java-app.

docker run java-app

If you want to name the container run the command as

Docker run –myfirst container java-app.

You can check the name and list of containers in docker desktop

You might also like