Binary isn't persisted in dockerfile - docker

Newbie to Docker and what appears to be a simple issue isn't working. I would like to build a binary inside of a dockerfile and execute the binary in docker-compose.
Dockerfile:
FROM golang:1.10
ENV DIR=/go/src/api
RUN go get github.com/Masterminds/glide
WORKDIR $DIR
ADD glide.yaml glide.yaml
ADD glide.lock glide.lock
RUN glide install
COPY . $DIR
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
CMD main
And in my docker-compose.yml I have this:
version: '2'
services:
api:
build:
context: ./api
dockerfile: Dockerfile
ports:
- 8000:8000
However, there's no main executable when run from docker-compose. I've actually added a command: ls to the docker-compose file and it's not there. I've then added a ls to the Dockerfile image and main is there.
How do I prevent main from being lost in translation?

Related

Dockercompose run command on mounted directory

I am trying to create an editing environment for my project - I want to run poetry update on the code repository in the container when the container starts instead of manually having to do it.
When running docker-compose build editor-environment but obviously if we haven't run docker-compose up editor-environment the /code directory has not been mounted. How could I accomplish something like this?
Docker-Compose.yml
version: '3.6'
services:
editor-environment:
build:
dockerfile: env_docker/Dockerfile
volumes:
- .:/code
tty: true
Dockerfile
FROM python:3.10.4-slim-buster
RUN pip install poetry
RUN cd /code --> throws error, can't cd to /code
RUN poetry updates
ENTRYPOINT ["/bin/sh -c"]
CMD ["bash"]

Docker: Shared volume when build

I have this files:
docker-compose.yml (shortened):
version: '3.7'
services:
php-fpm:
build:
context: .
dockerfile: docker/php/Dockerfile
target: dev
volumes:
- .:/app
frontend:
build:
context: .
dockerfile: docker/php/Dockerfile
target: frontend
volumes:
- .:/app
docker/php/Dockerfile (shortened):
FROM alpine:3.13 AS frontend
WORKDIR /app
COPY . .
RUN apk add npm
RUN npm install
RUN npx webpack -p --color --progress
FROM php:7.4-fpm AS dev
ENTRYPOINT ["docker-php-entrypoint"]
WORKDIR /app
COPY ./docker/php/www-dev.conf /usr/local/etc/php-fpm.d/www.conf
CMD ["php-fpm"]
I want to use all what building in frontend (as I understood at the stage build at this time volumes are not available) in php-fpm container, but I get something like this: file_get_contents(/app/static/frontend.version): failed to open stream.
How I can do this? I don't understand very well in Docker and the only solution I have is to move build script to php-fpm container.
You need to delete the volumes: in your docker-compose.yml file. They replace the entire contents of the image's /app directory with content from the host, which means everything that gets done in the Dockerfile gets completely ignored.
The Dockerfile you show uses a setup called a multi-stage build. The important thing you can do with this is build the first part of your image using Node, then COPY --from=frontend the static files into the second part. You do not need to declare a second container in docker-compose.yml to run the first stage, the build sequence runs this automatically. This at a minimum looks like
COPY --from=frontend /app/build ./static
You will also need to COPY the rest of your application code into the image.
If you move the Dockerfile up to the top of your project's source tree, then the docker-compose.yml file becomes as simple as
version: '3.8'
services:
php-fpm:
build: . # default Dockerfile, default target (last stage)
# do not overwrite application code with volumes:
# no separate frontend: container
But you've put a little bit more logic in the Dockerfile. I might write:
FROM node:lts AS frontend # use a prebuilt Node image
WORKDIR /app
COPY package*.json . # install dependencies first to save time on rebuild
RUN npm install
COPY . . # (or a more specific subdirectory?)
RUN npx webpack -p --color --progress
FROM php:7.4-fpm AS dev
WORKDIR /app
COPY . . # (or a more specific subdirectory?)
COPY --from=frontend /app/build ./static
COPY ./docker/php/www-dev.conf /usr/local/etc/php-fpm.d/www.conf
# don't need to repeat unmodified ENTRYPOINT/CMD from base image

docker build command doesn't refresh my source files

I made a typo in my source code and I noticed it after i run docker-compose up in my cli. I tried rebuilding the project but didn't change my index.js cached code.
This is my Dockerfile
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm","start" ]
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4001:8081"
I recreated the image
$ sudo docker build -t phillalexakis/visits:latest .
and run
docker-composer up
It didn't change the source code at all, what have I completely missed? (I'm new with docker)
Docker-compose is looking for an image named [folder_name]_node-app, but the image you've built is tagged phillalexakis/visits.
Change your node-app service in docker-compose.yml file :
node-app:
build: .
image: node-app
And use docker-compose to build the images:
docker-compose build or docker-compose up --build

