Docker and Golang permission denied : "can't create log directory" - docker

Hello,
I'm here today because I'm stuck on a permission problem of my application.
I have set up a "lumberjack" log system to keep track of everything that happens on my application and rotate logs. In local I don't have any problem... but when I go in production it's not the same.
My error appear here when I want to access at "/var/log/app:
if err := os.MkdirAll(config.Directory, 0755); err != nil {
log.Error().Err(err).Str("path", config.Directory).Msg("can't create log directory")
return nil
}
A part of my docker-compose.yml:
(maybe missing a volumes in this container ?)
app:
container_name: app
image: app
restart: always
depends_on:
- postgres
networks:
- app
- web
environment:
- xxx_USERNAME=xxxxx
- xxx_PASSWORD=xxxxx
- xxx_HOST=xxxxx
- xxx_NAME=xxxxx
- xxx_DEBUG=1
- xxx_CONSOLELOGGING_LOG=TRUE
- xxx_ENCODELOGJSON_LOG=TRUE
- xxx_FILELOGGING_LOG=FALSE
- xxx_DIRECTORY_LOG=/var/log/app
- xxx_FILENAME_LOG=file.log
- xxx_MAXSIZE_LOG=100
- xxx_MAXBACKUPS_LOG=5
- xxx_MAXAGE_LOG=30
ports:
- 443:443
labels:
- #TRAEFIK CONFIG
Makebuild for Docker:
CGO_ENABLED=0 go build -ldflags "-X main.gitCommit=$(GIT_COMMIT) -X main.buildDate=$(BUILD_DATE) -X main.version=$(VERSION) -linkmode external -extldflags '-static' -s -w" -a -installsuffix cgo -o web
Dockerfile for my production app:
############################
# STEP 1 build executable binary
############################
FROM golang:alpine as builder
# Install git + SSL ca certificates.
# Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache git ca-certificates gcc g++ make && update-ca-certificates
# Create appuser
RUN adduser -D -g '' appuser
WORKDIR /usr/src/app
COPY . .
RUN go mod download
RUN go mod verify
WORKDIR /usr/src/app/cmd/web
# Build the binary static link
RUN make docker
############################
# STEP 2 build a small image
############################
FROM scratch
WORKDIR /
# Import from builder.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
# Copy our static executable and resources
COPY --from=builder /usr/src/app/cmd/web/web /web
COPY --from=builder /usr/src/app/cmd/web/views /views
COPY --from=builder /usr/src/app/cmd/web/static /static
# Use an unprivileged user.
USER appuser
ENTRYPOINT ["./web"]
CMD [":443"]

Did you forgot to map volume to /var/log/ ?
app:
container_name: app
image: app
restart: always
depends_on:
- postgres
networks:
- app
- web
environment:
- xxx_USERNAME=xxxxx
- xxx_PASSWORD=xxxxx
- xxx_HOST=xxxxx
- xxx_NAME=xxxxx
- xxx_DEBUG=1
- xxx_CONSOLELOGGING_LOG=TRUE
- xxx_ENCODELOGJSON_LOG=TRUE
- xxx_FILELOGGING_LOG=FALSE
- xxx_DIRECTORY_LOG=/var/log/app
- xxx_FILENAME_LOG=file.log
- xxx_MAXSIZE_LOG=100
- xxx_MAXBACKUPS_LOG=5
- xxx_MAXAGE_LOG=30
ports:
- 443:443
labels:
- #TRAEFIK CONFIG
volumes:
- ./:/var/log/
See the last line of docker compose snippet...
FROM scratch is empty... and you not creating new directory it with provided instructions. Add settings to map directory to your container.

Related

dial tcp 127.0.0.1:8080: connect: connection refused. go docker app

