Docker Practice Question

Practice Question Set 2nd

Question 1: Create an index.html page on the base system.

Steps to Follow:

  1. The index.html file contains the data “I am learning docker”.

  2. Extend the image ubuntu.

  3. Run the command “apt-get update”.

  4. Install “Nginx with apt-get command”.

  5. Copy index.html to /var/www/html directory.

  6. The image should expose its port 80.

  7. The build time command for Nginx is “nginx” “-g” & “daemon off”.

  8. The name of the image should be nginx:0.0.1.

  9. Build the image & launch the container Command to test :- curl http://(IP-Address):(port-NO )/index.html.

  10. Test command should display “I am learning docker".

Solution:

  1. 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.

  1. To build the image and run the container:
docker build -t nginx:0.0.1 . 
docker run -p 8080:80 nginx:0.0.1
  1. After running the container, you can test it using the specified command:
bash curl http://localhost:8080/index.html
  1. This should display:
I am learning docker

Question 2: Create an image with the following instructions

Steps to Follow:

  1. Extend the image centos:7

  2. Give maintainer detail as your name

  3. Install Apache webserver

  4. Create a webpage of name website.html having data “This website is running in Container” at the time of building the image

  5. Expose port 80 Set environmental variable NAME with value Docker

  6. Start the Apache web server at the build time with entry point

  7. Command to test :- curl http://(IP-Address):(port-NO )/index.html

  8. Test command should display “This website is running in Container"

Solution:

  1. 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.

  1. To build the image and run the container:
docker build -t my-apache-image .
docker run -p 8080:80 my-apache-image
  1. After running the container, you can test it using the specified command:
curl http://localhost:8080/website.html
  1. This should display:
This website is running in Container

Question 3:

Steps to Follow:

  1. 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"); }
  1. Use base image openjdk:11

  2. Copy Hello.java to /usr/src/myapp directory

  3. Set workdir as /usr/src/myapp

  4. RUN the commands “javac” & “Hello.java

  5. Build time commands are “java” & “Hello”

  6. Build the image & launch the container from it

  7. The container should give the output

    "This is java app by using Docker"

Solution:

  1. 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.

  2. To build the image and run the container:

docker build -t my-java-app .
docker run my-java-app
  1. 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:

  1. Create 2 files in the home directory
  1. Join.py

string1 = input("Enter string 1" )

string2 = input ("Enter string 2 ")

joined_string = string1 + string2 print(joined_string)

  1. rev.py

str = input ("Enter string ")

#reverse string using slicing

reversed = str[::-1]

#print reversed string

print(reversed)

  1. Extend the image centos:7

  2. Install python3

  3. Create a “My-python” directory in / drive

  4. The working directory is /My-python/ Copy join.py & rev.py in the “My-python” directory

  5. The entry point is python3

  6. Tag the image with the name my-python:007

  7. Push the image in the docker hub.

Solution:

  1. 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