ENV variables not coming through godotenv Docker - docker

I have a web app written in Go, dockerised and using gomod.
I cannot get it to read environment variables.
Upon running docker-compose up always returns "Error getting env, not comming through"
I'm using godotenv to try do this. Below is my implementation. I cannot for the life of me figure out what's going wrong. If anyone can see something I'm missing you'll be saving a life.
The main.go, .env, docker-compose.yml and Dockerfile are all in the root of the project
main.go
func main() {
router := mux.NewRouter()
err := godotenv.Load()
if err != nil {
log.Fatalf("Error getting env, not comming through %v", err)
} else {
fmt.Println("We are getting the env values")
}
fmt.Println(os.Getenv("MY_ENV"))
}
.env
MY_ENV=thisismyenvvariable
DB_HOST=testdata123
DB_DRIVER=testdata123
DB_USER="testdata123"
DB_PASSWORD=testdata123
DB_NAME=testdata123
DB_PORT=5432
docker-compose.yml
version: '3'
services:
app:
container_name: template_123
build: .
ports:
- 8080:8080
restart: on-failure
volumes:
- api:/usr/src/app/
env_file:
- .env
depends_on:
- template-postgres
networks:
- template
template-postgres:
image: postgres:latest
container_name: startup_template_golang_db_postgres
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- DATABASE_HOST=${DB_HOST}
ports:
- '5432:5432'
volumes:
- database_postgres:/var/lib/postgresql/data
env_file:
- .env
networks:
- template
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin_container
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
depends_on:
- template-postgres
ports:
- "5050:80"
networks:
- template
restart: unless-stopped
volumes:
api:
database_postgres:
# Networks to be created to facilitate communication between containers
networks:
startup_template:
driver: bridge
Dockerfile
# Start from golang base image
FROM golang:alpine as builder
# ENV GO111MODULE=on
# Add Maintainer info
LABEL maintainer="satoshi123"
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git
# Set the current working directory inside the container
WORKDIR /app
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed
RUN go mod download
# Copy the source from the current directory to the working Directory inside the container
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Start a new stage from scratch
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .
# COPY --from=builder /app/.env .
# Expose port 8080 to the outside world
EXPOSE 8080
#Command to run the executable
CMD ["./main"]

