Basic Docker Commands
Tags: docker, devops, beginners
Published: 2020-05-26 07:09:18

Once you have docker desktop installed, you can run the below command to check if docker is running fine
`docker version`

I've faced a lot of issues trying to resolve issues since there are few restrictions on my work laptop. 
Quick & Easy thing I found to proactively do is : Run everything as an Admin - both docker desktop and your command prompt
Remember, Stack Overflow is your best friend for any issues you face

### RUNNING AN IMAGE
`docker run <image-name>`
This runs the docker image that is specified.
Example: `docker run hello-world`
Docker has a set of available images that can downloaded. On running the command, it will automatically download the image and run. 
One of them is hello-world which is the basic docker image, used to test if docker is installed correctly on your machine.

### OVERRIDE DEFAULT IMAGE COMMAND
`docker run <image-name> <command>`
This runs the docker image & overrides the default command that is specified in the image
Example: `docker run busybox echo hi there`
Example: `docker run busybox echo ls`
Note: We cant run the same in hello-world because echo command is not supported/ does not exist in the hello-world Snapshot. Busybox is another pre-defined image that is available and will automatically be downloaded the first time you run it.

### DISPLAY RUNNING IMAGES
`docker ps`
Shows the output of Headers and all images currently running. This shows very important information such as the container-id, name of the image, the default command, status etc.

### DISPLAY ALL IMAGES
`docker ps --all`
If you want to see the history of all images, this command will show all containers ever created.

### UNDERSTANDING DOCKER RUN
`docker run <image-name>`
command runs 2 commands together- 
1. `docker create <image-name>`
2. `docker start <image-name>`

Example: 
`docker create hello-world` This command creates the image and outputs the ID of the image created. This can directly be used in the next command to start the image 
`docker start -a <container-id>` -a is added to show the output coming from the process. By default it will not show you any information from the terminal. Container Id can be got from `docker ps --all` command or from the create command
`docker start <container-id>`
Note: Once a container has already been created, you cannot replace the overridden default command. Hence, we cannot do below for an open container since the container is already defined 
`docker start -a <container-id> echo something`

### DELETING UNWANTED CONTAINERS
`docker system prune`
This deletes the stopped containers and build cache that was created. 
Note: if we run `docker ps --all` , it show empty after running prune
Be extremely careful when running this command as you will lose all previous information!

### RETRIEVING LOG OUTPUT
In case we forget -a after starting a container, we do not need to run the entire image again to see the output  
`docker logs <container-id>`
Displays all information emitted from a container
Overall sequence
`docker create busybox hi there
docker start <container-id>               
docker logs <container-id>`
   
### STOPPING A CONTAINER 
`docker stop <container-id>`
Allows tasks to auto complete. If the current task doesn't stop in 10 seconds, it automatically runs the kill command to force stop the task
`docker kill <container-id>`
Immediate shutdown of all tasks running

### EXECUTING A COMMAND IN RUNNING CONTAINER
`docker exec -it <container-id> <command>`
It gives option to give an input  
Example: `docker run redis`
`docker exec -it <container-id> redis-cli`
Note: redis is yet another free container which is an open source in-memory database. You can easily save and fetch data using the cli.
-it can be split into -i -t
-i make sure that whatever we type gets set to STDIN of the process we are running 
-t effect is to make all text shows in a formatted manner on the screen. 
You could try out and see what the difference is when you don't add either of these options
Once the redis-cli is open you can even run redis supported commands,
`set value 100`
`get value` this will print 100

### GETTING SHELL/TERMINAL ACCESS IN A RUNNING CONTAINER
To avoid docker exec each time, we can actually get shell/terminal access in a running container
`docker exec -it <container-id> sh`
Using sh command will directly opens up the container and now you can run commands within the container 
To exit press Ctrl+C or Ctrl+D (Command+C or D on Mac)
Note: sh is a shell or a command processor. It allows us to type commands in and process them. Similar to bash on Mac or powerShell/gitbash on windows. 

### STARTING AN IMAGE WITH SHELL OPEN
`docker run -it <image-name> sh`
Example: `docker run -it busybox sh`                    
This will automatically start the container with shell command.

### CONTAINER ISOLATION
If we run the same process on 2 different containers, they stay completely independent from each other. 
Example if we run
`docker run -it busybox sh`
on 2 command prompts, and lets say we make changes like adding a file in one container only.
It does not reflect in the other. This way they are decoupled entirely



That comes to the end of the basic Docker scripts that you will need to get started. I just thought it might be helpful to get a list of basic commands and what it does out there, so a beginner could easily refer the list and what these commands mean. Hope this helps someone somewhere sometime!!!!