I have a Dockerfile that runs perfectly on it's own.
But when I run it from docker-compose up --build
I get: $GOPATH/go.mod exists but should not
Below is a snippet of terminal output.
Successfully tagged app_app:latest
Starting golang_db ... done
Starting golang_app ... done
Attaching to golang_db, golang_app
golang_app | $GOPATH/go.mod exists but should not
.
.
golang_db | Version: '5.6.48' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
golang_app exited with code 1
Any suggestions?
#docker-compose.yml
version: '3'
services:
db:
build:
context: ./MySQL
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: docker
MYSQL_PASSWORD: docker
container_name: golang_db
ports:
- "3306:3306"
tty: true
app:
build:
context: ./Go
volumes:
- "./Go:/go"
container_name: golang_app
ports:
- "9000:9000"
tty: true
depends_on:
- db
#Go/Dockerfile
FROM golang:alpine AS builder
ENV GO111MODULE=on
RUN mkdir /app
ADD . /app/
WORKDIR /app
COPY ./structs.go .
COPY ./handlers.go .
COPY ./server.go .
COPY ./favicon.ico .
COPY ./assets /assets
RUN go mod init stuff.com
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(ls -1 *.go)
EXPOSE 9000
CMD ["go", "run", "."]
#MySQL/Dockerfile
FROM mysql:5.6
COPY test.sql /docker-entrypoint-initdb.d/test.sql
In the root of your GOPATH no packages are expected. The expectation is usually to have a subdirectory src/github.com/someproject/somerepo, which is most likely the root cause of the warning
I sorted it out, the Dockerfile above has been updated.
Related
My Dockerfile:
FROM node:14-alpine
RUN mkdir /home/app
COPY . /home/app
WORKDIR /home/app
RUN yarn && yarn build
CMD ["yarn", "start:prod"]
My docker-compose.yml:
version: '3.7'
services:
db:
image: postgres:latest
restart: always
env_file:
- .env
volumes:
- $PWD/data/postgres:/var/lib/postgresql/data
networks:
host:
backend:
build: ./
restart: always
depends_on:
- db
networks:
host:
volumes:
db:
I run docker-compose build and got error:
error An unexpected error occurred: "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com".
if i run docker build --network=host -t back . it work, but i need a docker-compose.
How i can set --network=host for docker-compose?
i tried
network_mode: "host"
but it didn't help
I created this docker-compose file based on my two images (front and back-end), both are running ok when I run docker-compose up.
The problem is that I want to make hot reload work properly for my frontend which is built upon react.
I tried many things but the properties volumes and build do not seem to be able to find my directories.
So, the directory for both projects are:
C:\Users\pwdie\Downloads\projects\typescript-api (api)
C:\Users\pwdie\Downloads\projects\frontend-api-consumer (front)
being "projects" for the root for both of them, and frontend-api-consumer containing the docker-compose.yml as the image shows.
Didn't understand that part of the docker-compose system, explanations will be well received.
Front Dockerfile
FROM node:17
WORKDIR /app
COPY package.json ./
RUN yarn
COPY ./ ./
EXPOSE 3000:3000
CMD ["yarn", "dev"]
Api Dockerfile
FROM node:17
WORKDIR /app
COPY package.json ./
RUN yarn
COPY . .
ENV DB_HOST=host.docker.internal
EXPOSE 5000:5000
CMD ["yarn", "dev"]
Docker-compose.yml
version: "3.8"
services:
api:
image: api
build: ../typescript_api
volumes:
- ../typescript_api:/var/app
environment:
DB_USER: postgres
DB_PASSWORD: postgres
DB_HOST: host.docker.internal
DB_PORT: 5432
DB_NAME: typescript_api
ACCESS_TOKEN_SECRET: 1c6c3296f699e051220674d329e040dee0abae986f62d62eb89f55dfa95bff1ac9b52731177e664e25bff9b6ce0eba6ec7a9b6cf7d03e94487dc03179dc31c7e
ports:
- "5000:5000"
front:
image: front
build: .
volumes:
- ./:/var/app
environment:
REACT_APP_MAPBOX_TOKEN: pk.eyJ1Ijoic291c2FkaWVnbzExIiwiYSI6ImNrdHEwbDRrdTBycTEycXBtbXZ5eXEzMm4ifQ.U58H7S1um_WcRC2rBoEuNw
REACT_APP_API_URL: http://localhost:5000
depends_on:
- api
ports:
- "3000:3000"
I am very new to Docker so please forgive me, but I have a working dockerfile and docker-compose when the Main.go is at root-level but in this project the app will break if I put main.go at root.
File Structure
queue-backend
- .idea
- cmd
- appCode
- handler.go
- helper.go
- main.go
- routes.go
- pkg
- forms
- models
- mongodb
- models.go
- tmp
.gitignore
docker-compose.yml
Dockerfile
go.mod
README.md
db
extensions
Anyway... my dockerfile looks like this
FROM golang:1.16
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY ../.. .
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
CMD ["air"]
Docker-compose.yml looks like
version: '3.9'
services:
backend:
build: ""
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- mongodb_container
mongodb_container:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: queue-delivery
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
I have tried to set WORKDIR to /app/cmd/appCode or /cmd/appCode and matching the same in the docker-compose to .:/app/cmd/appCode and .:/cmd/appCode which none work, it always returns this, or the stated paths above instead of just the '/app' path
backend_1 | no Go files in /app
backend_1 | failed to build, error: exit status 1
At this point, I'm not sure what else to try...
To resolve Dockerfile in docker-compose.yml you need to change the build section as below
version: '3.9'
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- mongodb_container
mongodb_container:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: queue-delivery
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
Your Dockerfile has some issues,
FROM golang:1.16
WORKDIR /app
# File changes must be added at the very end, to avoid the installation of dependencies again and again
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
COPY go.mod .
COPY go.sum . # can not find this file in the directory structure
RUN go mod download
COPY ../.. . # doesn't make sense, just use COPY . .
CMD ["air"]
I have built a MEAN stack application with nginx front end.
I have 2 docker files - one for front end and one for back end
And I have a docker-compose file that pulls them together along with the database
This works great on my development machine
I then push the images to my dockerhub site
On my production ubuntu machine I pull the images that I want from my dockerhub repository
But how should I run them?
I transfer my docker-compose file to the server and try to run it:
docker-compose -f docker-compose.prod.yml up
but it complains that the folder structure isnt what I have on my dev machine:
ERROR: build path /home/demo/api either does not exist, is not accessible, or is not a valid URL.
I dont want to put all the code on the server and rebuild it.. surely that defeats the purpose of using dockerhub images?
I also need the docker compose file to pull in the .prod.env file for database credentials etc.
I know Im missing something here.
How do I run my images without rebuilding them from scratch?
Do I need another service for this?
Thanks in advance
docker-compose.prod.yml:
version: '3'
services:
# Database
database:
env_file:
- .prod.env
image: mongo
restart: always
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: $DB_ADMIN_PASSWORD
# Create a new database. Please note, the
# /docker-entrypoint-initdb.d/init.js has to be executed
# in order for the database to be created
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
DB_NAME: $DB_NAME
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
MONGO_INITDB_DATABASE: $DB_NAME
volumes:
# Add the db-init.js file to the Mongo DB container
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
- /data/db
ports:
- '27017-27019:27017-27019'
networks:
- backend-net
# Database management
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: $MONGO_INITDB_ROOT_PASSWORD
ME_CONFIG_MONGODB_SERVER: database
depends_on:
- database
networks:
- backend-net
# Nodejs API
backend:
depends_on:
- database
env_file:
- .prod.env
build:
context: ./api
dockerfile: Dockerfile-PROD-API
# Note: put this container name into proxy.conf.json for local angular CLI development instead of localhost
container_name: node-api-prod
networks:
- backend-net
# Nginx and compiled angular app
frontend:
build:
context: ./ui
dockerfile: Dockerfile-PROD-UI
ports:
- "8180:80"
container_name: nginx-ui-prod
networks:
- backend-net
networks:
backend-net:
driver: bridge
DOCKERFILE-PROD-API:
#SERVER ========================================
FROM node:10-alpine as server
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#RUN ls -lha
EXPOSE 3000
CMD ["npm", "run", "start"]
DOCKERFILE-PROD-UI:
#APP ========================================
FROM node:10-alpine as build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install #angular/cli && npm install
COPY . .
RUN npm run build
#RUN ls -lha
#FINAL ========================================
FROM nginx:1.18.0-alpine
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
Using full image names including dockerhub path resolved the issue for me.
Working solution shown below:
Dockerfile-PROD-UI
#GET ANGULAR ========================================
FROM node:10-alpine as base
WORKDIR /usr/src/app
COPY ui/package*.json ./
RUN npm install #angular/cli && npm install
COPY ui/. .
#BUILD ANGULAR ========================================
FROM base as build
RUN npm run build
#RUN ls -lha
#NGINX ========================================
FROM nginx:1.18.0-alpine
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
Dockerfile-PROD-API
#SERVER ========================================
FROM node:10-alpine as server
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#RUN ls -lha
EXPOSE 3000
CMD ["npm", "run", "start"]
docker-compose.yml
version: '3.5'
services:
# Database
database:
image: mongo
restart: always
env_file:
- .prod.env
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: $DB_ADMIN_PASSWORD
# Create a new database. Please note, the
# /docker-entrypoint-initdb.d/init.js has to be executed
# in order for the database to be created
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
DB_NAME: $DB_NAME
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
MONGO_INITDB_DATABASE: $DB_NAME
volumes:
# Add the db-init.js file to the Mongo DB container
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
- db-data:/data/db
ports:
- '27017-27019:27017-27019'
networks:
- backend-net
# Nodejs API
backend:
image: DOCKERHUBHUSER/DOCKERHUB_REPO:prod-backend
restart: always
depends_on:
- database
env_file:
- .prod.env
build:
context: ./api
dockerfile: Dockerfile-PROD-API
container_name: backend
networks:
- backend-net
# Nginx and compiled angular app
frontend:
image: DOCKERHUBHUSER/DOCKERHUB_REPO:prod-frontend
restart: always
depends_on:
- backend
build:
context: .
dockerfile: Dockerfile-PROD-UI
ports:
- "8180:80"
container_name: frontend
networks:
- backend-net
networks:
backend-net:
driver: bridge
volumes:
db-data:
name: db-data
external: true
docker-compose up --build becomes very slow using cloud docker-machine and after one hour i have and error of a not found entrypoint.sh file.
On local Mac docker-machine same config work fine.
My docker file
FROM python:3.5.2
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD app/ /code/
ADD static/ /code/
ADD entrypoint.sh /code/
my docker compose
version: '3.7'
services:
web:
build: .
command: gunicorn --bind 0.0.0.0:8000 app.wsgi
volumes:
- .:/code
entrypoint: ./entrypoint.sh
expose:
- "80"
nginx:
image: nginx:1.15.5
restart: always
ports:
- "80:80"
volumes:
- ./static:/static
depends_on:
- web
commands I run
cd myprojectfolder
eval $(docker-machine env [my-cloud-machine-name])
docker-compose -f docker-compose.yml -f envs/prd/prd.yml up --build -d
error i have after one hour (my project files are only 40mb)
ERROR: for web Cannot start service web: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"./entrypoint.sh\": stat ./entrypoint.sh: no such file or directory": unknown
ERROR: compose.cli.main.main: Encountered errors while bringing up the project.
Please help me
Thanks
I changed as suggested to this configuration but nothing changed. I'm stuck on this:
docker.api.build._set_auth_headers: Sending auth config ()
this is my new config
docker compose
version: '3.7'
services:
web:
command: gunicorn --bind 0.0.0.0:8000 app.wsgi
entrypoint: ./entrypoint.sh
expose:
- "80"
nginx:
image: nginx:1.15.5
restart: always
ports:
- "80:80"
volumes:
- ./static:/static
depends_on:
- web
my prd.yml file
version: '3.7'
services:
web:
build:
context: .
dockerfile: ./envs/prd/Dockerfile
nginx:
volumes:
- ./envs/prd/nginx/nginx.conf /etc/nginx/nginx.conf
my prd dockerfile
FROM python:3.5.2
ENV PYTHONUNBUFFERED 1
WORKDIR /
ADD requirements.txt .
RUN pip install -r requirements.txt
ADD app/ ./app
ADD static/ ./static
ADD envs/prd/settings.py /app/settings.py
ADD entrypoint.sh .
my .dockerignore
.DS_Store
.dockerignore
.git/
.gitignore
README.rst
README.md
*.pyc
__pycache__
.idea/*
Remove the volume declarations from your docker-compose and instead copy all relevant files in during the build stage (in your Dockerfile). For example in your web service remove volumes: .:/code and add COPY * /code to its Dockerfile.