Docker for Windows - Volumes not working correctly - docker

I am starting to work with Docker for Windows, but I can't make volumes to work with docker-compose.
First, I've created a simple Dockerfile:
FROM node:latest
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install nodemon -g
Then, a docker-compose.yml:
version: '3'
services:
nodeServer:
build: .
volumes:
- './:/usr/src/app'
command: bash -c "npm run start"
When a volume is declared in the `docker-compose.yml´, it doesn't work.
But, when I try to bind a volume through the command line like this:
docker build .
docker run -it -v ${PWD}:/usr/src/app d0d9397e9194 bash
It works. I can't understand the difference between these two approaches.
I've checked more than once my configurations:

Related

Dockercompose run command on mounted directory

I am trying to create an editing environment for my project - I want to run poetry update on the code repository in the container when the container starts instead of manually having to do it.
When running docker-compose build editor-environment but obviously if we haven't run docker-compose up editor-environment the /code directory has not been mounted. How could I accomplish something like this?
Docker-Compose.yml
version: '3.6'
services:
editor-environment:
build:
dockerfile: env_docker/Dockerfile
volumes:
- .:/code
tty: true
Dockerfile
FROM python:3.10.4-slim-buster
RUN pip install poetry
RUN cd /code --> throws error, can't cd to /code
RUN poetry updates
ENTRYPOINT ["/bin/sh -c"]
CMD ["bash"]

Running docker-compose container from docker hub

I have created 2 containers locally and pushed them into docker hub to use on a VM, one is an Angular App and the other is a Django REST API with a db:
Angular App
Dockerfile
FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install npm#8.11.0 --legacy-peer-deps
RUN npm run build --prod
FROM nginx:alpine
COPY --from=node /app/dist/bom-e-barato /usr/share/nginx/html
I created the image by doing docker build -t andreclerigo/bom_e_barato:latest . then pushed it with docker push andreclerigo/bom_e_barato:latest and then I can run it on my VM by doing docker run -d -p 80:80 andreclerigo/bom_e_barato:latest
Django REST API
Dockerfile
FROM python:3.8.10
ENV PYTHONUNBUFFERED 1
RUN mkdir /rest-api-scraper
WORKDIR /rest-api-scraper
ADD . /rest-api-scraper/
RUN pip install -r requirements.txt
docker-compose.yml
version: '3'
services:
web:
build: .
command: bash -c "python backend/manage.py makemigrations && python backend/manage.py migrate && python backend/manage.py runserver 0.0.0.0:8000"
container_name: rest-api-scraper
volumes:
- .:/rest-api-scraper
ports:
- "8000:8000"
image: andreclerigo/rest-api-scraper:latest
I created the image by doing docker-compose build, then I pushed it to docker hub by doing docker-compose push to run locally I can do docker-compose up
Question
What steps do I need to take to pull this image and run the the image (docker-compose up) on my VM?

Adding Volume to docker container's /app Issue

I am having trouble creating a volume that maps to the directory "/app" in my container
This is basically so when I update the code I don't need to build the container again
This is my docker file
# stage 1
FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod
# stage 2
FROM nginx:alpine
COPY --from=node /app/dist/my-first-app /usr/share/nginx/html
I use this command to run the container
docker run -d -p 100:80/tcp -v ${PWD}/app:/app docker-testing:v1
and no volume gets linked to it.
However, if I were to do this
docker run -d -p 100:80/tcp -v ${PWD} docker-testing:v1
I do get a volume at least
Anything obvious that I am doing wrong?
Thanks
The ${PWD}:/app:/app should be ${PWD}/app:/app.
If you explode ${PWD}, you'd obtain something like /home/user/src/thingy:/app:/app which does not make much sense.
EDIT:
I'd suggest using docker-compose to avoid this kind of issues (it also simplify a lot the commands to start up docker).
In your case the docker-compose.yml would look like this:
docker run -d -p 100:80/tcp -v ${PWD}:/app:/app docker-testing:v1
version: "3"
services:
doctesting:
build: .
image: docker-testing:v1
volumes:
- "./app:/app"
ports:
- "100:80"
I didn't really test if it works, there might be typos...

Docker container port is not made public

docker-compose.yml
version: '3.3'
services:
apps:
build:
context: .
dockerfile: ./Dockerfile
ports:
- "3000:3000"
restart: always
Dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
COPY . /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3000
RUN npm install
RUN npm start
Only port expose is seen here but the internal port 3000 is not made public
Versions:
Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01
I don't get it! You used docker-compose and created apps service, but your container has a random name crancky villani!!!
go into the path your docker-compose.yml placed and run this command:
docker-compose down; docker-compose up -d --build
Then check the results again.
Docker image was not properly built since the last step to start node is defined incorrectly.
it is supposed to be like this,
CMD ["npm", "start"]

Docker Image Contains files that Docker Container doesn't

I have a Dockerfile that contains steps that create a directory and runs an angular build script outputing to that directory. This all seems to run correctly. However when the container runs, the built files and directory are not there.
If I run a shell in the image:
docker run -it pnb_web sh
# cd /code/static
# ls
assets favicon.ico index.html main.js main.js.map polyfills.js polyfills.js.map runtime.js runtime.js.map styles.js styles.js.map vendor.js vendor.js.map
If I exec a shell in the container:
docker exec -it ea23c7d30333 sh
# cd /code/static
sh: 1: cd: can't cd to /code/static
# cd /code
# ls
Dockerfile api docker-compose.yml frontend manage.py mysite.log pnb profiles requirements.txt settings.ini web_variables.env
david#lightning:~/Projects/pnb$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea23c7d30333 pnb_web "python3 manage.py r…" 13 seconds ago Up 13 seconds 0.0.0.0:8000->8000/tcp pnb_web_1_267d3a69ec52
This is my dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt install nodejs
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN mkdir /code/static
WORKDIR /code/frontend
RUN npm install -g #angular/cli
RUN npm install
RUN ng build --outputPath=/code/static
and associated docker-compose:
version: '3'
services:
db:
image: postgres
web:
build:
context: .
dockerfile: Dockerfile
working_dir: /code
env_file:
- web_variables.env
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
In the second example, the static directory has never been created or built into. I thought that a container is an instance of an image. How can the container be missing files from the image?
You're confusing build-time and run-time, along playing with Volumes.
Remember that host mount has priority over FS provided by the running container, so even your built image has assets, they are going to be overwritten by .services.web.volumes because you're mounting the host filesystem that overwrites the build result.
If you try to avoid volumes mounting you'll notice that everything is working as expected.

Resources