The Ultimate Cheat Sheet for Docker and Docker Compose

The Ultimate Cheat Sheet for Docker and Docker Compose

Day 20: #90DaysOfDevOps Challenge

Here is a Cheat sheet of carefully curated Docker and Docker Compose you will ever need:-

1. Commands for Installation of Docker:

CommandMeaningSyntax
For WindowsThis command helps you to install Docker on Windows.https://download.docker.com/win/stable/InstallDocker.msi
For LinuxThis command helps you to install Docker on Linux.curl -sSL https://get.docker.com/
For macThis command helps you to install Docker on mac os.https://download.docker.com/

2.Commands for Docker Registry and Repository :

CommandMeaningSyntax
Log in to a RegistryThis command helps you log in to your Registry.docker login docker login localhost:8080
Logout from a registry:This command helps you log out from your Registry.docker logout docker logout localhost:8080
Searching an imageBy using this docker command you can search any image from your docker.search nginx docker search --filter stars=3 --no-trunc nginx
Pulling an ImageThis command can be used to download a specific image or set of images.docker image pull nginx docker image pull eon01/nginx localhost:5000/myadmin/nginx
Pushing an imageThis command can be used to push a specific image or set of images.docker image push eon01/nginx docker image push eon01/nginx localhost:5000/myadmin/nginx

3. Commands for Running Containers :

CommandMeaningSyntax
To create a containerThis command is used to create a container without runningdocker container create -t -i eon01/infinite --name XYZ
To run a containerThis command is used to run a containerdocker container run -it --name XYZ -d eon01/infinite
To rename a containerUse this command to rename a containerdocker container rename XYZ infinity
For removing a containerThis command is used to remove container in the topicdocker container rm infinite
Update a containerThis command is used to update the container in the topicdocker container update --cpu-shares 512 -m 300M infinite

4. Commands for Starting or Stopping the Container:

CommandMeaningSyntax
For starting a containerThis command is used for starting a containerdocker container start nginx
For stopping a containerThis command is used for stopping a containerdocker container stop nginx
For restarting the containerThis command is used for restarting a containerdocker container restart nginx
For pausing the containerThis command is used for pausing a containerdocker container pause nginx
For unpausing the containerThis command is used for unpausing a container in the dockerdocker container unpause nginx
For Blocking a containerThis command is used for blocking a container in the dockerdocker container wait nginx
Sending a SIGKILLThis command is used for Sending a SIGKILL in the dockerdocker container kill nginx
For sending another signalThis command is used for sending another signaldocker container kill -s HUP nginx
For Connecting to an Existing ContainerWe can use this command for Connecting to an Existing Containerdocker container attach nginx

5. Commands for Obtaining Container Information:

CommandMeaningSyntax
Fetching information From Running ContainersWe can fetch information from the running container by using this commanddocker ps Or docker container ls
For fetching about every containerThis command for fetching about every containerdocker container ls -aOrdocker ps -a
For container logWe can use this command to see the container logdocker logs infinite
For ‘tail -f’ Containers’ LogsWith this command Container isn’t running in the foreground, and if there isn’t anything running in the foreground, Docker closes automatically.docker container logs infinite -f
For Inspecting ContainersThis is the command used for inspecting containersdocker container inspect infinite docker container inspect --format '' $(docker ps -q)
For Containers EventsTo obtain real-time events from the server, use docker events.docker system events infinite
For Public PortsUse this command for finding a public portdocker container port infinite
For Running ProcessesWe can use this command for displaying the running processes in the containerdocker container top infinite
Command for Container Resource UsageIt displays a live stream of resources usage statics for containersdocker container stats infinite
Commands for Inspecting changes to files or directories on a container’s filesystemThis command is used for inspecting changes to files or directories on a container’s filesystemdocker container diff infinite

6. Commands for Managing Images

