Dockerfile entrypoint conflicts with docker-compose context - docker

I have a docker setup that does not have the Dockerfile or docker-compose at the root because there are many services.
build
client.Dockerfile
deployments
docker-compose.yml
web
core
scripts
run.sh
docker-compose
version: "3.1"
services:
client:
build:
context: ..
dockerfile: ./build/client.Dockerfile
volumes:
- ./web/core:/app
ports:
- 3000:3000
- 35729:35729
And then the dockerfile:
FROM node:10.11
ADD web/core/yarn.lock /yarn.lock
ADD web/core/package.json /package.json
ENV NODE_PATH=/node_modules
ENV PATH=$PATH:/node_modules/.bin
RUN yarn
WORKDIR /app
ADD web/core /app
EXPOSE 3000
EXPOSE 35729
RUN cat /app/scripts/run.sh
ENTRYPOINT ["/bin/bash", "/app/scripts/run.sh"]
CMD ["start"]
Now the RUN command displays the result of the file so it is there. However, when running docker-compose up the client_1 | /bin/bash: /app/scripts/run.sh: No such file or directory
I'm guessing it has something to do with the docker-compose context because when the dockerfile was at the root, it seemed to work fine.
I'm getting the feeling that docker is designed essentially to work only at the root.
Context:
I want a live reloading create-react-app server like this: https://www.peterbe.com/plog/how-to-create-react-app-with-docker.
I would like to setup my project this way: https://github.com/golang-standards/project-layout

Your volume is wrongly mounting. This should fix the issue. I created the similar folder structure. From the root folder of build ran docker-compose -f ./deployments/docker-compose.yml up. It works normally only thing i change volume path.
volumes:
- ../web/core:/app

Related

Flask App on Docker Image and Docker-compose.yml cannot import "app"

I tried to deploy a flask app project. When I directly docker-compose up --build is working but when I create the image and save&load to another place my same docker-compose.yml file cannot run properly. The error is cannot import "app".
working area:
Dockerfile
docker-compose.yml
src/app.py
not working era:
image.tar file(which is using for docker load)
docker-compose.yml
Dockerfile:
RUN mkdir /app
COPY . /app
WORKDIR /app/src
COPY requirements.txt /app/src/requirements.txt
RUN pip install -r requirements.txt
ENV FLASK_APP="app.py"
EXPOSE 5005
CMD python -u -m flask run --host=0.0.0.0
docker-compose.yml:
version: "3"
services:
app:
restart: unless-stopped
build: .
ports:
- "5005:5005"
volumes:
- .:/app
expose:
- 5005
src/app.py:
if __name__ == "__app__":
# start up api
app.run(port=5005, debug=True, host="0.0.0.0")
The result of docker-compose up on the not working era is Error:cannot import "app".
The docker-compose.yml file wouldn't work on a machine where the application code doesn't exist because of the volume mount defined here
volumes:
- .:/app
This volume mount will tell Docker to take whatever's in the . directory of the host machine and mount it to the /app directory on the container. In this case, on your machine where the app doesn't run, you mentioned that the application code doesn't exist in this directory, so it would make sense that the application doesn't run. If you remove the volume mount from the docker-compose.yml file on the second machine and try to run the container again, things should work normally as long as your application code is all copied over to the image correctly. You can read more about Docker volumes here.

NextJS Docker error: Couldn't find a `pages` directory. Please create one under the project root

Goal
Dockerize NextJS application
Problem
Docker compose up yields in the following error: Couldn't find a pages directory. Please create one under the project root.
Application
Files & folders
docker-compose.yml
web
.next
pages
public
.dockerignore
dockerfile
[more nextjs files & folders here]
docker-compose
version: '3'
services:
web:
build:
context: web
dockerfile: dockerfile
ports:
- "3000:3000"
container_name: rughood_web
dockerfile
FROM node:16
WORKDIR /web
COPY package*.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
.dockerignore
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.git
Note!
The NextJS application itself is working fine when I run npm run dev within the web directory (which invokes the script "dev": "next dev" in package.json). I only have the error when trying to dockerize it. Moreover, in the docker-compose I also initiate a Redis cache, which is working fine too. Therefore I conclude the error must be how I try to combine Docker and NextJS. Thank you very much in advance :)
Update 1
How I got there
Using the tips from #HansKilian and Exploring Docker container's file system I did the following:
Cd to the web directory
Built an image from the dockerfile docker build .
Explored the image with the following command docker run --rm -it --entrypoint=/bin/bash name-of-image
Once inside, execute ls or ls -lsa
This gave me the following results:
What's in the derived image
dockerfile
next-env.d.ts
next.config.js
node_modules
package-lock.json
package.json
pages
public
tsconfig.json
[Among other files/folders]
So the pages folder actually seems to be in the root of the container, yet still I get the error (pages is a directory in the container in which I can cd and -ls)
P.s. don't forget to delete your image if you're not going to using it anymore
Update 2
Building the image and running it from within the web directory actually works, so it might actually have something to do with the docker-compose?
Here is my working Dockerfile with nextjs:
FROM node:16.14.0
RUN npm install -g npm#8.5.5
RUN mkdir -p /app
WORKDIR /app
COPY package*.json /app
RUN npm i
COPY . /app
EXPOSE 3000
RUN npm run build
CMD ["npm", "run", "dev"]
And docker-compose.yml :
version: "3.7"
services:
project-name:
image: project-name
build:
context: .
dockerfile: Dockerfile
container_name: project-name
restart: always
volumes:
- ./:/app
- /app/node_modules
- /app/.next
ports:
- "3000:3000"
While I was trying every single line of code ever uploaded to the internet, I came back to my initial set-up (from the question) and suddenly it now does work. Source control confirming I didn't change a thing.
To be sure, I deleted all containers, images and volumes from Docker and ran docker compose up. Yet still it worked. Tried many things to recreated the error, but I couldn't. Thank you all for helping and hopefully this may be come to use for someone else!

