Getting Docker Dev Engine to work with VS Code - docker

whenever I run the dev engine, I get the following error at the end of the install:
chown: invalid group: ‘root:docker’
WARNING: Could not change owner for docker socket in container : exit code 1
Docker socket permission set to allow in container docker
I am on macOS, so not sure if I need to create a docker group or not.
I have the following Dockerfile.devenv
FROM python:3.9-buster
COPY requirements.txt requirements.txt
COPY . .
RUN pip install -r requirements.txt
USER root
WORKDIR /src
EXPOSE 8000
RUN useradd -ms /bin/bash devenv
and no docker compose file.

Based on docker documentation when we are specifying Dockerfile, we need to make sure that we are using vscode user and included to docker group.
So i think your Dockerfile.devenv need to be updated. e.g:
FROM python:3.9-buster
WORKDIR /src
COPY requirements.txt requirements.txt
COPY . .
RUN pip install -r requirements.txt
# create vscode user then add to docker group
RUN useradd -s /bin/bash -m vscode \
&& groupadd docker \
&& usermod -aG docker vscode
USER vscode
EXPOSE 8000

According to the documentation: https://learn.microsoft.com/en-us/visualstudio/mac/docker-quickstart?view=vsmac-2019
It could be possible to run the service of Visual Studio by configurating the dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY DockerDemo/DockerDemo.csproj DockerDemo/
RUN dotnet restore "DockerDemo/DockerDemo.csproj"
COPY . .
WORKDIR "/src/DockerDemo"
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]
So it could be possible to run your python environment within the environment for visual studio to run over macOS:
FROM python:3.9-buster
COPY requirements.txt requirements.txt
COPY . .
RUN pip install -r requirements.txt
USER root
WORKDIR /src
EXPOSE 8000
RUN useradd -ms /bin/bash devenv
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /src
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY DockerDemo/DockerDemo.csproj DockerDemo/
RUN dotnet restore "DockerDemo/DockerDemo.csproj"
COPY . .
WORKDIR "/src/DockerDemo"
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /src
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]

Related

Some folders are not copied when build image with docker build for asp.net core web application

I have following dockerfile content
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Nuget.config", "."]
COPY ["MyProject/MyProject.csproj", "MyProject/"]
RUN dotnet restore "MyProject/MyProject.csproj" --configfile /src/Nuget.config
COPY . .
WORKDIR /src/MyProject
RUN dotnet build "MyProject.csproj" -c Release -o /app/build --configfile /src/Nuget.config
RUN mkdir -p /app/publish/MyNotes
RUN mkdir -p /app/publish/MyData
RUN cp -R "../packages/mylibrary/2.0.3/lib/netstandard2.0/MyNotes" "/app/publish/"
RUN cp -R "../packages/mylibrary/2.0.3/lib/netstandard2.0/MyData" "/app/publish/"
#COPY ["../packages/mylibrary/2.0.3/lib/netstandard2.0/MyNotes", "/app/publish/MyNotes"]
#COPY ["../packages/mylibrary/2.0.3/lib/netstandard2.0/MyNotes/ps/", "/app/publish/MyNotes/ps/"]
#COPY ["../packages/mylibrary/2.0.3/lib/netstandard2.0/MyNotes/pc/", "/app/publish/MyNotes/pc/"]
#COPY ["../packages/mylibrary/2.0.3/lib/netstandard2.0/MyData", "/app/publish/MyData"]
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN find . -type f
ENTRYPOINT ["dotnet", "MyProject.dll"]
I want to copy some files from packages folder to build folder.
The structure of MyNotes and MyData:
MyNotes
|_ pc
|_ example1.txt
|_ example2.txt
|_ ps
|_ example4.txt
MyData
|_ example7.txt
If I run RUN find . -type f, it seems that folders MyData and MyNotes are copied.
After compiled image, after I run docker image docker run -d -p 8080:80 mycontainer and then docker exec -it GUID bash, I run ls to see that MyNotes folder does not exist, but MyData is.
I don't understand why, where is my mistake ? I have same issue if I run COPY (see commented lines with # in front).

Even with a slight change in code, docker image build takes a lot of time

Here is a Dockerfile and I wonder why with a slight change in C# code rebuilding the image takes a lot of time compared to Python. I read all tips and still no chance. Is there a way to improve it?
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
RUN apt-get update && apt-get install -y libldap-2.4-2
RUN apt-get install curl -y
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["test/testAdmin.csproj", "test/"]
COPY ["testDal/testDal.csproj", "testDal/"]
COPY ["testCore/testCore.csproj", "testCore/"]
RUN dotnet restore "test/testAdmin.csproj"
COPY . .
WORKDIR "/src/testAdmin"
RUN pwd
RUN ls
RUN mv appsettings.json appsettings.json
RUN dotnet build "testAdmin.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "testAdmin.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "testAdmin.dll"]
For production environment this is OK but for testing we need faster builds for every pull.