I have two apps in go language. user_management app, which I run (docker-compose up --build) first, then I run(docker-compose up --build) sport_app. sport_app is dependent from user_management app.
sport_app Dockerfile file as below.
FROM golang:alpine
RUN apk update && apk upgrade && apk add --no-cache bash git openssh curl
WORKDIR /go-sports-entities-hierarchy
COPY . /go-sports-entities-hierarchy/
RUN rm -rf /go-sports-entities-hierarchy/.env
RUN go mod download
RUN chmod +x /go-sports-entities-hierarchy/scripts/*
RUN ./scripts/build.sh
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
ENV GIN_MODE="debug" \
GQL_SERVER_HOST="localhost" \
GQL_SERVER_PORT=7777 \
ALLOWED_ORIGINS=* \
USER_MANAGEMENT_SERVER_URL="http://localhost:8080/user/me" \
# GQLGen config
GQL_SERVER_GRAPHQL_PATH="graphql" \
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED=true \
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH="playground" \
# Export necessary port
EXPOSE 7777
CMD /wait && ./scripts/run.sh
sport_app docker-compose.yml file as below.
version: '3'
volumes:
postgres_data:
driver: local
services:
go-sports-entities-hierarchy:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
WAIT_HOSTS: postgres:5432
# Web framework config
GIN_MODE: debug
GQL_SERVER_HOST: go-sports-entities-hierarchy
GQL_SERVER_PORT: 7777
ALLOWED_ORIGINS: "*"
USER_MANAGEMENT_SERVER_URL: http://localhost:8080/user/me
# GQLGen config
GQL_SERVER_GRAPHQL_PATH: graphql
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
ports:
- 7777:7777
depends_on:
- postgres
- redisearch
go-sports-events-workflow:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
WAIT_HOSTS: postgres:5432
# Web framework config
GIN_MODE: debug
GQL_SERVER_HOST: go-sports-events-workflow
GQL_SERVER_PORT: 7778
ALLOWED_ORIGINS: "*"
# GQLGen config
GQL_SERVER_GRAPHQL_PATH: graphql
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
depends_on:
- postgres
- redisearch
- go-sports-entities-hierarchy
user_management app Dockerfile as below:
FROM golang:alpine
RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Move to working directory /build
WORKDIR /build
# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download
# Copy the code into the container
COPY . .
# Build the application
RUN go build -o main .
# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist
# Copy binary from build to main folder
RUN cp -r /build/html .
RUN cp /build/main .
# Environment Variables
ENV DB_HOST="127.0.0.1" \
APP_PROTOCOL="http" \
APP_HOST="localhost" \
APP_PORT=8080 \
ALLOWED_ORIGINS="*"
# Export necessary port
EXPOSE 8080
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
# Command to run when starting the container
CMD /wait && /dist/main
user_management app docker-compose.yml file as below:
version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- 5432:5432
go-user-management:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
# Postgres Details
DB_PORT: 5432
# APP details
APP_PROTOCOL: http
APP_HOST: localhost
APP_PORT: 8080
# System Configuration Details
ALLOWED_ORIGINS: "*"
ports:
- 8080:8080
depends_on:
- postgres
In sport_app I write below code and get error:
client := resty.New()
resp, err := client.R().SetHeader("Content-Type", "application/json").SetHeader("Authorization", "Bearer "+token).Get("http://localhost:8080/user/me")
Error is: Get "http://localhost:8080/user/me": dial tcp 127.0.0.1:8080: connect: connection refused:"
This API(http://localhost:8080/user/me) is written in the user_management app and this is working, I check with the postman.
I already read this question answers, but can not solve my problem.
I am new to docker, please help.
For communicating between multiple docker-compose clients, you need to make sure that the containers you want to talk to each other are on the same network.
For example, (edited for brevity) here you have one of the docker-compose.yml
# sport_app docker-compose.yml
version: '3'
services:
go-sports-entities-hierarchy:
...
networks:
- some-net
go-sports-events-workflow
...
networks:
- some-net
networks:
some-net:
driver: bridge
And the other docker-compose.yml
# user_management app docker-compose.yml
version: '3'
services:
postgres:
...
networks:
- some-net
go-user-management
...
networks:
- some-net
networks:
some-net:
external: true
Note: Your app’s network is given a name based on the project name, which is based on the name of the directory it lives in, in this case a prefix user_ was added.
They can then talk to each other using the service name, i.e. go-user-management, etc.
You can, after running the docker-compose up --build commands, run the docker network ls command to see it, then docker network inspect bridge, etc.

Docker - correct approach for cms development

I am currently setting up docker for development purpose.
The goal would be to easily setup mysql, apache, php, and the cms (Omeka).
I managed to rewrite a dockerfile and dockercompose that automatically setup the cms for a normal user, but i need to be able to access the CMS source code i wget'd during build.
From my understanding, volumes/binds aren't the solution because they only access files added after build (i tried anyway and couldn't access the files).
Should i write a script the run the compose routine, and then download the files and do the things necessary for installing the cms, or is there a more docker approach to this ?
Here is my docker-compose :
version: '3'
services:
db:
image: mysql:latest
environment:
//some vars. irrelevant
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- db
ports:
- 8081:80
omeka:
build: .
depends_on:
- db
ports:
- "8080:80"
environment:
//some vars. Irrelevant
volumes:
- type: bind
source: ./www/
target: /var/www/
volumes:
www:
And my Dockerfile :
FROM php:8.0.3-apache
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y --no-install-recommends \
[some packages]
RUN docker-php-ext-install exif mysqli
# Install omeka
WORKDIR /var/www
RUN git clone --recursive https://github.com/omeka/Omeka.git
RUN chown -R root.www-data Omeka && chmod 775 Omeka
WORKDIR /var/www/Omeka
[....]
# Copy some files, run some commands, set some rights
# Configure apache
COPY omeka.conf /etc/apache2/sites-available/omeka.conf
[....]
# Configure php
[...]
# Add init script
COPY run.sh /run.sh
RUN dos2unix "/run.sh"
RUN chmod 755 /*.sh
EXPOSE 443
CMD ["/run.sh"]
Running this (i removed some vars, and of course config files are lacking for clarity purposes) gives me a www folder that stays empty.
I don't quite understand how i could do this, as i don't exactly know how the files are persistent or not when re-running docker-compose p -d
This is my first docker file as well.
Thank you

ENV variables not coming through godotenv 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.

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"]

How to create docker compose for hybris commerce

I would like to run hybris with the docker.
I am trying to create a docker to run hybris.
Can anyone help me with this?
This is my code:
UPDATE Question:
When I enter the container (ubuntu) and try to build hybris, there is always an error.
I created the docker-compose and the dockerfile. However, when I run ant clean all in the container the build always fails.
[![![enter image description here][1]][1]
version: '3.3'
services:
db:
image: mysql:5.6
volumes:
- //C/dockerVolumes/db_local_hybris:/var/lib/mysql
container_name: mysql_hybris
hostname: mysql_hybris
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
hybris:
image: miltex/jdk:hybrisDevs
build:
context: .
container_name: hybris_dev
hostname: hybris_dev
ports:
- "8009:8009"
- "8010:8010"
- "9001:9001"
- "9002:9002"
- "1099:1099"
tty: true
links:
- db
Dockerfile
FROM miltex/jdk:1.8
#update
RUN apt-get update
## Run Initial Ant ##
RUN mkdir -p /app/hybris_dev
COPY ./hybris/HYBRISCOMM6700P_10-80003492.ZIP /app/hybris_dev/
RUN cd /app/hybris_dev && unzip HYBRISCOMM6700P_10-80003492.ZIP
#COPY ./bin/hybris-wrapper.sh /app/hybris_dev/hybris/bin/platform/
RUN mkdir /app/hybris_dev/hybris/bin/custom
COPY ./src/custom /app/hybris_dev/hybris/bin/custom
RUN cd /app/hybris_dev/installer && rm -R recipes
RUN mkdir /app/hybris_dev/installer/recipes
COPY ./src/custom/recipes /app/hybris_dev/installer/recipes
#CMD /app/hybris_dev/intaller/install.sh -r local setup ; /app/hybris_dev/hybris/bin/custom/platform/setantenv.sh ; ant clean all
## Copy hybris-wrapper to configure template properties at runtime ##
COPY ./bin/hybris-wrapper.sh /hybris-wrapper.sh
## Expose AJP S-AJP HTTP HTTPS RMI ports ##
EXPOSE 8009 8010 9001 9002 1099
RUN chmod -R 777 /app
#RUN chmod +x /hybris-wrapper.sh
# Run Hybris server
#ENTRYPOINT ["/hybris-wrapper.sh"]
[1]: https://i.stack.imgur.com/uoK3E.png
The mistake was the image I was using before. Now this dockerhub miltex:jdk:1.8 image is working perfectly.

Resources