Change directory in docker alpine failing - docker

Trying to build a dockerfile with alpine image and install set of directories. Here is the script below. It works fine until mkdir but does not change to /opt to download the git code.
The git code gets downloaded only to /src. Not sure whey the cd /opt command does not work.
FROM alpine
ADD . /src
WORKDIR /src
RUN apk update
RUN apk add git
RUN mkdir /opt
RUN cd /opt && git clone --recursive https://github.com/Azure/azure-iot-sdk-python.git
RUN ls -al
RUN cd azure-iot-sdk-python && ls -al build_all/linux

The current working directory is reset for every RUN to the value of the last WORKDIR.
So, because of that, this line RUN cd azure-iot-sdk-python && ls -al build_all/linux is doing it from here /src, and your cloned repo is in opt.
So both of this will work:
RUN cd /opt/azure-iot-sdk-python && ls -al build_all/linux
and:
WORKDIR /opt
RUN cd azure-iot-sdk-python && ls -al build_all/linux

Robert is right, and you could avoid this problem by following the Dockerfile best practice, and have only one RUN, see https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

Related

Copy a repo clone in dockerfile

I'm trying to clone a repository in my dockerfile and then copy that repository into a specific folder of the docker container.
Here is my dockerfile:
FROM node:11-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json /usr/src/app/
RUN apk --no-cache --virtual build-dependencies add git python make g++ \
&& git config --global url."https://".insteadOf git:// \
&& apk add curl \
&& apk add git bash && git clone https://github.com/vishnubob/wait-for-it.git \
&& yarn install \
&& yarn cache clean --force \
&& apk del build-dependencies
COPY wait-for-it /usr/src/app
This outputs: ERROR: Service 'exchanges_api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder416734770/wait-for-it: no such file or directory
However, if I add a RUN ls before the copy I get confirmation the file exists, any idea where I might be going wrong?
From this error '' no such file or directory" you see that is the problem with file path and indeed as you can see there is a mistake in this COPY . /package. json this is not a valid path you either need something like .. /package.json or simply package.json depends on where is the file located in your directory.
COPY is used to copy resource from host machine to your docker image. But in you use case, it looks like you are trying to copy resource from one location to other within the docker image.
Replace COPY wait-for-it /usr/src/app with the following:
RUN cp -a wait-for-it /usr/src/app

Setting up NSCA in Docker Alpine image for passive nagios check

In the Alpine linux package site https://pkgs.alpinelinux.org/packages
NSCA packages are yet to get added. Is there an alternative to setup NSCA in Alpine Linux for passive-check?
If there is no package for it, you can always build it yourself.
FROM alpine AS builder
ARG NSCA_VERSION=2.9.2
RUN apk update && apk add build-base build-base gcc wget git
RUN wget http://prdownloads.sourceforge.net/nagios/nsca-$NSCA_VERSION.tar.gz
RUN tar xzf nsca-$NSCA_VERSION.tar.gz
RUN cd nsca-$NSCA_VERSION&& ./configure && make all
RUN ls -lah nsca-$NSCA_VERSION/src
RUN mkdir -p /dist/bin && cp nsca-$NSCA_VERSION/src/nsca /dist/bin
RUN mkdir -p /dist/etc && cp nsca-$NSCA_VERSION/sample-config/nsca.cfg /dist/etc
FROM alpine
COPY --from=builder /dist/bin/nsca /bin/
COPY --from=builder /dist/etc/nsca.cfg /etc/
Since this is using multiple stages, your resulting image will not contain development files and will still be small.

Dockerfile is caching an old version of a generated file

I'm working on a Dockerfile with a multi-stage build. The general idea is to build the binary for the backend, build the javascript bundle for the frontend, and then put these two things in a final container for the app.
Here's the docker file:
# go binary
FROM golang:alpine as build-go
RUN apk --no-cache add git bzr mercurial
ENV D=/go/src/github.com/tamuhack-org/quack
RUN go get -d -v golang.org/x/net/html
RUN go get -d -v github.com/gorilla/handlers
RUN go get -d -v github.com/gorilla/mux
COPY ./main.go $D/main.go
COPY ./frontend/dist $D/frontend/dist
RUN rm -rf $D/frontend/dist/index.html
RUN rm -rf $D/frontend/dist/index.js
RUN cd $D && go build -o main && cp main /tmp/
# ui
FROM node:alpine AS build-node
RUN mkdir -p /src/ui
COPY ./frontend/package.json /src/ui/
RUN cd /src/ui && yarn install
COPY ./frontend /src/ui
# Replace the dev instance of index.html with the prod version.
RUN rm -rf /src/ui/dist/index.html
RUN mv /src/ui/dist/index-prod.html /src/ui/dist/index.html
RUN cd /src/ui && yarn build
# final
FROM alpine
RUN apk --no-cache add ca-certificates
WORKDIR /app/server/
COPY --from=build-go /tmp/main /app/server/
COPY --from=build-node /src/ui/dist /app/server/frontend/dist
EXPOSE 8080
CMD ["./main"]
What I've noticed is that when I update the frontend source code and build the docker container, the new version of the container doesn't update with the new bundle. Are there any obvious errors in the Dockerfile that may be the reason for why I'm not seeing any file changes? If I run yarn build locally, the bundle is accurate, but the docker container seems to be caching an older version. Thoughts?

