My Dockerfile
FROM alpine:latest
WORKDIR /usr/src/app
COPY ./init.sh /usr/src/app
RUN chmod +x init.sh
CMD ["sh","-c","init.sh"]
Build goes OK
docker build . -t test
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM alpine:latest
---> e66264b98777
Step 2/5 : WORKDIR /usr/src/app
---> Running in 1ff4467854af
Removing intermediate container 1ff4467854af
---> 6b4f1fa4ca1c
Step 3/5 : COPY ./init.sh /usr/src/app
---> e61e55063baa
Step 4/5 : RUN chmod +x init.sh
---> Running in ae3724c0f595
Removing intermediate container ae3724c0f595
---> 3c2cd99a30b9
Step 5/5 : CMD ["sh","-c","init.sh"]
---> Running in 9df8c7d70e38
Removing intermediate container 9df8c7d70e38
---> bd9c9af6380d
Successfully built bd9c9af6380d
Successfully tagged test:latest
I got this
docker run -it test
sh: init.sh: not found
Why?
Use ./init.sh to execute the script, provided that the shell script has a proper shebang like #!/bin/sh on the top:
FROM alpine:latest
WORKDIR /usr/src/app
COPY ./init.sh /usr/src/app
RUN chmod +x init.sh
CMD ["sh", "-c", "./init.sh"]
Note that it's actually not necessary, and can be shortened as CMD ./init.sh.
Related
I try to make docker image, but when I run cmd in terminal.
sudo docker build testapi .
I get an error:
=> ERROR [6/6] RUN go build -o /app/testapi/cmd/test-api 0.3s
------
> [6/6] RUN go build -o /app/testapi/cmd/test-api:
#14 0.231 no Go files in /app
------
executor failed running [/bin/sh -c go build -o /app/testapi/cmd/test-api]: exit code: 1
File structure
/testapi
/cmd
/test-api
maing.go
/pkg
/...
Dockerfile
Dockerfile:
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod ./
RUN go mod download
COPY . ./
RUN go build -o /app/testapi/cmd/test-api
EXPOSE 8080
CMD [ "/testapi/cmd/test-api" ]
Try this way.
Also do not forget to add go.sum in Dockerfile if you have. COPY go.sum .
FROM golang:1.16-alpine
WORKDIR /app
COPY go.mod .
COPY . .
RUN go build -o main ./cmd/<FOLDER_NAME>
WORKDIR /dist
RUN cp /app/main .
EXPOSE 3000
CMD ["/dist/main"]
Works fine
Status: Downloaded newer image for golang:1.16-alpine
---> 7642119cd161
Step 2/9 : WORKDIR /app
---> Running in f8ad2994262c
Removing intermediate container f8ad2994262c
---> 6e079707cc3e
Step 3/9 : COPY go.mod .
---> c79a0cc04ff5
Step 4/9 : COPY . .
---> 897027112e73
Step 5/9 : RUN go build -o main ./cmd/api
---> Running in eb4579f1c339
Removing intermediate container eb4579f1c339
---> e41f6e5542a4
Step 6/9 : WORKDIR /dist
---> Running in 92115865d1ec
Removing intermediate container 92115865d1ec
---> 8152a0ebe4bc
Step 7/9 : RUN cp /app/main .
---> Running in 5c68cb195826
Removing intermediate container 5c68cb195826
---> 6f2e1fb8a611
Step 8/9 : EXPOSE 3000
---> Running in fba77820c1ec
Removing intermediate container fba77820c1ec
---> 182a624d807a
Step 9/9 : CMD ["/dist/main"]
---> Running in 164ba25694bd
Removing intermediate container 164ba25694bd
---> 7cf22cffc472
Successfully built 7cf22cffc472
Successfully tagged bla:latest
go build [-o output] [build flags] [packages]
Im looking at a problem when i deploy to AWS ECS regarding permissions.
I need to start a shell script in the entrypoint of the Dockerfile, which uses wait-for-it.sh to pause until the supporting container is ready.
The base for the project is aspnet core and is intended to facilitate database upgrades.
The error from the container is:
container_linux.go:380: starting container process caused: exec: "./docker-entrypoint.sh": permission denied
Other posts suggest its because the scripts arnt marked as executable. See below build output which confirms this is done
Step 14/21 : WORKDIR /app
---> Running in 5517aabffaa2
Removing intermediate container 5517aabffaa2
---> 9724951dd81b
Step 15/21 : COPY Data.Migrator/wait-for-it.sh wait-for-it.sh
---> dcd710cf0671
Step 16/21 : RUN chmod +x wait-for-it.sh
---> Running in 914a4e63fbcd
Removing intermediate container 914a4e63fbcd
---> d1e8da150a8c
Step 17/21 : COPY ["Data.Migrator/docker-entrypoint.sh", "docker-entrypoint.sh"]
---> 1e193d79558c
Step 18/21 : RUN ["chmod", "+x", "docker-entrypoint.sh"]
---> Running in fb737194fcc3
Removing intermediate container fb737194fcc3
---> e89b9eaa086c
Step 19/21 : COPY --from=publish /app/publish .
---> 972f1bd340ec
Step 20/21 : ENTRYPOINT ["./docker-entrypoint.sh"]
---> Running in 19469ffcfb32
Removing intermediate container 19469ffcfb32
---> 00447053022c
My service in the docker-compose is
data_migrator:
image: xxx.dkr.ecr.eu-west-1.amazonaws.com/migrator:latest
restart: 'always'
depends_on:
sql-server-db:
condition: service_healthy
environment:
MAIN_STORE_CONNECTION_STRING: "Server=sql_server_db,1433;Database=MainStore;User Id=sa;Password=Password123#;MultipleActiveResultSets=True;"
SIGNABLE_STORE_CONNECTION_STRING: "Server=sql_server_db,1433;Database=SignableStore;User Id=sa;Password=Password123#;MultipleActiveResultSets=True;"
DEPENDSUPON_HOST: sql-server-db
DEPENDSUPON_PORT: 1433
ECS_IMAGE_PULL_BEHAVIOR: always
networks:
- publicnet
Docker entrypoint script
#!/bin/bash
# Abort on any error (including if wait-for-it fails).
set -e
# Wait for the backend to be up, if we know where it is.
if [ -n "$DEPENDSUPON_HOST" ]; then
echo "Waiting for database connection..."
./wait-for-it.sh "$DEPENDSUPON_HOST:${DEPENDSUPON_PORT:-6000}"
fi
# Run the main container command.
exec "$#"
and the dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Data.Migrator/Data.Migrator.csproj", "Data.Migrator/"]
COPY ["Web.Workflow/Web.Workflow.csproj", "Web.Workflow/"]
RUN dotnet restore "Data.Migrator/Data.Migrator.csproj"
COPY . .
WORKDIR "/src/Data.Migrator"
RUN dotnet build "Data.Migrator.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Data.Migrator.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
# Copy wait-for-it.sh into our image
COPY Data.Migrator/wait-for-it.sh wait-for-it.sh
# Make it executable, in Linux
RUN chmod +x wait-for-it.sh
COPY ["Data.Migrator/docker-entrypoint.sh", "docker-entrypoint.sh"]
RUN ["chmod", "755", "docker-entrypoint.sh"]
# Make it executable, in Linux
COPY --from=publish /app/publish .
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["dotnet", "Data.Migrator.dll"]
Edit: This is solved, see answer below
I'm trying to deploy some software using Docker / Jenkins for the first time and I'm running into some issues with the paths.
This is the current Dockerfile:
WORKDIR /usr/src/app
COPY ./ .
RUN pip install pipenv
RUN pipenv install --ignore-pipfile
ENV PATH="${PATH}:/usr/src/app"
RUN pipenv run echo $PATH
RUN pwd
RUN ls
RUN pipenv run whereis run
RUN pipenv run run
When I try to build the docker image using Jenkins, I get the following output:
09:19:48 ---> Running in ff1a52b2e299
09:19:48 Removing intermediate container ff1a52b2e299
09:19:48 ---> 67355de18e72
09:19:48 Step 7/11 : RUN pipenv run echo $PATH
09:19:48 ---> Running in 5cb904118910
09:19:49 /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/src/app
09:19:49 Removing intermediate container 5cb904118910
09:19:49 ---> 0df62985c94d
09:19:49 Step 8/11 : RUN pwd
09:19:49 ---> Running in 4e79f53b581f
09:19:49 /usr/src/app
09:19:50 Removing intermediate container 4e79f53b581f
09:19:50 ---> 563ab4218eba
09:19:50 Step 9/11 : RUN ls
09:19:50 ---> Running in fbc7670633d1
09:19:50 Dockerfile
09:19:50 Jenkinsfile
09:19:50 Pipfile
09:19:50 Pipfile.lock
09:19:50 README.md
09:19:50 alembic.ini
09:19:50 run.bat
09:19:50 run.sh
09:19:50 trendanalyse
09:19:51 Removing intermediate container fbc7670633d1
09:19:51 ---> 02a4b76defd0
09:19:51 Step 10/11 : RUN pipenv run whereis run
09:19:51 ---> Running in 70d5448e29b1
09:19:51 run: /usr/src/app/run.bat /usr/src/app/run.sh /usr/src/app/run.bat /usr/src/app/run.sh
09:19:52 Removing intermediate container 70d5448e29b1
09:19:52 ---> 455fc44688ce
09:19:52 Step 11/11 : RUN pipenv run run
09:19:52 ---> Running in b25bf9d5f818
09:19:52 [91mError: the command run could not be found within PATH or Pipfile's [scripts].
09:19:53 [0mThe command '/bin/sh -c pipenv run run' returned a non-zero code: 1
I don't see what's wrong, the run.sh file is in the current path, not sure why it won't run. It's working locally on my Windows machine, maybe it's some difference between Windows / Linux that I'm not seeing?
Thanks!
There were two issues with the Dockerfile, I needed to add the line
RUN chmod +x run.sh
Additionally, I had to change
RUN pipenv run run
to
RUN pipenv run run.sh
(I think this is due to differences in Windows / Linux). Now it works :)
Server Version: 18.03.1-ce ,RHEL 7.2 .Here is my dockerfile:
FROM openjdk:8-jdk-alpine
ENV http_proxy http://192.168.156.25:3128
ENV https_proxy http://192.168.156.25:3128
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/licensingservice
ADD #project.build.finalName#.jar /usr/local/licensingservice/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
it build without error:
---> Using cache
---> 8fa60876c229
Step 5/9 : RUN mkdir -p /usr/local/licensingservice
---> Using cache
---> bca46b1256e1
Step 6/9 : ADD licensing-service-0.0.1-SNAPSHOT.jar /usr/local/licensingservice/
---> a66979ed3755
Step 7/9 : ADD run.sh ./run.sh
---> 95b492565374
Step 8/9 : RUN chmod +x run.sh
---> Running in eec3075c30f3
Removing intermediate container eec3075c30f3
---> 96a2d7b89b80
Step 9/9 : CMD ./run.sh
---> Running in c338e9d33371
Removing intermediate container c338e9d33371
---> 324d5a83cf84
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 324d5a83cf84
Successfully tagged johncarnell/tmx-licensing-service:chapter4
but docker run -it 324d5a83cf84:
/bin/sh: ./run.sh: not found
I debug using docker run --rm -it 324d5a83cf84 cat ./run.sh,it print the file well.
run.sh:
#!/bin/sh
echo "hello1"
I suspect you are working on Windows and you are using the default Windows newline: CR LF. Change to LF in your run.sh and it will work like a charm.
But how to do that you ask? Open run.sh in Notepad++ and look at the bottom right of the window. Click on Windows (CR LF) and select Unix (LF).
It looks as if when my Dockerfile uses an ENTRYPOINT script, its base image's CMD is ignored. Could anybody tell me why and/or where in the doc this behavior is explained?
Here is an example with a Dockerfile extending the official php:7.0-fpm image (which uses CMD ["php-fpm"]). In the entrypoint script, I expect the $# variable to contain the string php-fpm but it's empty.
marc#imac-marc:/opt/php-docker$ cat Dockerfile-php
FROM php:7.0-fpm
COPY utils/entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
marc#imac-marc:/opt/php-docker$ cat utils/entrypoint.sh
#!/usr/bin/env bash
echo "CMD = $#"
marc#imac-marc:/opt/php-docker$ docker build -t me:myImage -f Dockerfile-php .
Sending build context to Docker daemon 276 kB
Step 1 : FROM php:7.0-fpm
---> d66add11c05d
Step 2 : COPY utils/entrypoint.sh /usr/local/bin/
---> a62ef60a202b
Removing intermediate container 20bd7782844e
Step 3 : RUN chmod +x /usr/local/bin/entrypoint.sh
---> Running in 27af4ffbd6c0
---> 77fba35c804d
Removing intermediate container 27af4ffbd6c0
Step 4 : ENTRYPOINT /usr/local/bin/entrypoint.sh
---> Running in 6779b16542ec
---> 326970218669
Removing intermediate container 6779b16542ec
Successfully built 326970218669
marc#imac-marc:/opt/php-docker$ docker run me:myImage
CMD =
If I add CMD ["php-fpm"] to my Dockerfile, it works as expected.
See https://github.com/moby/moby/issues/5147 - it explains when and why this behaviour was implemented.