Skip to content

Commit 2c57d00

Browse files
committed
CLI cheatsheet
1 parent edbe2d6 commit 2c57d00

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM node:14.17.0-alpine3.13
2+
WORKDIR /app
3+
COPY . /app
4+
RUN npm install
5+
EXPOSE 5000
6+
CMD node index.js

README.md

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,239 @@ app.listen(port, () => {
854854
> - `app.listen(port, () => {`: This is used to start the app.
855855
> - `console.log(`Example app listening at http://localhost:${port}`);`: This is used to print the message in the terminal.
856856
857+
- Step 6: Now run the following command to start the app.
858+
```bash
859+
npm start
860+
```
861+
hit this in the terminal and you will see the following output.
862+
```bash
863+
Example app listening at http://localhost:5000
864+
```
865+
Now visit the `localhost:5000` and you will see the following output.
866+
```json
867+
{
868+
"message": "Hello World!"
869+
}
870+
```
871+
![image](https://user-images.githubusercontent.com/97989643/233419825-0a7c8dcb-75d9-42aa-9536-41148b23c591.png)
872+
873+
#### ⚡ Login to the docker hub
874+
First, you need to login to the docker hub using the following command.
875+
```bash
876+
docker login
877+
```
878+
You will see the following output.
879+
```bash
880+
Logging in with your password grants your terminal complete access to your account.
881+
```
882+
- Step 7: Now stop the app using the `ctrl+c` command then delete the `node_modules` folder and create a new file and name it `Dockerfile` and paste the following code in it.
883+
```dockerfile
884+
FROM node:14.17.0-alpine3.13
885+
WORKDIR /app
886+
COPY . /app
887+
RUN npm install
888+
EXPOSE 5000
889+
CMD node index.js
890+
```
891+
892+
- Step 8: Now run the following command to build the docker image.
893+
```bash
894+
docker build -t subham4041/second-node-app:0.0.1.RELEASE .
895+
```
896+
> - `docker build -t subham4041/second-node-app:0.0.1.RELEASE .`: This command is used to build the docker image.
897+
> - `docker build`: This is the command to build the docker image.
898+
> - `-t subham4041/second-node-app:0.0.1.RELEASE`: This is the name of the docker image.
899+
> - `subham4041`: This is the docker hub username. You will get this when you log in to the docker hub.
900+
> - `second-node-app`: This is the name of the docker image.
901+
> - `0.0.1.RELEASE`: This is the version of the docker image.
902+
> - `.`: This is the path of the dockerfile. In our case, it is the current directory.
903+
904+
now you will see something like this.
905+
![image](https://user-images.githubusercontent.com/97989643/233451193-e20a6b06-e37c-418a-a553-7cf82f8451e5.png)
906+
907+
- Step 9: Now check how many docker images running in your system using the following command.
908+
```bash
909+
docker ls
910+
```
911+
You will see the following output.
912+
```bash
913+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
914+
```
915+
> - `CONTAINER ID`: This is the id of the container.
916+
> - `IMAGE`: This is the name of the docker image.
917+
> - `COMMAND`: This is the command that is running inside the container.
918+
> - `CREATED`: This is the time when the container was created.
919+
> - `STATUS`: This is the status of the container.
920+
> - `PORTS`: This is the port number of the container.
921+
> - `NAMES`: This is the name of the container.
922+
> - You can see that there is no docker image running in your system.
923+
- Step 10: Now run the following command to run the docker image.
924+
```bash
925+
docker run -d -p 5001:5000 subham4041/second-node-app:0.0.1.RELEASE
926+
```
927+
928+
> - `docker run -d -p 5001:5000 subham4041/second-node-app:0.0.1.RELEASE`: This command is used to run the docker image.
929+
> - `docker run`: This is the command to run the docker image.
930+
> - `-d`: This is used to run the docker image in the background.
931+
> - `-p 5001:5000`: This is used to map the port number of the docker image to the port number of the host machine.
932+
> - `subham4041/second-node-app:0.0.1.RELEASE`: This is the name of the docker image.
933+
> - You can see that the docker image is running in the background.
934+
935+
- Step 11: Now check how many docker images running in your system using the following command.
936+
```bash
937+
docker ps
938+
```
939+
You will see the following output.
940+
```bash
941+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
942+
b3b0b5b5b9b0 subham4041/second-node-app:0.0.1.RELEASE "docker-entrypoint.s…" 10 seconds ago Up 9 seconds
943+
```
944+
945+
- Step 12: Now visit the `localhost:5001` and you will see the following output.
946+
```json
947+
{
948+
"message": "Hello World!"
949+
}
950+
```
951+
- Step 13: Now run the following command to push the docker image to the docker hub.
952+
```bash
953+
docker push subham4041/second-node-app:0.0.1.RELEASE
954+
```
955+
![image](https://user-images.githubusercontent.com/97989643/233453706-e185ecb4-b886-4216-b551-64bc51062eda.png)
956+
957+
- Step 14: Now let's delete the docker image from the local system using the following command.
958+
959+
> You can do this using the docker desktop app also.
960+
961+
#### ⚡ Check the container id
962+
First, you need to check the image id using the following command.
963+
```bash
964+
docker ps
965+
```
966+
#### ⚡ Stop the container
967+
Now run the following command to stop the container.
968+
```bash
969+
docker stop <container_id>
970+
```
971+
> In my case, the container id is `b3b0b5b5b9b0` so I will run the following command `docker stop b3b0b5b5b9b0`.
972+
973+
#### ⚡ Kill the container
974+
Now run the following command to kill the container.
975+
```bash
976+
docker kill <container_id>
977+
```
978+
> This will send a signal to the container to gracefully stop. If you want to forcefully stop the container immediately, you can use the docker kill command instead:`docker stop <container_id>`
979+
980+
#### ⚡ Remove the container
981+
You can remove the container using the `docker rm` command followed by the container ID. For example, to remove the container with ID `b9061e6d08b6`, you can use the following command:
982+
```bash
983+
docker rm b9061e6d08b6
984+
```
985+
#### ⚡ Remove the image
986+
After removing the container, you can then delete the image using the `docker rmi` command:
987+
```bash
988+
docker rmi <image_id>
989+
```
990+
> In my case, the image id is `a0dacd3ecd79e1252ca8e8a655902af1b6ae98ee609cdee91056c67a6ca00a5c` so I will run the following command `docker rmi a0dacd3ecd79e1252ca8e8a655902af1b6ae98ee609cdee91056c67a6ca00a5c`.
991+
> This will delete the image from your system.
992+
993+
- Step 15: Now run the following command to pull the docker image from the docker hub.
994+
#### ⚡ Pull the docker image
995+
996+
```bash
997+
docker pull subham4041/second-node-app:0.0.1.RELEASE
998+
```
999+
- Step 16: Now run the following command to run the docker image.
1000+
```bash
1001+
docker run -d -p 5001:5000 subham4041/second-node-app:0.0.1.RELEASE
1002+
```
1003+
Now visit the `localhost:5001` and you will see the following output.
1004+
```json
1005+
{
1006+
"message": "Hello World!"
1007+
}
1008+
```
1009+
1010+
## 🥰 What did we understand from these two projects?
1011+
1012+
If someone pulls the docker image from the docker hub, then they don't need to install the nodejs and npm in their system. They just need to install the docker in their system and then they can run the docker image and they can easily access the application without installing the nodejs and npm in their system. This is the **magic of docker.**
1013+
1014+
## ✨ Docker CLI Cheat Sheet
1015+
1016+
### 1. Installation
1017+
1018+
| Command | Meaning | Syntax |
1019+
| --- | --- | --- |
1020+
| For Windows | This command helps you to install Docker on Windows. | `https://download.docker.com/win/stable/InstallDocker.msi` |
1021+
| For Linux | This command helps you to install Docker on Linux. | `curl -sSL https://get.docker.com/ | sh` |
1022+
| For Mac | This command helps you to install Docker on Mac OS. | `https://download.docker.com/mac/stable/Docker.dmg` |
1023+
1024+
### 2. Docker Registry and Repository
1025+
1026+
| Command | Meaning | Syntax |
1027+
| --- | --- | --- |
1028+
| Login to a Registry | This command helps you log in to your Registry. | `docker login [options] [server]` |
1029+
| Logout from a registry | This command helps you log out from your Registry. | `docker logout [server]` |
1030+
| Searching an image | By using this docker command you can search any image from your docker. | `docker search [options] term` |
1031+
| Pulling an image | This command can be used to download a specific image or set of images. | `docker pull [options] name[:tag]` |
1032+
| Pushing an image | This command can be used to push a specific image or set of images. | `docker push [options] name[:tag]` |
1033+
1034+
### 3. Running Containers
1035+
1036+
| Command | Meaning | Syntax |
1037+
| --- | --- | --- |
1038+
| Running a container from an image | This command creates and starts a container from an image. You can specify various options such as ports, volumes, environment variables, etc. | `docker run [options] image[:tag] [command] [args]` |
1039+
| Listing running containers | This command shows all the containers that are currently running. You can use filters and format options to customize the output. | `docker ps [options]` |
1040+
| Listing all containers (running and stopped) | This command shows all the containers that exist on your system, regardless of their status. You can use filters and format options to customize the output. | `docker ps -a [options]` |
1041+
| Stopping a container | This command stops a running container by sending a SIGTERM signal and then a SIGKILL signal if the container does not stop within a grace period. You can specify multiple containers to stop at once. | `docker stop [options] container [container...]` |
1042+
| Starting a stopped container | This command starts a stopped container by restoring its state. You can specify multiple containers to start at once. | `docker start [options] container [container...]` |
1043+
| Restarting a container | This command restarts a running or stopped container by stopping and then starting it again. You can specify multiple containers to restart at once. | `docker restart [options] container [container...]` |
1044+
| Removing a container | This command removes one or more containers from your system. You can use filters and force options to remove containers that are running or have volumes attached to them. | `docker rm [options] container [container...]` |
1045+
1046+
1047+
### 4. Managing Images
1048+
1049+
| Command | Meaning | Syntax |
1050+
| --- | --- | --- |
1051+
| Building an image from a Dockerfile | This command builds an image from a Dockerfile and optionally tags it with a name and tag. You can specify various options such as build arguments, cache settings, etc. | `docker build [options] path` |
1052+
| Removing an image from your system | This command removes one or more images from your system. You can use filters and force options to remove images that are in use or have dependent images. | `docker rmi [options] image [image...]` |
1053+
| Tagging an image with a name and tag | This command assigns a name and tag to an image. You can use this to create aliases for images or to prepare them for pushing to a registry. | `docker tag [options] image[:tag] name[:tag]` |
1054+
| Saving an image to a tar archive | This command saves one or more images to a tar archive file. You can use this to backup or transfer images between systems. | `docker save [options] image [image...] -o file` |
1055+
| Loading an image from a tar archive | This command loads one or more images from a tar archive file. You can use this to restore or import images from a backup or another system. | `docker load [options] -i file` |
1056+
1057+
### 5. Managing Volumes
1058+
1059+
| Command | Meaning | Syntax |
1060+
| --- | --- | --- |
1061+
| Creating a volume | This command creates a volume on your system. You can specify various options such as name, driver, labels, etc. | `docker volume create [options] [name]` |
1062+
| Listing volumes on your system | This command shows all the volumes that are stored on your system. You can use filters and format options to customize the output. | `docker volume ls [options]` |
1063+
| Inspecting a volume | This command shows detailed information about a specific volume. You can use this to check the status, driver, mount point, etc. of a volume. | `docker volume inspect [options] volume [volume...]` |
1064+
| Removing a volume from your system | This command removes one or more volumes from your system. You can use filters and force options to remove volumes that are in use or have dependent containers. | `docker volume rm [options] volume [volume...]` |
1065+
| Pruning unused volumes from your system | This command removes all the volumes that are not referenced by any containers from your system. You can use filters to exclude some volumes from pruning. | `docker volume prune [options]` |
1066+
1067+
### 6. Managing Networks
1068+
1069+
| Command | Meaning | Syntax |
1070+
| --- | --- | --- |
1071+
| Creating a network | This command creates a network on your system. You can specify various options such as name, driver, subnet, etc. | `docker network create [options] [name]` |
1072+
| Listing networks on your system | This command shows all the networks that are available on your system. You can use filters and format options to customize the output. | `docker network ls [options]` |
1073+
| Inspecting a network | This command shows detailed information about a specific network. You can use this to check the status, driver, IP range, connected containers, etc. of a network. | `docker network inspect [options] network [network...]` |
1074+
| Removing a network from your system | This command removes one or more networks from your system. You can use filters and force options to remove networks that are in use or have dependent containers. | `docker network rm [options] network [network...]` |
1075+
| Pruning unused networks from your system | This command removes all the networks that are not used by any containers from your system. You can use filters to exclude some networks from pruning. | `docker network prune [options]` |
1076+
1077+
### 7. Miscellaneous Commands (continued)
1078+
1079+
| Command | Meaning | Syntax |
1080+
| --- | --- | --- |
1081+
| Getting help on docker commands and options | This command shows the usage and options for any docker command or subcommand. You can use this to learn more about how to use docker. | `docker [command] --help` |
1082+
| Getting the version of docker | This command shows the version and build information of docker on your system. You can use this to check if you have the latest version or if you need to update. | `docker version [options]` |
1083+
| Getting system-wide information on docker | This command shows system-wide information about docker on your system. You can use this to check the status, resources, configuration, etc. of docker. | `docker info [options]` |
1084+
| Executing a command in a running container | This command executes a command in a running container and returns the output. You can use this to run commands that are not available in the container's shell or to interact with the container's processes. | `docker exec [options] container command [args]` |
1085+
| Attaching to a running container | This command attaches your terminal to a running container's standard input, output, and error streams. You can use this to interact with the container's shell or processes. | `docker attach [options] container` |
1086+
| Copying files or folders between a container and your system | This command copies files or folders between a container and your system. You can use this to transfer data to or from a container. | `docker cp [options] source destination` |
1087+
| Viewing logs from a container | This command shows the logs from a container's standard output and error streams. You can use this to monitor or troubleshoot a container's activity. | `docker logs [options] container` |
1088+
| Committing changes to an image | This command creates a new image from a container's changes. You can use this to save your modifications or to create new images based on existing ones. | `docker commit [options] container [repository[:tag]]` |
1089+
| Building an image from stdin or a URL | This command builds an image from a Dockerfile that is provided through stdin or a URL. You can use this to build images from remote sources or scripts. | `docker build [options] - < Dockerfile` or `docker build [options] url` |
1090+
| Showing the history of an image | This command shows the history of an image, including the layers and commands that were used to create it. You can use this to inspect how an image was built or to optimize it. | `docker history [options] image` |
1091+
1092+

0 commit comments

Comments
 (0)