Docker Practice Question
Practice Question Set 2nd
Question 1: Create an index.html page on the base system.
Steps to Follow:
The index.html file contains the data “I am learning docker”.
Extend the image ubuntu.
Run the command “apt-get update”.
Install “Nginx with apt-get command”.
Copy index.html to /var/www/html directory.
The image should expose its port 80.
The build time command for Nginx is “nginx” “-g” & “daemon off”.
The name of the image should be nginx:0.0.1.
Build the image & launch the container Command to test :- curl http://(IP-Address):(port-NO )/index.html.
Test command should display “I am learning docker".
Solution:
- Simple Dockerfile that you can use to achieve the specified requirements:
Dockerfile
# Use the base image
FROM ubuntu
# Set the working directory
WORKDIR /app
# Create and write data to index.html
RUN echo "I am learning docker" > index.html
# Extend the image by updating and installing Nginx
RUN apt-get update && \
apt-get install -y nginx
# Copy index.html to Nginx default directory
COPY index.html /var/www/html/
# Expose port 80
EXPOSE 80
# Build time command for Nginx
CMD ["nginx", "-g", "daemon off"]
# Set the name and tag for the image
FROM nginx:0.0.1
# Build the image and launch the container
docker build -t nginx:0.0.1 .
docker run -p 8080:80 nginx:0.0.1
Save this as "Dockerfile"
in a directory alongside your "index.html"
file.
- To build the image and run the container:
docker build -t nginx:0.0.1 .
docker run -p 8080:80 nginx:0.0.1
- After running the container, you can test it using the specified command:
bash curl http://localhost:8080/index.html
- This should display:
I am learning docker
Question 2: Create an image with the following instructions
Steps to Follow:
Extend the image centos:7
Give maintainer detail as your name
Install Apache webserver
Create a webpage of name website.html having data “This website is running in Container” at the time of building the image
Expose port 80 Set environmental variable NAME with value Docker
Start the Apache web server at the build time with entry point
Command to test :- curl http://(IP-Address):(port-NO )/index.html
Test command should display “This website is running in Container"
Solution:
- Create a Dockerfile:
Dockerfile
# Extend the base image
FROM centos:7
# Set maintainer details
MAINTAINER Your Name
# Install Apache webserver
RUN yum install -y httpd
# Create webpage and add content
RUN echo "This website is running in Container" > /var/www/html/website.html
# Expose port 80
EXPOSE 80
# Set environmental variable
ENV NAME Docker
# Start Apache web server at build time with entry point
ENTRYPOINT ["httpd", "-D", "FOREGROUND"]
Save this as "Dockerfile"
in a directory.
- To build the image and run the container:
docker build -t my-apache-image .
docker run -p 8080:80 my-apache-image
- After running the container, you can test it using the specified command:
curl http://localhost:8080/website.html
- This should display:
This website is running in Container
Question 3:
Steps to Follow:
- Create the file name “Hello.java” with the following content
class Hello{
public static void main(String[] args){
System.out.println("This is java app \n by using Docker"); }
Use base image openjdk:11
Copy Hello.java to /usr/src/myapp directory
Set workdir as /usr/src/myapp
RUN the commands “javac” & “Hello.java”
Build time commands are “java” & “Hello”
Build the image & launch the container from it
The container should give the output
"This is java app by using Docker"
Solution:
Create a Dockerfile
Dockerfile
# Use the base image FROM openjdk:11 # Create a directory for the app RUN mkdir -p /usr/src/myapp # Set the working directory WORKDIR /usr/src/myapp # Copy Hello.java to /usr/src/myapp COPY Hello.java . # Run the commands to compile and execute the Java program RUN javac Hello.java CMD ["java", "Hello"]
Save this as in
"Dockerfile"
a directory alongside your"
Hello.java
"
file.To build the image and run the container:
docker build -t my-java-app .
docker run my-java-app
- After running the container, it should give the output:
This is java app
by using Docker
Question 4: Create the image with the following parameter
Steps to Follow:
- Create 2 files in the home directory
string1 = input("Enter string 1" )
string2 = input ("Enter string 2 ")
joined_string = string1 + string2 print(joined_string)
str = input ("Enter string ")
#reverse string using slicing
reversed = str[::-1]
#print reversed string
print(reversed)
Extend the image centos:7
Install python3
Create a “My-python” directory in / drive
The working directory is /My-python/ Copy join.py & rev.py in the “My-python” directory
The entry point is python3
Tag the image with the name my-python:007
Push the image in the docker hub.
Solution:
- Create a Dockerfile:
Dockerfile
# Extend the base image
FROM centos:7
# Install Python3
RUN yum install -y python3
# Create My-python directory
RUN mkdir /My-python
# Set the working directory
WORKDIR /My-python
# Copy join.py and rev.py to My-python directory
COPY join.py rev.py ./
# Set entry point
ENTRYPOINT ["python3"]
# Tag the image
IMAGE my-python:007
# Push the image to Docker Hub
# Assuming you have logged in to Docker Hub using 'docker login'
docker build -t my-python:007 .
docker tag my-python:007 <your-dockerhub-username>/my-python:007
docker push <your-dockerhub-username>/my-python:007