Docker volume mapping to current working directory not work - docker

Docker version 20.10.21
docker run command with -v option works as expected when the destination path is other than /app. But when the destination path is /app it doesn't work as expected.
command works as expected:
docker run -d -v ${pwd}:/app2 react-app
command not works as expected:
docker run -d -v ${pwd}:/app react-app
as seen in the snapshot there is not port for the second container
here is Dockerfile content
FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
RUN mkdir data
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=http://api.myapp.com/
EXPOSE 3000
CMD [ "npm", "start" ]

You are running npm install in /app in the Dockerfile, but then at runtime you are mounting pwd over the files you installed in /app during the build process. Don't install your dependencies in /app during the build if you want to mount to /app at runtime.

Please try using $(pwd) instead of ${pwd}. Also if you are running it under Windows then you probably need to use some shell which implements pwd command correctly. E.g. Git Bash.
docker run -d -v $(pwd):/app react-app
Also once you start the container please check docker container inspect <container ID>, specifically Mounts section.
Or you can filter the output:
docker container inspect <container ID> -f '{{ .Mounts }}'
Also if you see that container exits immediately, please check its logs with
docker logs <container ID>

I solved it by excluding the node_modules from the mounting as:
docker run -d -v ${pwd}:/app -v /app/node_modules react-app

Related

docker container stops after docker run

I have a docker file which when built and run stops. I am trying to run both client and server in one docker container. If there is any solution to use docker-compose, then that is already in place and working fine. Please advise how to keep the container up and running using docker run. Thanks!
Here is my docker file, package.json and screenshot of folder structure.
DockerFile contents:
FROM node:14.14.0-alpine
RUN apk update && apk add bash
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
WORKDIR /app
EXPOSE 3000
EXPOSE 4565
CMD ["npm","run","prebuild"]
docker build: command:
docker build -t sample .
docker run command:
docker run -d -it --name sm -v `pwd`:/app sample
Package.json:

Astro in Docker not refresh

I am creating an Astro js container with Docker on windows.
Dockerfile
FROM node:18-alpine3.15
RUN mkdir app
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 24678
CMD ["npm","run","dev","--","--host"]
I build my image with the following command
docker build . -t astro
I run my container with this command
docker run --name astro1 -p 24678:24678 -v D:\Workspace\Docker\Practicas\docker-astro-example:/app -v /app/node_modules/ astro
So far without problems but when I make a change in the index.astro document it does not refresh the page to see the changes.

Copy files from container to local in Docker

I want to copy a file from container to my local. The file is generated after execute python script, but due to then ENTRYPOINT, the container exited right after it run, and cant be able to use docker cp command. Any idea on how to prevent the container from exit before manage to copy the file? Below is my Dockerfile:
FROM python:3.9-alpine3.12
WORKDIR /app
COPY . /app/
RUN pip install --no-cache-dir -r requirements.txt && \
rm -f /var/cache/apk/*
ENTRYPOINT ["python3", "main.py"]
I use this command to run the image:
docker run -d -it --name test [image]
If the output file is stored in it's own directory (say /app/output) you can run: docker run -d -it -v $PWD/output:/app/output/ --name test [image] and the file will be in the output directory of the current directory.
If it's not, then run the container with: docker run -d -it --name test [image]
Then copy the file to your own filesystem using docker cp test:/app/example.json . to copy it to the current directory.
If running a container in background is unnecessary then you can copy a file from stdout
docker run -it [image] cat /app/example.json > out_example.json

docker is not in the `container ls` or `ps` after docker run

I am running a docker container for a react application.
frontend.Dockerfile
FROM node:14 as builder
RUN mkdir -p /client
WORKDIR /client
COPY package.json /client
RUN npm install
COPY . /client
# copying the crt, key files for https
# COPY ../secret/website_com_au.key ../secret/website_com_au_chain.crt /client
RUN npm run build
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /client/build /usr/share/nginx/html
EXPOSE 80
RUN chown nginx.nginx /usr/share/nginx/html/ -R
I am building the dockerfile without any error.
sudo docker build -t nabil/website:webclient . -f frontend.Dockerfile
There is also no error after docker run,
sudo docker run -d --net=host nabil/website:webclient
But when I run docker ps or docker container ls, I don't see the docker container there.
docker ps -a to see all containers even container was dead.
Check docker logs container_id to see the reason why container was dead.
Check the status of container created:
docker ps -a (OR)
docker container ls -a
Check container logs for failing:
docker logs <container id/container name> (OR)
docker container logs <container id/container name>

Container is exiting on its own & not able to exec into it

I am trying to build and container image & then trying to run the enter the container after running it. But I am getting error response from daemon.
My Docker file -
COPY . /app
RUN sudo chmod 777 -R /app
WORKDIR /app
ADD entry_point.sh /opt/bin/
RUN sudo chmod 777 /opt/bin/entry_point.sh
COPY start-selenium-standalone.sh /opt/bin/start-selenium-standalone.sh
RUN sudo chmod 777 /opt/bin/start-selenium-standalone.sh
EXPOSE 4444 5900 9515
**Command to build docker image**
docker build -f Docker/Dockerfile -t sel-test:1 .
**Command to run the image**
docker run -d -p 4444:4444 -p 5900:5900 -v /dev/shm:/dev/shm sel-test:1
**Error I am getting -**
Error response from daemon: Container a9e0bb7f381584dd5e39dcd997640233835408ffdfe4e0e44108ddb7bb393cd0 is not running
Your container is exiting because there is nothing to run inside the container.
To see this, run the docker ps -a command and check the status of your container.
In order to run something inside the container use CMD in docker file to run bash inside the container whenever you use 'docker run'.

Resources