Complete Guide to Installing and Using Docker: A Practical Approach

┬╖

4 min read

Complete Guide to Installing and Using Docker: A Practical Approach

How to install Docker ?

Install Docker on Ubuntu : PROD
A. Set up the repository
  1. Update the apt package index and install packages to allow apt to use a repository over HTTPS
sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  1. Add DockerтАЩs official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Use the following command to set up the repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
B. Install Docker Engine
  • Update the apt package index, and install the latest version of Docker Engine, containerd, and Docker Compose
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
C. Verify that Docker Engine is installed correctly by running the hello-world image
sudo service docker start
sudo docker run hello-world (optional)
to verify the docker version use docker version CMD
for socket error give the executable permission sudo chmod 777 /var/run/docker.sock

Screenshot 2022-10-08 at 4 20 55 PM

Install Docker on Ubuntu With Single Line Command : POC

sudo apt install docker.io # version 20.10.12-0ubuntu2~20.04.1


Docker Practical 1: Pull Official Image From Docker Hub & Run on Your VM

Screenshot 2023-05-21 at 11 50 43 AM

  1. Using docker, pull the latest version of nginx
docker pull nginx

  1. Verify that the image was pulled successfully
docker images
  1. Run the container using the nginx image
docker run --name nginx -p 8080:80 -d nginx

docker run > рдирд╡реАрди рдХрдВрдЯреЗрдирд░ рдЪрд╛рд▓рд╡рдгреНрдпрд╛рд╕рд╛рдареА.
--name nginx > рдХрдВрдЯреЗрдирд░рдЪреЗ рдирд╛рд╡ nginx рдареЗрд╡рдгреНрдпрд╛рд╕рд╛рдареА.
-p 8080:80 > рд▓реЛрдХрд▓ 8080 рдкреЛрд░реНрдЯ рдХрдВрдЯреЗрдирд░рдЪреНрдпрд╛ 80 рдкреЛрд░реНрдЯрд▓рд╛ рдЬреЛрдбрдгреНрдпрд╛рд╕рд╛рдареА.
-d nginx > nginx рдкреНрд░рддрд┐рдорд╛ рдмреЕрдХрдЧреНрд░рд╛рдЙрдВрдбрдордзреНрдпреЗ (рдбрд┐рдЯреЕрдЪ рдореЛрдб) рдЪрд╛рд▓рд╡рдгреНрдпрд╛рд╕рд╛рдареА.
рддрд░ рд╕рдВрдкреВрд░реНрдг рдЖрджреЗрд╢ рд╣реЛрдИрд▓:

"docker run --name nginx -p 8080:80 -d nginx"
  1. Check the status of the Running container
docker ps

Now we have Checking our Server by using our public IP (We have did Some changes in Security group and use IP with 8080 port <54.167.224.71:8080\>)

  1. Verify connectivity to the nginx container through UI
< PUBLIC_IP_ADDRESS OF VM >:8080
  1. access your docker container
docker exec -it < container name > /bin/sh
  1. Stop the container
docker stop < container name >
  1. To Verify Stop Container Status & the container has been removed
docker ps -a
  1. Remove Images
docker rmi < image-id >

How to enter in side Continear :-

docker exec -it nginx /bin/sh

рд╕реНрдкрд╖реНрдЯреАрдХрд░рдг:

  • docker exec - рдЪрд╛рд▓реВ рдЕрд╕рд▓реЗрд▓реНрдпрд╛ рдХрдВрдЯреЗрдирд░рдордзреНрдпреЗ рдХрдорд╛рдВрдб рдЪрд╛рд▓рд╡рдгреНрдпрд╛рд╕рд╛рдареА.

  • -it - рдЗрдВрдЯрд░рдПрдХреНрдЯрд┐рд╡реНрд╣ рдореЛрдбрд╕рд╛рдареА рдЖрдгрд┐ рдЯрд░реНрдорд┐рдирд▓рд╢реА рдЬреЛрдбрдгреНрдпрд╛рд╕рд╛рдареА.

  • nginx - рдХрдВрдЯреЗрдирд░рдЪреЗ рдирд╛рд╡ рдЬреНрдпрд╛рдд рдХрдорд╛рдВрдб рдЪрд╛рд▓рд╡рд╛рдпрдЪреЗ рдЖрд╣реЗ.

  • /bin/sh - рдХрдВрдЯреЗрдирд░рдЪреНрдпрд╛ рд╢реЗрд▓рдордзреНрдпреЗ рдкреНрд░рд╡реЗрд╢ рдХрд░рдгреНрдпрд╛рд╕рд╛рдареА.

Docker Practical 2] :

a) create your own docker image
b) run as container
c) push to your Docker hub Registry

  • Sign in to Docker Hub : & create account Docker Hub

  • Run & Stop the Container

docker run --name nginx -p 8080:80 -d nginx
  • exec the container and see the index.html file in given path /usr/share/nginx/html
docker ps
docker stop nginx
  • create one folder and create Dockerfile and custom index file

  • build custom docker image and run the container

docker build -t <dockerhub_id>/nginx_doc:v1 .

(docker build -t pratiklahamge/test:v1 .)

  • verify the image
docker images
  • run the container
docker run --name new_nginx -p 8081:80 -d <dockerhub_id>/nginx_doc:v1\
  • Login To Dockerhub
docker login

provide USERNAME and PASSWORD

  • push the image to dockerhub
docker push <dockerhub_id>/nginx_doc:v1-new
(docker push pratiklahamge/test:v1)

To RUN static website in a container
docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site

if you want to specify perticular port

docker run --name static-site-2 -e AUTHOR="Your Name" -d -p 8888:80 dockersamples/static-site
┬а