Docker CLI & Dockerfile In One Shot

tanmoysrt

Tanmoy Sarkar

Posted on January 4, 2023

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

INSTRUCTION arguments
Enter fullscreen mode Exit fullscreen mode

Instruction Set

In the below table, I have mentioned some sort terms. dir -> Directory, src -> Source, dst -> Destination

INSTRUCTION DESCRIPTION
FROM <image_name> Fetch base image for Docker
COPY <src_dir_host> <dest_dir_container> Copy a folder of the host system to the container
WORKDIR <dir_path_contaiiner> Set the working directory of the container where the commands will run
RUN <command> Run commands in a shell
RUN [<command arg1>, <command arg2>] We can break the command in command args also.
Ex : RUN "apt-get update" -> RUN ["apt-get", "update"]
ENV <key>=<value> Set environment variables
EXPOSE <container_port> Expose the port of a container to make it accessible in the same network
ENTRYPOINT <command> That command will execute as soon as the container boot up
ENTRYPOINT [<command arg1>, <command arg2>] We can break the command in command args also.
Ex : RUN "/bin/sh sample.sh" -> 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

Argument

Description

-u

Username

--name

Name [Normally used for container]

-d

Detach [Run in the background]

-a

All [Stopped + Paused + Running]

-i

Interactive

-t

- tty [stdin] [For Container]
- tag name [For Image]

-it

Interactive with stdin

-w

Working directory [example -w '/opt/test']

-p

Port [Example : -p <host_system_port>:<container_port> ]

-v

Volume Mount [Example : -p <host_system_volume_path>:<container_volume_path> ]

-e

Environment Variable [Example : -e <variable_name>=<value> ]

Image Specific Commands

List all local images

docker images
Enter fullscreen mode Exit fullscreen mode

Build Image From Dockerfile in current directory

docker build . -t <tag_name>
Enter fullscreen mode Exit fullscreen mode

Pull an image from Dockerhub

docker pull <image_name>
# example : docker pull redis
Enter fullscreen mode Exit fullscreen mode

Delete an image

docker rmi redis
Enter fullscreen mode Exit fullscreen mode

Remove all unused images

docker image prune
Enter fullscreen mode Exit fullscreen mode

Container Specific Commands

Run a docker container from an image

docker run <image_name>
Enter fullscreen mode Exit fullscreen mode

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.

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]

docker ps
Enter fullscreen mode Exit fullscreen mode

List all containers [Stopped + Paused + Running]

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Start or Stop a container

docker start|stop <container_name or container_id> 
Enter fullscreen mode Exit fullscreen mode

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

Remove a stopped container

docker rm <container_name or container_id>
Enter fullscreen mode Exit fullscreen mode

Attach to the current process of container

docker attach <container_name or container_id>
Enter fullscreen mode Exit fullscreen mode

Run a command in the container

docker exec <container_name or container_id> <command>
Enter fullscreen mode Exit fullscreen mode

Example : Start a bash terminal in redis instance

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

docker logs -f <container_name or container_id>
Enter fullscreen mode Exit fullscreen mode

Inspect and grab all the details of a container

docker inspect <container_name or container_id>
Enter fullscreen mode Exit fullscreen mode

It will return the details in an JSON format

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

docker container stats
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
tanmoysrt
Tanmoy Sarkar

Posted on January 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Docker CLI & Dockerfile In One Shot