0

I have a microservices base architecture but the container deploy successfully but its not started.

Below is the dockerfile.

FROM alpine:latest 

RUN mkdir /app

WORKDIR /app

COPY backendApp /app

CMD [ "/app/backendApp" ]

And below is the docker-compose.yml file.

version: '3'

services:

  backend-service:
    build:
      context: ./backend-service
      dockerfile: backend-service.dockerfile
    restart: always
    ports:
      - "8080:80"
    deploy:
      mode: replicated
      replicas: 1
    environment:
      DSN: "host=postgres port=5432 user=postgres password=password dbname=postcreation sslmode=disable timezone=UTC connect_timeout=5"

And below is the log of the docker container.

enter image description here

EDIT

I have tried the below docker file but it gives another error.

FROM golang:latest

WORKDIR /app

COPY backendApp .

CMD [ "/app/backendApp" ]

Error enter image description here

Thanks

Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50

1 Answers1

1

There is one rectification in the backend-service.dockerfile, RUN mkdir /app is not required, the line WORKDIR /app will create app directory if it doesn't exists.

Also, after WORKDIR /app command gets executed it will change to the newly created directory so, COPY backendApp /app can also be written as COPY backendApp .

COPY backendApp /app is also fine.

Now coming to the no such file or directory error, the alpine docker image is very minimalist Linux image.
alpine Linux image doesn't comes with standard runtime libraries needed by your backendApp executable, you need to find out the runtime dependencies and add/install into your alpine Linux image using backend-service.dockerfile.

Refer the example below to get a bit more of understanding:
This is the file structure of the directory

Dockers$ tree
.
├── backend-service
│   ├── backendApp
│   ├── backendApp.c
│   └── backend-service.dockerfile
└── docker-compose.yml

2 directories, 4 files

As you can see there is a backendApp.c file, with just a simple Hello World! print statement in a while(1) loop and backendApp is the compiled binary.

The contents of respective backend-service.dockerfile is shown below:

FROM alpine:latest 

WORKDIR /app
COPY backendApp .
CMD [ "/app/backendApp" ]

The docker-compose.yml is exactly same as yours, now after running docker-compose build I get the output image:

docker images
REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
dockers-backend-service   latest    bbd272015851   22 minutes ago   7.07MB

And if I run it, I get the exact same error as yours

docker run dockers-backend-service
exec /app/backendApp: no such file or directory

Now here the problem is that the backendApp is a C program which needs libc6 library which is not part of alpine Linux image and that's why the error.

To resolve this I just have to add RUN apk add libc6-compat in backend-service.dockerfile and re-build the image and it will resolve the runtime dependency of backendApp binary

$ docker run dockers-backend-service
Hello Docker Compose!!

I hope this answers your Question!

Gaurav Pathak
  • 1,065
  • 11
  • 28
  • Thanks @Gaurav for the detailed explanation. I understand it now. but I am looking for it for golang linux. I have tried https://stackoverflow.com/questions/52056387/how-to-install-go-in-alpine-linux but it's not working. Can you please help me with it? – Girish Bhutiya Mar 11 '23 at 10:24
  • Hi @GirishBhutiya, I tried running a simple go program binary in alpine linux and it is working fine with `libc6-compat`. You can try the same approach I mentioned in the answer, you just need to compile the go program before hand – Gaurav Pathak Mar 11 '23 at 12:35
  • Here is the latest dockerfile. https://www.awesomescreenshot.com/image/37888858?key=981f409949f6427b57ad5ef504acdd35 and here are new errors. - https://www.awesomescreenshot.com/image/37888868?key=38bd6e7db5c5cf3e89c25fee455fc0e9 I have tried to search for new errors on google but nothing works. – Girish Bhutiya Mar 11 '23 at 13:30
  • 1
    This is eligible for a new question, perhaps you can post more details along with your go lang program code – Gaurav Pathak Mar 11 '23 at 14:18