Dockerfile: Permission denied during build when running ssh-agent on /tmp

So I'm trying to create an image, which adds a SSH private key to /tmp, runs ssh-agent on it, does a git clone and then deletes the key again.
This is the idea I'm trying to accomplish
Dockerfile:
FROM node:4.2.4
MAINTAINER Me
CMD ["/bin/bash"]
ENV GIT_SSL_NO_VERIFY=1
ENV https_proxy="httpsproxy"
ENV http_proxy="httpproxy"
ENV no_proxy="exceptions"
ADD projectfolder/key /tmp/
RUN ssh-agent /tmp
WORKDIR /usr/src/app
RUN git clone git#gitlab.private.address:something/target.git
RUN rm /tmp/key
WORKDIR /usr/src/app/target
RUN npm install
EXPOSE 3001
Now the problem lies within the build-process. I use the following command to build:
docker build -t samprog/targetimage:4.2.4 -f projectfolder/dockerfile .
The layers up to "ADD projectfolder/key /tmp/" work just fine, though the "RUN ssh-agent /tmp" layer doesn't want to cooperate.
Error code:
Step 9 : RUN ssh-agent /tmp/temp
---> Running in d2ed7c8870ae
/tmp: Permission denied
The command '/bin/sh -c ssh-agent /tmp' returned a non-zero code: 1
Any ideas? Since I thought it was a permission issue, where the directory was already created by the parent image, I created a /tmp/temp and put the key in there. Doesn't work either, same error.
I'm using Docker version 1.10.3 on SLES12 SP1
I did it. What I did is, I got rid of ssh-agent. I simply copied the ~/.ssh- directory of my docker-host into the /root/.ssh of the image and it worked.
Do not use the ~ though, copy the ~/.ssh-directory inside the projectfolder first and then with the dockerfile inside the container.
Final dockerfile looked as follows:
FROM node:4.2.4
MAINTAINER me
CMD["/bin/bash"]
ENV GIT_SSL_NO_VERIFY=1
ENV https_proxy="httpsproxy"
ENV http_proxy="httpproxy"
ENV no_proxy="exceptions"
ADD projectfolder/.ssh /root/.ssh
WORKDIR /usr/src/app
RUN git clone git#gitlab.private.address:something/target.git
RUN rm -r /root/.ssh
WORKDIR /urs/src/app/target
RUN npm set registry http://local-npm-registry
RUN npm install
EXPOSE 3001
The dockerfile still has to be improved on efficiency and stuff, but it works! Eureka!
The image now has to be squashed and it should be safe to use, though we only use it in our local registry.
I have faced with the same problem with maven:3-alpine. It was solved when I properly installed openssh-client:
RUN apk --update add openssh-client
Then copied keys with known hosts to the image:
ADD id_rsa /root/.ssh/
ADD id_rsa.pub /root/.ssh/
ADD known_hosts /root/.ssh/
And ran git clone command inline (with ssh-agent and ssh-add):
RUN eval $(ssh-agent -s) \
&& ssh-add \
&& git clone ssh://git#private.address:port/project/project.git
Complete docker file:
FROM maven:3-alpine
RUN apk update
RUN apk add python
RUN apk add ansible
RUN apk add git
RUN apk --update add openssh-client
ADD id_rsa /root/.ssh/
ADD id_rsa.pub /root/.ssh/
ADD known_hosts /root/.ssh/
RUN eval $(ssh-agent -s) \
&& ssh-add \
&& git clone ssh://git#private.address:port/project/project.git
ADD hosts /etc/ansible/hosts
RUN ansible all -m ping --ask-pass
I had the same issue while executing any bash command when building my Dockerfile.
I solved by adding RUN chmod -R 777 ./ like suggested in the answer of this question. I think this is a workaround, I'm not sure why docker in ubuntu has permission issues when building a container.

Understanding a simple dockerfile of postgresql

I was just reading a docker file HERE.
and basically the Dockerfile looks like follows:
FROM postgres:9.1
MAINTAINER Mike Dillon <mike#appropriate.io>
ENV POSTGIS_MAJOR 2.1
ENV POSTGIS_VERSION 2.1.7+dfsg-3~94.git954a8d0.pgdg80+1
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \
postgis=$POSTGIS_VERSION \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /docker-entrypoint-initdb.d
COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh
I want to make sure i have interpreted the below two commands correctly:
RUN mkdir -p /docker-entrypoint-initdb.d
COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh
The RUN command is running a mkdir command , which means the current directory will have a subdirectory called:
/docker-entrypoint-initdb.d
andin the next command I.E. the COPY command the contents of the directory ./initdb-postgis.sh are being copied into /docker-entrypoint-initdb.d/postgis.sh , am i right ?
RUN cmd is used to execute cmd command and will create and commit a new layer for the image your are building. So those commands are run in the context of the image that is being building. mkdir -p /docker-entrypoint-initdb.d will create a new folder docker-entrypoint-initdb.d in the root of the image. COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh will copy the file initdb-postgis.sh (that has to be located at the same level you have run the docker build command to the file /docker-entrypoint-initdb.d/postgis.sh inside the container.

Resources