Mastering Docker: A Guide to Common Practical Challenges🤔💡

@Harsh
6 min readJan 6, 2024

--

Introduction:

Welcome, fellow tech enthusiasts and aspiring DevOps engineers! In this comprehensive blog, we embark on a journey to unravel the mysteries of Docker, a powerful containerization platform revolutionizing the way we develop, ship, and run applications.

we’ve recently received a set of practice questions, now as we dive into each question, we’ll unravel the intricacies of Docker, providing clear and concise solutions to common challenges.

So, grab your favorite beverage, set up your development environment, and let’s navigate through the fascinating world of Docker, one question at a time.

This blog aims to be your go-to resource for mastering Docker fundamentals. Let’s get started!

QUESTION 1:

Download the image “Ubuntu” & launch the container from it in detached mode The name of the container should be “LW” .

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

Run the date command in the container without attaching it

SOLUTION :

  1. Downloading the images,

2. Launching the container in detached mode by attaching the base directory name DCA and running date command without attaching it.

Exec command in the above picture is used to run commands in docker without attaching to it.

QUESTION 2:

Launch the webserver in a container ď‚·

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

Create the webpage name “LW.html” in the “LW-Webserver” directory

The webpage contains the data “We Love Docker”

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

Expose port 80 of the webserver to 1234ď‚·

Install Apache webserver in a container

Start the Apache webserverď‚·

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

SOLUTION :

→ Creating the directory LW-Webserver on base system and creating the LW.html page inside the directory and put the content inside page.

→ Launching a container name “WEB” and attaching the LW-Webserver directory with main root directory of apache web server inside container, exposing it and installing apache server.

[root@ip-172-31-32-150 ~]# docker run -it --name WEB -v /root/LW-Webserver:/var/www/html/ -p 1234:80 centos:7 
[root@9125bd9ed82d /]# yum install httpd -y

You can check inside container that the file you have created in Base directory is present inside the document root.

→ Next step is to start the apache services and show the output.

QUESTION 3:

Launch the container from the image “centos:7” and name it “Superman”.

The container should run in interactive mode ď‚·

Come out of the container without stopping it ď‚·

Get the IP address of the container without attaching it ď‚·

Stop the container “Superman” 

Remove the container “Superman”

SOLUTION:

→ Launching the container in interactive mode and coming out of it without stopping it.

In the above step we have launched the container in interactive mode and come out of by pressing ctrl + p + q together. This will take you out of the container without stopping it.

→ Finding out the IP of the container without attaching to it.

The inspect command give the entire information about the Container that you have specified in the command. And the grep command will get you the all the lines that have the word IPAddress.

We have write the inspect command and provide this command as the input to the grep command with the help of pipe symbol. The pipe takes output from one command and uses it as input for another.

→ Stopping and removing the container:

We can verify also with the same grep command that the container is permanently deleted.

QUESTION 4:

Launching MySQL database ď‚·

Get MySQL image from the docker hub ď‚·

Launch the container with the following parameters :

o Container should run in detached mode

o Name of a container should be “DataBase”

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

o Container should have the following environmental variables : 

MYSQL_ROOT_PASSWORD is “redhat” 

MYSQL_DATABASE name is “DB”

MYSQL_USER is “SHAKTIMAN”

MYSQL_PASSWORD is “redhat”

SOLUTION :

→ Getting the mysql image from docker hub;

→ Creating the base directory as My_SQL and run the container with the following parameters;

Command :

[root@ip-172-31-32-150 ~]# docker run -dit --name DataBase -v /root/My_SQL:/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD=redhat \
> -e MYSQL_DATABASE=DB \
> -e MYSQL_USER=SHAKTIMAN \
> -e MYSQL_PASSWORD=redhat mysql

QUESTION 5;

Launching word press application ď‚·

Get the word press image from Docker Hub ď‚·

Launch the container in detached mode ď‚·

Name of a container should be “Word_Press”

Link the “Word_Press”container with the container “DataBase”

Expose port 80 to 1234ď‚·

Go to the word press application with the private IP address ď‚·

Connect the MySQL database to the Word Press applicationď‚·

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

Create a blog with the title “I love Docker “

Remove the container “Word_press” & “DataBase” 

Relaunch the containers “My_SQL” and “Word_Press”. Word press application should show the blog which was created

SOLUTION:

→ Geting the image “wordpress” from the hub;

→ Launching the container from the wordpress images with the given parameters;

[root@ip-172-31-32-150 ~]# docker run -dit --name Word_Press --link DataBase -p 1234:80 wordpress 

→ Going to the word press application with the private IP address and Connecting the MySQL database to the Word Press application

→ Creating a user with the username “Learner” & password “Docker”

→ Creating a blog with the title “I love Docker “

Remove the container “Word_press” & “DataBase” Relaunch the containers “My_SQL” and “Word_Press”.

*Relaunching them;

Now we can see that our user credentials are same;

And now we can see that our previous post is there in the wordpress;

Conclusion

By now, you’ve tackled a range of questions, from basic container creation to more complex networking setups. The world of Docker is vast, and your newfound knowledge is a solid foundation for future projects and challenges.

As we wrap up this Docker journey, remember that mastering containerization is an ongoing process. The landscape is dynamic, and Docker continues to evolve. Stay curious, explore advanced features, and always be on the lookout for new best practices.

Thank you for joining us on this Docker adventure! If you have additional questions or topics you’d like to explore, drop them in the comments. Happy containerizing!

--

--