Docker container exit automatically

Hello,
I am building docker containers. I used below sample dockerfile for building.

# Stage 1: Build stage

FROM ubuntu:latest AS build
 
# Install build-essential for compiling C++ code

RUN apt-get update && apt-get install -y build-essential && apt-get install bash
 
# Set the working directory

WORKDIR /app
 
# Copy the source code into the container

COPY hello.cpp .

 
# Compile the C++ code statically to ensure it doesn't depend on runtime libraries

RUN g++ -o hello hello.cpp -static
 
# Stage 2: Runtime stage

FROM scratch
 
#try for bash
 
# Copy the static binary from the build stage

COPY --from=build /app/hello /hello
 
# Command to run the binary

CMD ["/hello"]
 

I used below command ro run :slight_smile:

sudo docker run -it cpp_helloworld

But when I checked it exited and was not running. The status was created

Later I used below command to run the container:

# : sudo docker run -it cpp_helloworld /bin/sh
# :sudo docker run -it cpp_helloworld /bin/bash
# :sudo docker run -it cpp_helloworld sh
# :sudo docker run -it cpp_helloworld bash

But I got the error saying :

3feff6b9b9fc82655c1bdcd8adecfeedee46433ff4e65de53e59f4f087b5c032
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: “/bin/sh”: stat /bin/sh: no such file or directory: unknown

Here, am I missing anything in dockerfile? or while running? Please guide me here.

regards,
Siddhartha V

So you are building an image (not container) from scratch and copy only the hello binary and expect a shell work in an empty container :slight_smile:

You have to figure out why the hello binary doesn’t give you any response, but FROM scratch means you want a totally empty image and copy your binaries to it. There will be nothing else in it.

Regarding the hello binary, I assume it is not exdecutable or you have an application issue and you have to figure it out in the code.

The permission issue could be solved by using --chmod in the COPY instruction

https://docs.docker.com/reference/dockerfile/#copy—chown—chmod

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.