How to create a database and run ef core migration in docker?

This is my Dockerfile
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApi.csproj", "."]
RUN dotnet restore "./MyApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApi.dll"]
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
This is entrypoint.sh
#!/bin/bash
set -e
run_cmd="dotnet run --server.urls http://*:80"
until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done
>&2 echo "SQL Server is up - executing command"
exec $run_cmd
sleep 10
>&2 echo "Creating Database"
/opt/mssql-tools/bin/sqlcmd -S db -U sa -P P#55w0rd -i create-database.sql
This is create-database.sql
USE master
GO
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'Demo')
BEGIN
CREATE DATABASE Demo;
END;
GO
But when I run it, I got this error
#17 0.615 chmod: cannot access './entrypoint.sh': No such file or directory
#17 ERROR: executor failed running [/bin/sh -c chmod +x ./entrypoint.sh]: exit code: 1
Your file is not in the directory in which you think it is.
Remember that the WORKDIR command is effectively cd for your Dockerfile. Let's take a close look at the sequence of events in your question.
In the first stage, you have WORKDIR /src. You copy files from your local directory into the /src directory:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApi.csproj", "."]
COPY . .
In your final phase, you copy files from /app/publish into /app:
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
Your CMD attempts to run entrypoint.sh in current directory, which is /app because of that WORKDIR /app directive:
CMD bash entrypoint.sh
However, you never explicitly move entrypoint.sh from /src to /app/publish, and while I'm not familiar with the dotnet publish command, I'm guessing that it doesn't move that file for you, either.
The solution is to make sure that entrypoint.sh gets moved into /app. The simplest solution may be to just copy it in explicitly in your final stage:
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY entrypoint.sh /app/entrypoint.sh
ENTRYPOINT ["dotnet", "MyApi.dll"]
CMD /bin/bash ./entrypoint.sh

Unable to use Arg values in commands in dockerfile

I am trying to allow passing build args to a dockerfile during build process but I cannot figure out how to get it to work.
This is my dockerfile
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
RUN apt-get update && apt-get install openssh-server -y \
&& echo "root:Docker!" | chpasswd
COPY sshd_config /etc/ssh/
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 2222
ARG node_build=production
ENV node_build_env=$node_build
ARG net_build=Release
ENV net_build_env=$net_build
FROM node:12.18.3 AS node-build
RUN echo $build_command_env
WORKDIR /root
COPY ["MyProject/package.json", "."]
COPY ["MyProject/package-lock.json", "."]
RUN npm i --ignore-scripts
COPY ["MyProject/angular.json", "."]
COPY ["MyProject/tsconfig.json", "."]
COPY ["MyProject/tslint.json", "."]
COPY ["MyProject/src", "./src"]
RUN npx ng build -c $node_build_env
FROM microsoft/dotnet:2.2-sdk AS build
RUN echo $net_build_env
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
COPY ["MyProject.ServiceModel/MyProject.ServiceModel.csproj", "MyProject.ServiceModel/"]
COPY ["MyProject.ServiceInterface/MyProject.ServiceInterface.csproj", "MyProject.ServiceInterface/"]
COPY ["NuGet.config", "NuGet.config"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c $net_build_env -o /app
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c $net_build_env -o /app
FROM base AS final
WORKDIR /app
COPY --chown=www-data:www-data --from=publish /app .
COPY --chown=www-data:www-data --from=node-build /root/wwwroot ./wwwroot
ENTRYPOINT service ssh start && dotnet MyProject.dll
I have tried many different ways. I tried just using ARG and declaring after every FROM but that didn't work. This version I try copying ARG to ENV but that also didn't work.
How do I do this?
Issue was that variable had to be in double quote:
RUN dotnet build "MyProject.csproj" -c "$net_build_env" -o /app
You have to add --build-arg <arg>=<value> in your docker build command in order to use ARG arguments in your containerfile.
There's no need for ENV if you're only going to use the args in container build process

docker build not setting up environment variables using ENV

Trying to build a docker image with golang and react code. The environment variable JWT_SECRET_KEY is not being set.
# Build the Go API
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
ENV JWT_SECRET_KEY=DefaultKey
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .
# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build
# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main
To build this i ran the command
docker build -t webapp .
If you want JWT_SECRET_KEY to be set in the production stage you need to move it to that stage. Or if you need it in both copy it. So change your docker file to
# Build the Go API
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .
# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build
# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
ENV JWT_SECRET_KEY=DefaultKey
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main

Resources