Home > other >  Dockerfile cannot run a container using "docker-compose-up --build" command
Dockerfile cannot run a container using "docker-compose-up --build" command

Time:06-04

Dockerfile cannot run a container using "docker-compose-up --build" command

When I run Dockerfile using the "docker-compose up --build" command, the file not found is output, and the container is not running.

Dockerfile, docker-compose.yaml, directory and result is below.

Docker version :

\server>docker --version
Docker version 20.10.14, build a224086

Dockerfile :

FROM openjdk:14-jdk-alpine3.10

RUN mkdir -p /app/workspace/config && \
    mkdir -p /app/workspace/lib && \
    mkdir -p /app/workspace/bin

WORKDIR /app/workspace
VOLUME /app/workspace

COPY ./bin ./bin
COPY ./config ./config
COPY ./lib ./lib

RUN chmod 774 /app/workspace/bin/*.sh

EXPOSE 6969

WORKDIR /app/workspace/bin

ENTRYPOINT ./startServer.sh

docker-compose.yaml:

version: '3'

services:
  server:
    container_name: cn-server
    build:
      context: ./server/
      dockerfile: Dockerfile
    ports:
      - "6969:6969"
    volumes:
      - ${SERVER_HOST_DIR}:/app/workspace
    networks:
      - backend

networks:
    backend:
        driver: bridge

directories : enter image description here

"docker-compose up --build" command execution result :

Building server
[ ] Building 3.7s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                               0.0s 
 => => transferring dockerfile: 425B                                                                                                                                                                                               0.0s 
 => [internal] load .dockerignore                                                                                                                                                                                                  0.0s 
 => => transferring context: 2B                                                                                                                                                                                                    0.0s 
 => [internal] load metadata for docker.io/library/openjdk:14-jdk-alpine3.10                                                                                                                                                       2.0s 
 => [internal] load build context                                                                                                                                                                                                  0.0s 
 => => transferring context: 239B                                                                                                                                                                                                  0.0s 
 => CACHED [1/8] FROM docker.io/library/openjdk:14-jdk-alpine3.10@sha256:b8082268ef46d44ec70fd5a64c71d445492941813ba9d68049be6e63a0da542f                                                                                          0.0s 
 => [2/8] RUN mkdir -p /app/workspace/config &&     mkdir -p /app/workspace/lib &&     mkdir -p /app/workspace/bin                                                                                                                 0.4s 
 => [3/8] WORKDIR /app/workspace                                                                                                                                                                                                   0.1s 
 => [4/8] COPY ./bin ./bin                                                                                                                                                                                                         0.1s 
 => [5/8] COPY ./config ./config                                                                                                                                                                                                   0.1s 
 => [6/8] COPY ./lib ./lib                                                                                                                                                                                                         0.1s 
 => [7/8] RUN chmod 774 /app/workspace/bin/*.sh                                                                                                                                                                                    0.5s 
 => [8/8] WORKDIR /app/workspace/bin                                                                                                                                                                                               0.1s 
 => exporting to image                                                                                                                                                                                                             0.2s 
 => => exporting layers                                                                                                                                                                                                            0.2s 
 => => writing image sha256:984554c9d7d9b3312fbe2dc76b4c7381e93cebca3a808ca16bd9e3777d42f919                                                                                                                                       0.0s 
 => => naming to docker.io/library/docker_cn-server                                                                                                                                                                               0.0s 
Creating cn-server ... done
Attaching to cn-server
cn-server        | /bin/sh: ./startServer.sh: not found
cn-server exited with code 127

Also bin, config, lib directories are not created in host volume directory and no files.

Please tell me what I was wrong or what I used wrong.

Thank you.

CodePudding user response:

There are two obvious problems here, both related to Docker volumes.

In your Dockerfile, you switch to WORKDIR /app/workspace and do some work there, but then in the Compose setup, you bind-mount a host directory over all of /app/workspace. This causes all of the work in the Dockerfile to be lost, and replaces the code in the image with unpredictable content from the host. In the docker-compose.yml file you should delete the volumes: block. You should be able to reduce what you've shown to as little as

version: '3.8'
services:
  server:
    build: ./server
    ports:
      - '6969:6969'

The second problem is in the Dockerfile itself. You declare VOLUME /app/workspace fairly early on. This is unnecessary, though, and its most visible effect is to cause later RUN commands in that directory to have no effect. So in particular your RUN chmod ... command isn't happening. Deleting the VOLUME line can help with that. (You also don't need to RUN mkdir directories you're about to COPY into the image.)

FROM openjdk:14-jdk-alpine3.10

WORKDIR /app/workspace
COPY ./bin ./bin
COPY ./config ./config
COPY ./lib ./lib
RUN chmod 0755 bin/*.sh

EXPOSE 6969
WORKDIR /app/workspace/bin
CMD ["./startServer.sh"]

There are other potential problems depending on the content of the startServer.sh file. I'd expect this to be a shell script and its first line to be a "shebang" line, #!/bin/sh. If it explicitly names GNU Bash or another shell, that won't be present in an Alpine-based image. If you're working on a Windows-based system and the file has DOS line endings, that will also cause an error.

  • Related