Complete Docker and Container work flow for learners.


Docker:
First, we need a machine to install docker tool/software, so we will launch either vm instance from GCP or an instance from AWS.
- in my case VM Instance from GCP.
Go to SSH support tolls, like Mobaxterm or Putty to connect with created Instance.
- In my case, I am using Mobaxterm,
To connect VM Instance from Mobax, go here https://sudharsanreddy.hashnode.dev/simple-steps-for-connecting-to-google-cloud-vm-instance-through-mobaxterm
- Till to here, we launch an instance and connected with Mobax.
To perform docker activities we need docker software in our machine. So, we install docker by using the bellow commands
sudo apt install docker.io
to confirm docker installation, we use "docker info or docker -v" command in the command line.
till here, Instance created, connection through Mobax completed and docker application installation completed.
Here, we check few confirmations by using the below commands.
docker images --> display images
docker ps --> display processes
docker ps -a --> display running processes
There is no images and running containers/processes.
To run a container, we have two options.
we can pull the image from dockerhub
we can create the Dockerfile and build the Dockerfile for image creation and we can run the Dockerfile for container.
6.1.1 We will go for first step. We pull the image from Dockerhub and run
For this we can use the command "docker pull " - In my case nginx is my image.
6.1.2 Now, we run the nginx image, so it will convert from image to container.
--> docker run -d --name my-nginx-web-server -p 80:80 nginx
--> till here, we pull the image, run the image and up. To confirm this, open web browser and hit http://<external-ip>:80
--> nginx default html page will display.
6.1.3 Now, we made changes with custom html file, so we will get custom page will open nginx server.
--> for this, we move to running docker container.
--> docker exec -it /bin/bash
6.1.4 Now we are in inside container.
--> We will cross check index.html path in /usr/share/nginx/html/index.html
--> exit from container.
6.1.5 Create a custom html file in your local directory.
--> In my case myindex.html is my custom index.html file.
--> copy this html to inside container /usr/share/nginx/html/index.html
6.1.6 docker cp myindex.html :/usr/share/nginx/html/index.html
6.1.7 Now refresh the nginx web browser, you will get custom index file content.
6.1.8 Till to here, we pull the nginx image, we run nginx image, we changed default nginx index file.
6.2 Now, we will do the same with Dockerfile.
6.2.1 First, we create Dockerfile.
--> Here we have already downloaded nginx image. So, I am using same image.
--> So, we will run few commands for this work.
--> docker images -> copy the docker image id for reference.
--> mkdir my-directory-image
--> cd my-directory-image
-->Create a Dockerfile.
--> Add any additional instructions you need to the Dockerfile. For example, you might want to install additional software or copy files into the image.
-->Build the new image from Dockerfile
-docker build -t my-docker-image .
--> Run the new image to test it:
docker run -d --name my-nginx-container -p 80:80 my-docker-image
--> Open browser, and hit url http://<external-ip>:80
--> Now, we change index.html file with custom html file.
--> Create custom html file. here my file name myindex.html
--> copy the custom html file to /usr/share/nginx/html/index.html
--> docker cp myindex.html <container-id>:/usr/share/nginx/html/index.html
--> Now, open the browser hit http://<external-ip>:80
The output of nginx with custom index.html file content.
This is an overall theory for Docker.
Subscribe to my newsletter
Read articles from Sudharsan Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
