Last active
August 3, 2021 17:06
-
-
Save Yatharth0045/25894b23784ce5db726443e66d233f61 to your computer and use it in GitHub Desktop.
This gist gives a quickstart over docker and helps in understanding the whole container lifecycle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Check docker server and client version | |
docker version | |
## Check for running containers | Currently, it should be empty | |
docker ps | |
## Check for images | Currently, it should be empty | |
docker images | |
## Now Pull an Image (Nginx) from DockerHub | |
## SYNTAX: docker pull <image-name> | |
docker pull nginx | |
## Check for the image again | |
docker images | |
## Run the image to get the container | |
## FYI, nginx is a webserver use to host webpages | |
## SYNTAX: docker run [OPTIONS...] <image-name> | |
docker run --name my-nginx -d nginx | |
## --name: To give a name to the container | |
## -d: Run the container in background (datach mode) | |
## Verify if container is running | |
docker ps | |
## Get logs for the container | |
## SYNTAX: docker logs <container-name/id> | |
docker logs my-nginx | |
## Executing a command within the container | Checking the default nginx configuration | |
## SYNTAX: docker exec <container-name/id> <cmd-to-be-executed> | |
docker exec my-nginx cat /etc/nginx/conf.d/default.conf | |
## Get various container information | |
## SYNTAX: docker inspect <container-name/id> | |
docker inspect my-nginx | |
## Get the default webpage of nginx container | |
## Find the container IP and open it over browser | |
docker inspect my-nginx -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}' | |
## -f: Used to define the Format in which we want (get value against particular key) | |
## Let's try to stop the running container | |
## SYNTAX: docker stop <container-name/id> | |
docker stop my-nginx | |
## Check if container has stopped | |
docker ps -a | |
## -a: Show all containers (Running and Stopped) | |
## Start the container again | |
## SYNTAX: docker start <container-name/id> | |
docker start my-nginx | |
## Verify if container has started | |
docker ps | |
## Show only Running containers | |
## Edit the default webpage of Nginx to some HTML page of our own by modifying the container | |
## Create your own webpage using Heredoc syntax | |
cat > index.html << EOF | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Welcome to nginx!</title> | |
<style> | |
body { | |
width: 35em; | |
margin: 0 auto; | |
font-family: Tahoma, Verdana, Arial, sans-serif; | |
} | |
</style> | |
<body style="background-color:powderblue;"> | |
<center> | |
<h1>This is Docker Quickstart</h1> | |
<h3>This tutorial is provided by <h2>Yatharth | DevOps Engineer at Knoldus</h2></h3> | |
<p> <b>Like and share</b> the blog if you found it helpful </p> | |
<p> For more blogs from this Author, <a href="https://blog.knoldus.com/author/yatharth45/" target="_blank" >Click here</a></p> | |
</center> | |
</body> | |
</html> | |
EOF | |
## Copy the html file from local to the container | |
## SYNTAX: docker cp </path/to/file> <container-name/id>:</dest/path/to/file> | |
docker cp index.html my-nginx:/usr/share/nginx/html/index.html | |
## Verify if nginx started rendering the updated html page | Get the container IP again and copy in the browser | |
docker inspect my-nginx -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}' | |
## Now save the updated container with a new image | |
## Why?? | Since containers are ephemeral, the changes we have made will be lost once the container is stopped. | |
## SYNTAx: docker commit <container-name/id> <new-image-tag> | |
docker commit my-nginx my-nginx-image | |
## Check the new image | |
docker images | |
## Run the new container image | |
docker run --name my-new-nginx -d my-nginx-image | |
## Verify that the container is running | |
docker ps | |
## Check the webpage hosted by the new nginx container | Get the IP address of the container and copy it over browser. | |
docker inspect my-new-nginx -f '{{ range.NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}' | |
## Rename the existing docker image to "<dockerhub-id>/<image-name>" | |
## WHY?? Because, for pushing the image to dockerhub, we need to follow the above format | |
## SYNTAX: docker tag <image-name> <dockerhub-id>/<new-image-name> | |
docker tag my-nginx-image yatharth0045/my-nginx-image | |
## Login to dockerhub via docker CLI using your dockerhub creds | |
docker login | |
## It will prompt for your ID and Password | |
## Push the image to dockerhub | |
## SYNTAX: docker push <dockerhub-id>/<image-name> | |
docker push yatharth0045/my-nginx-image | |
## Remove the running container | |
## Way 1 | Stop the container and then remove it | |
docker stop my-nginx && docker rm my-nginx | |
## Way 2 | Forcefully remove the container | |
docker rm -f my-new-nginx | |
## Delete the image from local | |
## Removing multiple images | |
## SYNTAX: docker rmi <image/s> | |
docker rmi nginx my-nginx-image yatharth0045/my-nginx-image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment