Docker Practice Question

Practice Question Set 1st

Question 1: Download the image “Ubuntu” & launch the container from it in detached mode

Steps to follow:

  1. The name of the container should be “LW”.

  2. Create a directory of the name “DCA” on a base system and attach it to the container.

  3. Run the date command in the container without attaching it.

Solution:

To achieve the described tasks using Docker, you can follow these steps:

  1. Download the Ubuntu image:
docker pull ubuntu
  1. Launch the container from the Ubuntu image in detached mode, naming it "LW":
docker run -d --name LW ubuntu
  1. Create a directory named "DCA" on the base system:
mkdir DCA
  1. Attach the "DCA" directory to the running container:
docker cp DCA LW:/DCA

Replace LW with the actual name of your container if it's different.

  1. Run the date command in the container without attaching to it:
docker exec LW date

Replace LW with the actual name of your container if it's different.

These commands assume that you have Docker installed and running on your system. Make sure to adapt them if needed based on your operating system and Docker setup.

Question 2: Launch the webserver in a container

Steps to follow:

  1. Create a directory of the name “LW-Webserver” on the base system.

  2. Create the webpage name “LW.html” in the “LW.

  3. Webserver” directory.

  4. The webpage contains the data “We Love Docker” .

  5. Launch a container name “WEB” in interactive mode and attach the directory. “LW-Webserver” to the “/var/www/html” directory of the webserver.

  6. Expose port 80 of the webserver to 1234.

  7. Install Apache webserver in a container.

  8. Start the Apache webserver.

  9. Enter the URL of the website and display the “LW.html” webpage.

Solution:

To achieve the described tasks, you can use Docker to create a containerized web server with Apache. Here are the step-by-step commands to follow:

  1. Create the directory and HTML file:

     mkdir LW-Webserver 
     echo "We Love Docker" > LW-Webserver/LW.html
    
  2. Create Dockerfile: Create a file named Dockerfile in the same directory with the following content:

    Dockerfile

     FROM httpd:latest 
     COPY ./LW-Webserver/ /usr/local/apache2/htdocs/
    
  3. Build Docker Image:

     docker build -t custom-webserver .
    
  4. Launch Container:

     docker run -it --name WEB -p 1234:80 custom-webserver
    

    This command launches the container in interactive mode, names it "WEB", maps port 1234 on the host to port 80 on the container, and attaches the "LW-Webserver" directory to the "/usr/local/apache2/htdocs/" directory inside the container.

  5. Access the Webpage: Open your web browser and navigate to http://localhost:1234/LW.html. You should see the "We Love Docker" message.

Now, you have a Docker container running an Apache web server with your custom webpage accessible at http://localhost:1234/LW.html.

Question 3: Launching MySQL database

Steps to follow:

  1. Get MySQL image from the docker hub.

  2. Launch the container with the following parameters

    1. Container should run in detached mode

    2. Name of a container should be “DataBase”

    3. Create a directory on a base system as “My_SQL” and attach it to the container

    4. Container should have the following environmental variables

  3. MYSQL_ROOT_PASSWORD is “redhat”

  4. MYSQL_DATABASE name is “DB”

  5. MYSQL_USER is “SHAKTIMAN”

  6. MYSQL_PASSWORD is “redhat”

Solution:

To launch a MySQL container with the specified parameters, you can use the following commands:

  1. Pull the MySQL image from Docker Hub:

     docker pull mysql:latest
    
  2. Create a directory on the base system and launch the MySQL container: bash

     mkdir My_SQL
        docker run -d --name DataBase -v $(pwd)/My_SQL:/var/lib/mysql \
          -e MYSQL_ROOT_PASSWORD=redhat \
          -e MYSQL_DATABASE=DB \
          -e MYSQL_USER=SHAKTIMAN \
          -e MYSQL_PASSWORD=redhat \
          mysql:latest
    
    • -d: Run the container in detached mode.

    • --name DataBase: Name the container "DataBase."

    • -v $(pwd)/My_SQL:/var/lib/mysql: Mount the "My_SQL" directory on the host to "/var/lib/mysql" inside the container.

  3. Verify that the container is running:

     docker ps
    

Now you have a MySQL container named "DataBase" running with the specified environmental variables and data directory attached to the host.

Question 4: Launching word press application.

Steps to follow:

  1. Get the word press image from Docker Hub.

  2. Launch the container in detached mode.

  3. Name of a container should be “Word_Press”.

  4. Link the “Word_Press” container with the container “DataBase”.

  5. Expose port 80 to 1234.

  6. Go to the word press application with the private IP address.

  7. Connect the MySQL database to the Word Press application.

  8. Create a user with the username “Learner” & password “Docker”.

  9. Create a blog with the title “I love Docker”.

  10. Remove the container “Word_press” & “DataBase”.

  11. Relaunch the containers “My_SQL” and “Word_Press”.

Solution:

Here are the steps to launch a WordPress application, link it to a MySQL database, perform some basic setup, and then remove and relaunch the containers:

  1. Get the WordPress image from Docker Hub:

     docker pull wordpress:latest
    
  2. Launch the MySQL container (if not already running):

     mkdir My_SQL
        docker run -d --name DataBase -v $(pwd)/My_SQL:/var/lib/mysql \
          -e MYSQL_ROOT_PASSWORD=redhat \
          -e MYSQL_DATABASE=DB \
          -e MYSQL_USER=SHAKTIMAN \
          -e MYSQL_PASSWORD=redhat \
          mysql:latest
    
  3. Launch the WordPress container and link it to the MySQL container:

     docker run -d --name Word_Press --link DataBase:mysql -p 1234:80 \
          -e WORDPRESS_DB_HOST=mysql \
          -e WORDPRESS_DB_USER=SHAKTIMAN \
          -e WORDPRESS_DB_PASSWORD=redhat \
          -e WORDPRESS_DB_NAME=DB \
          wordpress:latest
    
    • --link DataBase:mysql: Links the WordPress container with the MySQL container.
  4. Access WordPress application: Open your web browser and navigate to http://localhost:1234 Follow the WordPress setup instructions, and during the setup, connect it to the MySQL database using the provided information.

  5. Create a user, blog, and perform some basic setup:

    • During the WordPress setup, create a user with the username "Learner" and password "Docker."

    • Create a blog with the title "I love Docker."

  6. Remove the containers:

     docker stop Word_Press DataBase 
     docker rm Word_Press DataBase
    
  7. Relaunch the containers:

     docker run -d --name DataBase -v $(pwd)/My_SQL:/var/lib/mysql \
          -e MYSQL_ROOT_PASSWORD=redhat \
          -e MYSQL_DATABASE=DB \
          -e MYSQL_USER=SHAKTIMAN \
          -e MYSQL_PASSWORD=redhat \
          mysql:latest
    
     docker run -d --name Word_Press --link DataBase:mysql -p 1234:80 \
          -e WORDPRESS_DB_HOST=mysql \
          -e WORDPRESS_DB_USER=SHAKTIMAN \
          -e WORDPRESS_DB_PASSWORD=redhat \
          -e WORDPRESS_DB_NAME=DB \
          wordpress:latest
    

Now, your WordPress application should be accessible at http://localhost:1234, and it should show the blog you created earlier.