# Docker Images

Now to create our custom image :

<https://github.com/mmumshad/simple-webapp-flask>

![](/files/-MERMNPFdrGr-3tpsVFR)

When Docker build the image it build in a Layered architecture:

by running docker history command we can check the disk space used in every layer:

<https://github.com/mmumshad/simple-webapp-flask>

![](/files/-MERSBpkxkBIZboc_EgL)

All the layers build are cached by Docker so in case a particular step failed it will reuse the cache from the previous step.

![](/files/-MERTDR3XXjLYQlO0HdB)

**Command Vs Entrypoint :**

When we run docker run ubuntu command . It runs an instance of Ubuntu image & exits immediately. If we want to see the list of running containers we not able to. As we can see above after executing docker ps -a it showing it exited . Now question is why is that ??

a) Unlike VMs containers are not meant to host the operating systems . Conatiners are meant to run a specific task of process such a to host an instance of a WebServer , application server , a database or simply to carry some kind of computations or analysis task . Once the task is completed the container exits. A container only lives as long as the process inside it is alive . If the webservice inside the container is stopped or crashed then container exits. This is why the Ubuntu container that we run stops immediately . Bcoz Ubuntu is just an image of an operating system that is used as the base image for other applications . There is no application or process running inside it by default .

**>>>Now who defines what process is run within the container. If we see the Dockerfile for NGNIX there is a instruction called CMD .**

**>>CMD defines the program that will be run within the container when it starts.**

![](/files/-MERWwUc81vLyUo53eQr)

**What we trying to do earlier was to run a container with a plain ubuntu operating system shown below :**

> \>>bash is not really a process like our webservers or database servers. It is a shell that listens for inputs from the terminal & if it cannot find the terminal it exits.
>
> \>>When we ran the Ubuntu container earlier Docker created a container from the Ubuntu image and launch the bash program . By default Docker doesn't attach a terminal to a container when it is run . And so the bash program doesn't find a terminal & so it exits.. Since the process that was started when the container was created finsihed container exits as well . So how do we specify a different command  to start the container . One option is to append a command to the docker run cmd & that way it overrides the default command specify within the image.
>
> docker run ubuntu sleep 5
>
> But how to make above change permanent . Say we always want the image to run the sleep command when it starts .

![](/files/-MERXi7fUvd92j0w8BEL)

![](/files/-MER_7v8-vok1j7eS48S)

Now as we can see we have hardcoded the sleep interval.But it doesn't look very good. The name of the image ubuntu-sleeper itself implies that the container will sleep so we should have to specify sleep command againinstead we would like to be shown below :

docker run ubuntu-sleeper 10

The sleep command should be invoked automatically & that is where the ENTRYPOINT instructions comes in to play:

FROM Ubuntu

ENTRYPOINT \["sleep"]

The ENTRYPOINT instruction is like the command instrcution as in we specify the program that will be run when the container starts & whatever we specify let say 10 in the case is appended to the ENTRYPOINT. So the command that will be run when the container starts is sleep 10.

So that the diff between these two :

In case of CMD instruction the command line parameters passed will get replaced entirely where as in case of ENTRYPOINT the command line parameters will get appended .

\>>>We have to use both CMD & ENTRYPOINT instruction :

![](/files/-MERbpIxc_woeYdbGauX)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://architectures.gitbook.io/project/docker/docker-images.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