Docker-compose builds app and copy's not inteded directory content specified by dockerfile

I'm trying to containerize two services an socket service and a django application
My file structure is
\main file {docker-compose file}
\ django application {Dockerfile}
\ socket app {Dockerfile}
When I run docker build . it build the image
then when I run docker-compose build,
I notice that the socket app and django app are copied to the container instead of only the django application as specified by the Dockerfile.
I get the idea that the Dockerfile is executed in the main directory instead of the django directory?
Here is Dockerfile that is inside the django app application
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt
# Copy project
COPY . /code/
RUN ls
And here is the docker-compose file.
With the usage of the ls command I tried to figure out what happend and the output is that the applications in the main folder are copied instead of the django application.
version: '3'
services:
db:
image: postgres:10.1-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
build: ./django_app
command: ls /code/
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
volumes:
postgres_data:
is this intended use or am I doing something wrong?
The volumes: directive in your docker-compose.yml file is hiding literally everything your Dockerfile does. You'll solve your immediate problem by changing the two directories to match: in the volumes: directive, bind-mount ./django_app:/code.
In a more production-oriented workflow, I'd recommend making your Docker image totally self-contained: make sure it has a CMD that runs your application, and do not use volumes: to inject your code. Delete command: and volumes: from the docker-compose.yml and let the image provide its own code and default command. (To do development, use a Python virtual environment for local code isolation, and make sure all of your tests and a basic hand-run workflow pass before using Docker for anything.)

create docker-compose file from Dockerfile

I have this simple Dockerfile:
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
I would like to use docker-compose so that I can simply say docker-compose up or docker-compose down.
I am struggling to find a simple docker-compose example of how I would use docker-compose, all I can find are examples like this which cover more ground than I need.
How could I create a simple docker-compose file from the above?
You write following in docker-compose.yml file to run your docker container using compose:
version: '3'
services:
app:
build: .
ports:
- 8080:8080
Docker compose file is made of multiple services but in your case it is enough to define one service. build option specifies from where to pick up the Dockerfile to build the image and ports will allow port forwarding from the container to you host OS.
To start the service you can use:
docker-compose up
And to stop the service:
docker-compose down
You can find more documentation about the compose file here

named docker volume not updating using docker-compose

I'm trying to have one service to build my client side and then share it to the server using a named volume. Every time I do a docker-compose up --build I want the client side to build and update the named volume clientapp:. How do I do that?
docker-compose.yml
version: '2'
volumes:
clientapp:
services:
database:
image: mongo:3.4
volumes:
- /data/db
- /var/lib/mongodb
- /var/log/mongodb
client:
build: ./client
volumes:
- clientapp:/usr/src/app/client
server:
build: ./server
ports:
- "3000:3000"
environment:
- DB_1_PORT_27017_TCP_ADDR=database
volumes:
- clientapp:/usr/src/app/client
depends_on:
- client
- database
client Dockerfile
FROM node:6
ENV NPM_CONFIG_LOGLEVEL warn
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
# builds my application into /client
CMD ["npm", "build"]
By definition, a volume is the persistent directories that docker won't touch other than to perform an initial creation when they are empty. If this is your code, it probably shouldn't be a volume.
With that said, you can:
Delete the volume between runs with docker-compose down -v and it will be recreated and initialized on the next docker-compose up -d.
Change your container startup scripts to copy the files from some other directory in the image to the volume location on startup.
Get rid of the volume and include the code directly in the image.
I'd recommend the latter.
Imagine you shared your src folder like this :
...
volumes:
- ./my_src:/path/to/docker/src
...
What worked for me is to chown the my_src folder :
chown $USER:$USER -R my_src
It turned out some files were created by root and couldn't be modified by docker.
Hope it helps !

Resources