If you're already using env_file in your docker_compose.yml, you don't really need godotenv, as the environment is already passed down from docker-compose:
version: '3'
services:
app:
image: busybox:latest
command: sh -c 'echo "Hello $$USER!"'
env_file:
- .env
# .env
USER=user1
$ docker-compose up
Recreating test_app_1 ... done
Attaching to test_app_1
app_1 | Hello user1!
test_app_1 exited with code 0
This is a better idea than trying to copy the .env file into the container, because it means you can pass environment variables without having to rebuild the container each time ;)
If you nonetheless want to use godotenv, I found that by simply uncommenting the COPY --from=builder /app/.env . line from your Dockerfile, the .env file gets loaded correctly (as godotenv finds it in the directory, whereas if it were commented it wouldn't).
$ docker-compose up
Starting template_123 ... done
Attaching to template_123
template_123 | We are getting the env values
template_123 | thisismyenvvariable
template_123 exited with code 0
If you want to keep it in sync with your filesystem, you will need to use a volume to link your .env with the one on your filesystem, or as I've said, ditch godotenv altogether as it is not really useful in your case.

Related

How to dockerize React Nextjs

I want to build my next js project by docker tool, but I got some trouble like this:
Error: Could not find a production build in the '/var/app/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
Dockerfile:
FROM node:16-alpine
RUN mkdir -p /var/app
COPY ["./", "/var/app"]
WORKDIR /var/app
RUN npm i -g next
EXPOSE 3002
RUN npm run build
CMD ["npm", "run", "start"]
docker-compose.yml
version: '3.3'
services:
next-project:
container_name: next-project
build: ./
working_dir: /var/app
restart: 'unless-stopped'
volumes:
- ./:/var/app
env_file:
- .env
ports:
- "54000:3002"
I do run commands like this
docker-compose build && docker-compose up -d
the build was successful but when it run is failed, is there any missing configuration?
When you map your current directory to /var/app, all the files that are in that directory in the container become hidden and replaced with the files in the current directory.
Since you don't have a .next directory in the host directory, the container can't find the built files.
To get it to run, you need to remove the mapping of the current directory, so your docker-compose file becomes
version: '3.3'
services:
next-project:
container_name: next-project
build: ./
working_dir: /var/app
restart: 'unless-stopped'
env_file:
- .env
ports:
- "54000:3002"

Golang docker container exiting inmediately after it starts

My container exits and I don't know why, probably because I don't know enough Go and of course Docker.
Here is my Dockerfile and docker-compose.yml.
The reason of that network name is because this docker-compose file is being extended by another at a parent folder together with other apps. Also the version 2.
The go get./ and go buid I took it from Go official image at dockerhub, and the go mod download and go run cmd/server/main.go are commands the app needs to run.
FROM golang:1.13
RUN mkdir /prework_ms
COPY . /prework_ms
WORKDIR /prework_ms
RUN cd /prework_ms \
go get ./ \
go build \
go mod download \
go run cmd/server/main.go
EXPOSE 8080
docker-compose.yml:
version: "2"
services:
go:
build: .
volumes:
- .:/prework_ms
ports:
- "8080:8080"
networks:
- appnet
mysql:
image: mysql:5.7.25
ports:
- "3306:3306"
volumes:
- ./sql:/docker-entrypoint-initdb.d/
networks:
- appnet
environment:
MYSQL_ROOT_PASSWORD: prework
MYSQL_DATABASE: prework
The command that starts the service is declared using the Dockerfile CMD instruction. Also, the RUN commands should be separated using shell's &&:
FROM golang:1.13
RUN mkdir /prework_ms
COPY . /prework_ms
WORKDIR /prework_ms
RUN go get ./ && go build && go mod download
EXPOSE 8080
CMD ["go", "run", "cmd/server/main.go"]

docker-compose doesn't refresh volumes on file changes

I am using Docker Toolbox on Windows 10 Home. I am not able to see the changes in my code on the docker-container.
My docker-compose.yml file looks like this
version: "3.7"
services:
flask:
build: ./flask
container_name: flask
restart: always
environment:
- APP_NAME=MyFlaskApp
expose:
- 8080
volumes:
- ./flask:/app
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
And my Dockerfile looks like this
FROM python:3.7.2-stretch
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install the dependencies
RUN pip install -r requirements.txt
# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
My folder structure is like this
-project
- flask
- app.ini
- Dockerfile
- requirements.txt
- run.py
-nginx
- Dockerfile
- nginx.conf
I'm pretty sure everything is in order here but I still can't make live changes to the server

"Error: Cannot find module" with Nodemon and Docker, even with volumes mounted

I keep getting errors that my modules don't exist when I'm running nodemon inside Docker and I save the node files. It takes a couple of saves before it throws the error. I have the volumes mounted like how the answers suggested here but I'm still getting the error and I'm not too sure what's causing it.
Here is my docker-compose.yml file.
version: "3.7"
services:
api:
container_name: api
build:
context: ./api
target: development
restart: on-failure
ports:
- "3000:3000"
- "9229:9229"
volumes:
- "./api:/home/node/app"
- "node_modules:/home/node/app/node_modules"
depends_on:
- db
networks:
- backend
db:
container_name: db
command: mongod --noauth --smallfiles
image: mongo
restart: on-failure
volumes:
- "mongo-data:/data/db"
- "./scripts:/scripts"
- "./data:/data/"
ports:
- "27017:27017"
networks:
- backend
networks:
backend:
driver: bridge
volumes:
mongo-data:
node_modules:
Here is my docker file:
# Ger current Node Alpine Linux image.
FROM node:alpine AS base
# Expose port 3000 for node.
EXPOSE 3000
# Set working directory.
WORKDIR /home/node/app
# Copy project content.
COPY package*.json ./
# Development environment.
FROM base AS development
# Set environment of node to development to trigger flag.
ENV NODE_ENV=development
# Express flag.
ENV DEBUG=app
# Run NPM install.
RUN npm install
# Copy source code.
COPY . /home/node/app
# Run the app.
CMD [ "npm", "start" ]
# Production environment.
FROM base AS production
# Set environment of node to production to trigger flag.
ENV NODE_ENV=production
# Run NPM install.
RUN npm install --only=production --no-optional && npm cache clean --force
# Copy source code.
COPY . /home/node/app
# Set user to node for better security.
USER node
# Run the app.
CMD [ "npm", "run", "start:prod" ]
Turns out I didn't put my .dockerignore in the proper folder. You're supposed to put it in the context folder.

mkdir and copy files vs volume for webpack in docker

I am trying to get webpack setup on my docker container. It is working, and running, but when I save on my local computer it is not updating my files in my container. I have the following docker-compose file:
version: '2'
services:
web:
build:
context: .
dockerfile: docker/web/Dockerfile
container_name: arc-bis-www-web
restart: on-failure:3
environment:
FPM_HOST: 'php'
ports:
- 8080:8080
volumes:
- ./app:/usr/local/src/app
php:
build:
context: .
dockerfile: docker/php/Dockerfile
environment:
CRM_HOST: '192.168.1.79'
CRM_NAME: 'ARC_test_8_8_17'
CRM_PORT: '1433'
CRM_USER: 'sa'
CRM_PASSWORD: 'Multi*Gr4in'
volumes:
- ./app:/usr/local/src/app
node:
build:
context: .
dockerfile: docker/node/Dockerfile
container_name: arc-bis-www-node
volumes:
- ./app:/usr/local/src/app
and my node container is run by the following dockerfile:
FROM node:8
RUN useradd --create-home user
RUN mkdir /usr/local/src/app
RUN mkdir /usr/local/src/app/src
RUN mkdir /usr/local/src/app/test
WORKDIR /usr/local/src/app
# Copy application source files
COPY ./app/package.json /usr/local/src/app/package.json
COPY ./app/.babelrc /usr/local/src/app/.babelrc
COPY ./app/webpack.config.js /usr/local/src/app/webpack.config.js
COPY ./app/test /usr/local/src/app/test
RUN chown -R user:user /usr/local/src/app
USER user
RUN npm install
ENTRYPOINT ["npm"]
Now I have taken out the copy calls from above and it still runs fine, but neither option is allowing me to save files locally and have them show up in the localhost for my container. Ideally, I thought having a volume would allow me to update my local files and have it read by the volume in the container. Does that make sense? I am still feeling my way around Docker. Thanks in advance for any help.
If you start your container with -v tag, you can map the container and your local storage. You can find more information here.

Resources