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"]
Related
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"
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.
Hello i tried to build docker-compose in my project with these structure file:
app/
-front-end/src/Components
-back-end/images
but when i run build i have these error with img relative url:
frontend_1 | Module not found: Can't resolve '../../../../../back-end/images'
And these is my docker-compose file:
version: '2'
services:
backend:
network_mode: host
build: ./back-end/
ports:
- "6200:6200"
volumes:
- ./back-end:/usr/src/app
frontend:
build: ./front-end/
ports:
- "3000:3000"
volumes:
- ./front-end:/usr/src/app
depends_on:
- backend
My frontend Dockerfile:
FROM node:10.15.3
RUN mkdir -p /usr/src/app
WORKDIR /TuKanasta
EXPOSE 3000
CMD ["npm", "start"]
the backend Dockerfile:
FROM node:10.15.3
RUN mkdir -p /usr/src/app
WORKDIR /TuKanasta
RUN npm install -g nodemon
EXPOSE 4000
CMD [ "npm", "start" ]
Note: My project run 100 % without docker.
volumes:
- ./back-end:/usr/src/app
...
volumes:
- ./front-end:/usr/src/app
If set in the same image, the second bind mount volume would overwrite the first /usr/src/app content, as illustrated in gladiusio/gladius-archive-node issue 4.
If set in two different images, /usr/src/app in frontend1 would not be able to see back-end, copied in /usr/src/app separate volume of backend service.
Declaring the volume as external might help, as illustrated in this thread.
Or copying into an existing volume (shown here)
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.
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.