"No Go files in..." error when i use go with docker compose

I installed Go on Ubuntu 16.04. This is my GOPATH=/home/{username}/work.
I created a project into /home/{username}/work/src.
This is my project folder hierarchy.
project-name
services
configuration
api
main.go
Dockerfile
bff
api
main.go
Dockerfile
docker-compose.yml
favicon.ico
README.md
I can build and run with my dockerfile but I can't build and up with docker-compose.
I couldn't find any solution.
Configuration service dockerfile:
FROM golang:1.11.1-alpine3.8 as builder
RUN apk update && apk add git && go get gopkg.in/natefinch/lumberjack.v2
RUN mkdir -p /go/src/project-name/services/configuration
RUN CGO_ENABLED=0
RUN GOOS=linux
ADD . /go/src/project-name/services/configuration
ENV GOPATH /go
WORKDIR /go/src/project-name/services/configuration/api
RUN go get
RUN go build
FROM alpine
RUN apk update
RUN apk add curl
RUN mkdir -p /app
COPY --from=builder /go/src/project-name/services/configuration/api/ /app/
RUN chmod +x /app/api
WORKDIR /app
EXPOSE 5001
ENTRYPOINT ["/app/api"]
It works with dockerfile.
This is my docker-compose file:
version: '3.4'
services:
bff:
image: project-name/bff:${TAG:-latest}
build:
context: .
dockerfile: services/bff/Dockerfile
ports:
- "5000:5000"
container_name: bff
depends_on:
- configuration
configuration:
image: project-name/configuration:${TAG:-latest}
build:
context: .
dockerfile: services/configuration/Dockerfile
ports:
- "5001:5001"
container_name: configuration
It didn't work.
When the “run go get” command runs, it gives an error, the error is:
can't load package: package project-name/services/configuration/api: no Go files in /go/src/project-name/services/configuration/api
ERROR: Service 'configuration' failed to build: The command '/bin/sh -c go get' returned a non-zero code: 1
In your Dockerfile, you say
ADD . /go/src/project-name/services/configuration
which expects the build context directory on the host to contain the source files. But your docker-compose.yml file says
build:
context: .
dockerfile: services/configuration/Dockerfile
where the context directory is the root of your source control tree, not the specific Go source directory you're trying to build. If you change this to
build:
context: services/configuration
# Default value of "dockerfile: Dockerfile" will be right
it will likely work better.
In plain Docker commands, your current docker-compose.yml file says the equivalent of
cd $GOPATH/src/project-name
docker build -f services/configuration/Dockerfile .
But you're probably actually running
cd $GOPATH/src/project-name/services/configuration
docker build .
and what directory is the current directory matters.

Asp.net core with linux docker container

For a asp.net core project template in VS 2017, there are docker-compose.ci.build.yml:
version: '3'
services:
ci-build:
image: microsoft/aspnetcore-build:1.0-2.0
volumes:
- .:/src
working_dir: /src
command: /bin/bash -c "dotnet restore ./WebAppCoreDockerTest.sln && dotnet publish ./WebAppCoreDockerTest.sln -c Release -o ./obj/Docker/publish"
docker-compose.yml:
version: '3'
services:
webappcoredockertest:
image: webappcoredockertest
ports:
- 8080:80
build:
context: ./WebAppCoreDockerTest
dockerfile: Dockerfile
and Dockerfile:
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
RUN echo "Oh dang look at that $source"
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "WebAppCoreDockerTest.dll"]
The Dockerfile uses the files in obj/Docker/publish (copied to container) if source argument is null and docker-compose.ci.build.yml puhlishs project to obj/Docker/publish, I assume they are related.
So the question is How to use them together? (docker-compose.ci.build.yml publishes files to obj/Docker/publish and Dockerfile use published files)
The docker-compose.ci.build.yml file is for building the project, as you mentioned. It uses the aspnetcore-build base image which is much heftier than the aspnetcore image used by the Dockerfile. The Dockerfile gets used by the build section of the docker-compose.yml file.
Everything works together with these two steps:
docker-compose -f docker-compose.ci.build.yml run ci-build
docker-compose up --build
The -f option in the first command allows you to specify a compose file other than the default docker-compose.yml.

Resources