# Docker CLI & Dockerfile In One Shot

In this blog, I will try to provide you with a complete cheat sheet on Docker CLI & YAML so that you can quickly revise the Docker without wasting any time.

This blog is intended for those who have learnt Docker Previously and want to revise that.

If you like this blog, give a 👍 and follow for more content.

---

## Dockerfile

It's just a text file that contains instructions for building a Docker image.

It's just to automate the process that you do manually. So if you know how to do a process manually, you can learn it quickly.

### Format

```bash
INSTRUCTION arguments
```

### Instruction Set

In the below table, I have mentioned some sort terms. `dir` -&gt; `Directory`, `src` -&gt; `Source`, `dst` -&gt; `Destination`

| INSTRUCTION | DESCRIPTION |
| --- | --- |
| FROM &lt;image\_name&gt; | Fetch base image for Docker |
| COPY &lt;src\_dir\_host&gt; &lt;dest\_dir\_container&gt; | Copy a folder of the host system to the container |
| WORKDIR &lt;dir\_path\_contaiiner&gt; | Set the working directory of the container where the commands will run |
| RUN &lt;command&gt; | Run commands in a shell |
| RUN \[&lt;command arg1&gt;, &lt;command arg2&gt;\] | We can break the command in command args also.  
Ex : `RUN "apt-get update"` -&gt; `RUN ["apt-get", "update"]` |
| ENV &lt;key&gt;=&lt;value&gt; | Set environment variables |
| EXPOSE &lt;container\_port&gt; | Expose the port of a container to make it accessible in the same network |
| ENTRYPOINT &lt;command&gt; | That command will execute as soon as the container boot up |
| ENTRYPOINT \[&lt;command arg1&gt;, &lt;command arg2&gt;\] | We can break the command in command args also.  
Ex : `RUN "/bin/sh sample.sh"` -&gt; `RUN ["/bin/sh", "sample.sh"]` |

There is one more command, which you see mostly. `CMD`

The purpose of this command is to run the commands specified in arguments. It also has two formats, for example.

* CMD echo "hello-world."
    
* CMD \["/bin/sh", "echo", "hello-world"\]
    

You might be confused now about the difference between **RUN** and **CMD.**

* `RUN` runs all the commands specified in the argument
    
* For `CMD` there is some particular case.
    
    * If you have provided some commands to run in `docker run` command, `CMD` is going to be ignored.
        
    * If you have listed multiple CMD statements, only the last CMD will be executed.
        
    * IF `ENTRYPOINT` in Dockerfile is provided, `CMD` it is going to be ignored.
        
    * In any container deployment platform, you can see the `CMD` to run manually.
        

🔵 As the blog's purpose is to give concise details of Dockerfile and CLI, we are not attaching Dockerfile examples here. But will create some blogs on Dockerfile and attach links to them.

---

## Docker CLI

### Arguments List

<table><tbody><tr><td colspan="1" rowspan="1" colwidth="270"><p><strong>Argument</strong></p></td><td colspan="1" rowspan="1"><p><strong>Description</strong></p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-u</p></td><td colspan="1" rowspan="1"><p>Username</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>--name</p></td><td colspan="1" rowspan="1"><p>Name [Normally used for container]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-d</p></td><td colspan="1" rowspan="1"><p>Detach [Run in the background]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-a</p></td><td colspan="1" rowspan="1"><p>All [Stopped + Paused + Running]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-i</p></td><td colspan="1" rowspan="1"><p>Interactive</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-t</p></td><td colspan="1" rowspan="1"><p>- tty [stdin] [For Container]<br>- tag name [For Image]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-it</p></td><td colspan="1" rowspan="1"><p>Interactive with stdin</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-w</p></td><td colspan="1" rowspan="1"><p>Working directory [<em>example</em> -w '/opt/test']</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-p</p></td><td colspan="1" rowspan="1"><p>Port [Example : -p &lt;host_system_port&gt;:&lt;container_port&gt; ]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-v</p></td><td colspan="1" rowspan="1"><p>Volume Mount [Example : -p &lt;host_system_volume_path&gt;:&lt;container_volume_path&gt; ]</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="270"><p>-e</p></td><td colspan="1" rowspan="1"><p>Environment Variable [Example : -e &lt;variable_name&gt;=&lt;value&gt; ]</p></td></tr></tbody></table>

### Image Specific Commands

**List all local images**

```bash
docker images
```

**Build Image From** `Dockerfile` **in current directory**

```bash
docker build . -t <tag_name>
```

**Pull an image from Dockerhub**

```bash
docker pull <image_name>
# example : docker pull redis
```

**Delete an image**

```bash
docker rmi redis
```

**Remove all unused images**

```bash
docker image prune
```

### **Container Specific Commands**

**Run a docker container from an image**

```bash
docker run <image_name>
```

> Now you can refer the **Arguments List** table to choose your required arguments
> 
> I am solving an question for example.
> 
> Let's assume, I need to create an container from image of **redis** with port mapping from container port 6379 to host port 6379 . The name will be **redis-instance-1 .** Make the instance detachable & run `redis-server --requirepass "SECRET_PASSWORD"` at its beginning.
> 
> ```bash
> docker run --name redis-instance-1 -d -p 6379:6379 redis redis-server --requirepass "SECRET_PASSWORD"
> ```
> 
> > **Its important to notice that , you need to pass all the arguments before providing the image name and entrypoint\_command**

**List containers \[only Running\]**

```bash
docker ps
```

**List all containers \[Stopped + Paused + Running\]**

```bash
docker ps -a
```

**Start or Stop a container**

```bash
docker start|stop <container_name or container_id> 
```

> You can get the `Container ID` by running the command for list all container

**Remove a stopped container**

```bash
docker rm <container_name or container_id>
```

**Attach to the current process of container**

```bash
docker attach <container_name or container_id>
```

**Run a command in the container**

```bash
docker exec <container_name or container_id> <command>
```

> Example : Start a bash terminal in redis instance
> 
> ```bash
> docker exec -it redis /bin/sh
> ```

***❓You may have a question. What's the difference between exec and attach, then***

With attach, we attach to the process that's currently running in the container

But with exec, we can run a new process in a container without hampering the running process of the container

**Log the data of the current process of the container**

```bash
docker logs -f <container_name or container_id>
```

**Inspect and grab all the details of a container**

```bash
docker inspect <container_name or container_id>
```

> It will return the details in an JSON format

Check the stats \[CPU, RAM, Network, Storage usage\] for all running containers

```bash
docker container stats
```

---

🏄 You have liked this blog and gained some knowledge, please consider to like & share with your friends.
