Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have a docker container without a shell?

Can we build a docker container that doesn't have any shell in that container ? Is it possible to create a container without a shell ?

like image 312
Sandipan Avatar asked Nov 28 '19 18:11

Sandipan


People also ask

What is required to run a Docker container?

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let's start our image and make sure it is running correctly.

Can you shell into a Docker container?

You can access the bash or shell of the container and execute commands inside it and play around with the file system. You can build, test, and deploy your applications inside the container itself. Using the Docker run command to run a container and access its shell.

Does a container have its own kernel?

No. Docker image/container only has the application layer of the OS and uses the kernel and CPU of the host machine. That's why docker container boot's so fast. In your host machine kernel is already running, so if you boot your docker container it will share the running kernel and start the container so fast.

Can a Docker container be created without a base OS image?

No its not like that. To create any docker image using DockerFile, You need to start with a base docker image. That base docker image can be anything, Like an empty image as well, In the docker file in your example the FROM section says ubuntu, it means its assuming ubuntu as the base image.


1 Answers

Yes, you can create a container from scratch, which does not contain anything even bash, it will only contain binaries that you copy during build time, otherwise, it will be empty.

FROM scratch
COPY hello /
CMD ["/hello"]

You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

While scratch appears in Docker’s repository on the hub, you can’t pull it, run it, or tag any image with the name scratch. Instead, you can refer to it in your Dockerfile. For example, to create a minimal container using scratch:

scratch-docker-image

Using this as a base image, you can create your custom image, for example you only node runtime not thing more, then you try form scratch-node.

FROM node as builder

WORKDIR /app

COPY package.json package-lock.json index.js ./

RUN npm install --prod

FROM astefanutti/scratch-node

COPY --from=builder /app /

ENTRYPOINT ["node", "index.js"]
like image 60
Adiii Avatar answered Oct 15 '22 21:10

Adiii