CommandMeaningSyntax
For listing imagesThis command is used to list imagesdocker image ls
For Building images From the current directory’s DockerfileThis command is used for building from the current directory’s dockerfiledocker build
For Building images From a GIT remote repositoryThis command is used for Building images command Using a remote GIT repositorydocker build github.com/creack/docker-firefox
For tagging and buildingThis command is for tagging and building.docker build -t eon/infinite
Specifying the Build Context while creating a DockerfileThis is used to Build an image from a Dockerfiledocker build -f myDockerfile
Creating a Dockerfile from a URLIt will help to create a dockerfile with a specific URL.curl example.com/remote/Dockerfile `
For removing imageThis command is used to remove a7n imagedocker image rm nginx
Using a File or the Normal Input Stream to Load a Tarred RepositoryUse STDIN or a tar archive to load an image.docker image load < ubuntu.tar.gz docker build -f myOtherDockerfile
Image Saving to a Tar Archiveard Input StreamIt is used Save one or more images to a tar archivedocker image save busybox > ubuntu.tar
Showing the History of an ImageThis command will let us know the history of the image inside the dockerimage history
Making an Image Out of a ContainerThis command will help you to take an image out of the containerdocker container commit nginx
For image taggingWe can use this command for image taggingdocker image tag nginx eon01/nginx
For pushing an imageWe can push any image through this commanddocker image push eon01/nginx

7. Commands for Networking:

CommandMeaningSyntax
For overlay networkThis is used to establish a distributed network between many Docker daemon hosts.docker network create -d overlay MyOverlayNetwork
For Bridge networkTo establish container test1 to bridge demo-bridge, type docker network connect demo-bridge test1.docker network create -d bridge MyBridgeNetwork
For removing a networkThis command s used to remove an overlay networkdocker network rm MyOverlayNetwork
For network listingThis command is used to list the overlay networksdocker network ls
For Getting Information About a NetworkWe can get information about an overlay network with the help of this commanddocker network inspect MyOverlayNetwork
For Connecting a Running Container to a NetworkBy using this command we can connect a container to a networkdocker network connect MyOverlayNetwork nginx
For Connecting a Container to a Network When it StartsWhen the container starts we can use this command to connect a container to the networkdocker container run -it -d --network=MyOverlayNetwork nginx
For Disconnecting a Container from a NetworkWe can use this Command for disconnecting a container from the networkdocker network disconnect MyOverlayNetwork nginx
For Exposing PortsWe can expose the empty ports using this commandEXPOSE <port_number>

8. Commands for Cleaning Docker:

CommandMeaningSyntax
For Removing a Running ContainerWe can remove a running container by using this commanddocker container rm nginx
For Removing a Container and its VolumeWe can use this command for removing the container and the things inside itdocker container rm -v nginx
For Removing all Exited ContainersWe can use this command for removing all the exited containersdocker container rm $(docker container ls -a -f status=exited -q)
For Removing All Stopped ContainersWe can remove all the stopped containers by using this commanddocker container rm docker container ls -a -q
For Removing a Docker ImageThis command is used for removing a docker imagedocker image rm nginx
For Dangling ImagesWe can dangle the images with this commanddocker image rm $(docker image ls -f dangling=true -q)
For Removing all ImagesWe can remove all the images in the docker by using this commanddocker image rm $(docker image ls -a -q)
For Delete all Untagged ImagesWe can delete all the untagged images with the use of this command`docker image rm -f $(docker image ls
For Stopping & Removing all ContainersFor stopping and removing all the containers we can use this commanddocker container stop $(docker container ls -a -q) && docker container rm $(docker container ls -a -q)
For Removing Dangling VolumesWe can remove all the dangling volumes by using this commanddocker volume rm $(docker volume ls -f dangling=true -q)
For removing all unneeded (containers, images, networks and volumes)This command is used to remove the unnecessary thingdocker system prune -f
For Clean allWe can use this command for cleaning everything in the dockerdocker system prune -a

9. Commands for Docker Compose:

Some commonly used Docker Compose commands with a brief description of their use:

Builds and starts containers based on the docker-compose.yml file

docker-compose up

Stops and removes containers created by the docker-compose up command

docker-compose down

Lists containers created by docker-compose

docker-compose ps

Displays log for containers created by docker-compose

docker-compose logs

Builds images for services defined in the docker-compose.yml file

docker-compose build

Starts containers created by docker-compose

docker-compose start

Stops containers created by docker-compose

docker-compose stop

Restarts containers created by docker-compose

docker-compose restart

Runs a command inside a running container

docker-compose exec

Validates and displays the compose file

docker-compose config

Thank you for reading!!!
~Bhaarat