dockerfile-commands

Dockerfile Commands

What is Dockerfile and for what is used for?
  • Docker builds images automatically by reading the instructions from a Dockerfile.
  • It is a text file without any .txt extensions that contains all commands in order, needed to build a given image.
  • It is always named Dockerfile.

Docker image consists of read-only layers each of which represents a Dockerfile instruction. The layers are stacked and each one is created by the change from the previous layer. For example, if I create a base layer of ubuntu and then in second instruction I install Python it will create a second layer. Likewise, if I do any changes by the instructions(RUN , COPY , ADD) it will create a new layer in that image.

Containers are read-write layers that are created by docker images.

In simple words, a Dockerfile is a set of instructions that creates a stacked-layer for each instruction that collectively makes an image(which is a prototype or template for containers)

dockerfile-commands-1.jpeg

dockerfile-commands-2.jpg

Frequently used Dockerfile commands –

  • FROM – Defines a base image, it can be pulled from docker hub
    (for example- if we want to create a javascript application with node as backend then we need to have node as a base image, so it can run node application.)
  • RUN – Executes command in a new image layer( we can have multiple run commands )
  • CMD – Command to be executed when running a container( It is asked to have one CMD command, If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.
  • EXPOSE – Documents which ports are exposed (It is only used for documentation)
  • ENV – Sets environment variables inside the image
  • COPY – It is used to copy your local files/directories to Docker Container.
  • ADD – It is more feature-rich version of the COPY instruction. COPY is preferred over ADD. Major difference b/w ADD and COPY is that ADD allows you to copy from URL that is the source can be URL but in COPY it can only have local ones.
  • ENTRYPOINT – Define a container’s executable (You cannot override and ENTRYPOINT when starting a container unless you add the –entrypoint flag.)
  • VOLUME – It defines which directory in an image should be treated as a volume. The volume will be given a random name which can be found using docker inspect command.
  • WORKDIR – Defines the working directory for subsequent instructions in the Dockerfile(Important point to remember that it doesn’t create a new intermediate layer in Image
    #Basic Dockerfile
    FROM ubuntu:18.04
    COPY . /app
    RUN make /app
    CMD python /app/app.py

Each instruction creates one layer:

FROM creates a layer from the ubuntu:18.04 Docker image.
COPY adds files from your Docker client’s current directory.
RUN builds your application with make.
CMD specifies what command to run within the container.
Let’s see this demo example of Docker layer architecture-

dockerfile-commands-3.png

If some files should be prevented from being copied into the Docker image(it can be sensitive informations like .env files which contains API keys or any other files that are not much important), a .dockerignore file can be added at the same level as the Dockerfile where files that should not be copied over into the Docker image can be specified. By this if we are using a COPY or ADD instruction in a Dockerfile to specify the files to be added into a Docker image, any file specified in the .dockerignore file will be ignored and not added into the Docker image.

Shell and Exec forms

All three instructions (RUN, CMD and ENTRYPOINT) can be specified in shell form or exec form.

Shell form

RUN apt-get install python3
CMD echo "Hello world"
ENTRYPOINT echo "Hello world" 

Exec form
This is the preferred form for CMD and ENTRYPOINT instructions. [“executable”, “param1”, “param2”, …]

Difference between RUN,CMD and ENTRYPOINT?

RUN – RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.

RUN apt-get install python

CMD – CMD instruction allows you to set a default command, which will be executed only when you run container without specifying a command. If Docker container runs with a command, the default command will be ignored. If Dockerfile has more than one CMD instruction, all but last CMD instructions are ignored.

CMD "echo" "Hello World!"

ENTRYPOINT – ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD, because it also allows you to specify a command with parameters. The difference is ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.

Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.

Choose CMD if you need to provide a default command and/or arguments that can be overwritten from command line when docker container runs.

Other Dokerfile commands

  • Copy command: COPY is a dockerfile command that copies files from a local source location to a destination in the Docker container. …
Usage. The general syntax of the COPY command is:

COPY <src> and <dest> : 

 here . <src> is the path to the source folder containing files to be copied. and  <dest> is the path in the container.
  • WORKDIR command: is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

 

  • <strong>RUN</strong> command:  triggers while we build the docker image.

RUN – RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.

  • <strong>CMD</strong> command: triggers while we launch the created docker image.

CMD – CMD instruction allows you to set a default command, which will be executed only when you run container without specifying a command. If Docker container runs with a command, the default command will be ignored. If Dockerfile has more than one CMD instruction, all but last
CMD instructions are ignored.

What is the difference between Run dotnet build and  Run dotnet publish in DockerFile?

Run dotnet build  compiles the source code into a (hopefully) runnable application. Publish takes that runnable application and puts it somewhere for other people to run it.

RUN dotnet build "ProductMicroservice.csproj" -c Release -o /app/build

dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. The output includes the following assets: Intermediate Language (IL) code in an assembly with a dll extension.

RUN dotnet publish "ProductMicroservice.csproj" -c Release -o /app/publish

Comparing Docker Images To Docker Containers

An image consists of a collection of files (or layers) that pack together all the necessities—such as dependencies, source code, and libraries—needed to set up a completely functional container environment.
Images are stored on a Docker registry, such as the Docker Hub, or on a local registry.

To check the Docker images run the command: docker images:

C:\Users\default1>docker images
REPOSITORY                   TAG   IMAGE  ID     CREATED         SIZE
productmicroservice          dev  37042bae6ee2   11 minutes ago  354MB

mymicroservice              dev a58f8995980f    7 hours ago   349MB



mehzan07/productmicroservice    latest 9114f6d2eead 5 weeks ago 349MB

following attributes:

Repository— repository name for image.
⦁ Tag—identifies the image by its tag, such as version number.
⦁ Image ID—is a unique image identity.
⦁ Created—the period of time since it was created.
⦁ Size—the image’s virtual size.

Command: docker history Repository:tag

C:\Users\default1> docker history productmicroservice:dev
IMAGE CREATED CREATED BY SIZE COMMENT
37042bae6ee2 26 minutes ago cmd /S /C #(nop) LABEL com.microsoft.visual… 41kB
6f968bb2ee2d 26 minutes ago cmd /S /C #(nop) LABEL com.microsoft.create… 41kB

To see all the layers that make up the downloaded image.

What Exactly Is A Docker Container?
A Docker container is a virtualized runtime environment that provides isolation capabilities for separating the execution of applications from the underpinning system. It’s an instance of a Docker image.
Containers are the ultimate utility of the Docker technology—they provide a portable and lightweight environment for deploying applications.
Each container is autonomous and runs in its own isolated environment, ensuring it does not disrupt other running applications or its underlying system. This greatly improves the security of applications.
Docker defines several container states, such as created, restarting, running, paused, exited, and dead. Since several states are possible, and a container is just an instance of the image, a container does not need to be running.
Here is an example of how to launch a container based on the latest version of a bash image:
Here is an example of how to create a container from an image  (a container based on the dev version of a productmicroservice image)

C:\Users\default1>docker run productmicroservice:dev
Microsoft Windows [Version 10.0.17763.3046]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\app>

Then, to view its details on your system, you can use either the docker ps command (outputs only running containers) or docker ps -a command (outputs both running and stopped containers).
Here is an example:

C:\Users\default1>docker ps
CONTAINER ID            IMAGE           COMMAND CREATED STATUS PORTS NAMES
405ff00d09da productmicroservice:dev "C:\\remote_debugger\\…" 46 minutes ago Up 46 minutes 0.0.0.0:51802->80/tcp ProductMicroservice

Every time Docker creates a container from an image, it places a thin read-write layer on top of the image. This writable layer allows for changes to be made to the container, as the lower layers in the image are unchangeable. It also stores any changes made to the container during its entire runtime.

 

Where is docker images location?

In Windows 10:
Start docker
In cmd run:
>docker info
Then you can get detail info and look to the Docker Root Dir
Docker Root Dir: C:\ProgramData\Docker
If you go to this path you can find: Image as a directory then try find the current image under
this.
Look to the:
C:\ProgramData\Docker\image\windowsfilter
C:\ProgramData\Docker\windowsfilter
You can find list of directories, look to the datum and find the current docker image.

In linux:
If you are using docker on Windows Subsystem for Linux (WSL2), you can access the
images via hidden share:
\\wsl$\docker-desktop-data\version-pack-data\community\docker\overlay2
The volums are also there at:
\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes
The docker version is 20.10.7

Conclusion

Understanding the differences between Docker images and Docker containers will help you in designing better Docker applications.
Docker images are read-only templates used to build containers. Containers are deployed instances created from those templates. Images and containers are closely related, and are essential in powering the Docker software platform

This post was part of Topics

Back to home page

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *