Why vscode devcontainer volumes are hidden? - docker

I have simple devcontainer config:
devcontainer.json
{
"name": "devcontainer",
"build": {
"dockerfile": "Dockerfile"
},
}
Dockerfile
FROM python:3.11
WORKDIR /usr/src
ENTRYPOINT sleep infinity
I build image with command (using devcontainer cli) devcontainer build --workspace-folder . --image-name devcontainer
Next I create container with command docker create --name container-a and attach to it in vscode.
All file changes saves after container restart, therefor some volumes should exist (as I think), but docker inspect returns empty arr for container-a.
So how can I find these volumes?

Related

Application in Docker container does not save file to docker volume

I am using Docker desktop to develop my application. I have created a "uploadedfiles" docker volume and I am trying to save files to it, from my docker application.
When I save a file from my docker application, I see that the file is saved to the "uploadedfiles" folder on my docker container. I am therefore assuming that I am not binding my application container to the created volume in my Dockerfile.Is my assumption correct?
How can I bind my application container to the created volume in my Dockerfile?
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["InstaTranscribeServerSide/InstaTranscribeServerSide.csproj", "InstaTranscribeServerSide/"]
COPY ["Services/Services.csproj", "Services/"]
COPY ["DataAccess/DataAccess.csproj", "DataAccess/"]
RUN dotnet restore "InstaTranscribeServerSide/InstaTranscribeServerSide.csproj"
COPY . .
WORKDIR "/src/InstaTranscribeServerSide"
RUN dotnet build "InstaTranscribeServerSide.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "InstaTranscribeServerSide.csproj" -c Release -o /app/publish
FROM base AS final
VOLUME CREATE uploadedfiles
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "InstaTranscribeServerSide.dll"]
Docker Volume does not show uploaded files
Container shows that the volume was not "bound"
Container shows file was uploaded to "uploadedfiles" folder on container:
I have successfully mount /home/ folder of my host to the /mnt folder of the existing (not running) container. You can do it in the following way:
Open configuration file corresponding to the stopped container, which can be found at /var/lib/docker/containers/99d...1fb/config.v2.json (may be config.json for older versions of docker).
Find MountPoints section, which was empty in my case: "MountPoints":{}. Next replace the contents with something like this (you can copy proper contents from another container with proper settings):
"MountPoints":{"/mnt":{"Source":"/home/<user-name>","Destination":"/mnt","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/<user-name>","Target":"/mnt"},"SkipMountpointCreation":false}}
or the same (formatted):
"MountPoints": {
"/mnt": {
"Source": "/home/<user-name>",
"Destination": "/mnt",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/home/<user-name>",
"Target": "/mnt"
},
"SkipMountpointCreation": false
}
}
Restart the docker service: service docker restart
This works for me with Ubuntu 18.04.1 and Docker 18.09.0

Mount Volume for Docker Container Inside Linux Virtual Guest

I would really appreciate the help since I have been struggling for days...
I am trying to build a java application using a docker image on a VirtualBox guest (CentOS 7). After doing a git checkout into a directory on the guest Virtualbox, the docker image runs with the following command:
docker run -it --rm --volume $(pwd):/usr/build-app build-monster
As soon as the docker container image starts, it executes a shell script that changes directories into /usr/build-app. This is where the script fails with the following error:
/usr/scripts/docker_start.sh: 2: cd: can't cd to /usr/build-app
Everything else that then references this directory also fails.
Most of the howtos I read have to do with sharing a directory from the Virtualbox Host, which I am not trying to do. When I do a docker inspect I can see the mount is there:
{
"Type": "bind",
"Source": "/root/build-agent-home/xml-data/build-dir/JOB1",
"Destination": "/usr/build-app",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
Inside the docker image's build file I also referenced the directory:
...
WORKDIR /usr/build-app/
COPY docker_start.sh /usr/scripts/docker_start.sh
RUN ["chmod", "+x", "/usr/scripts/docker_start.sh"]
ENTRYPOINT ["/usr/scripts/docker_start.sh"]
The original idea was to set this up in bamboo but after build failures, I have isolated the issue to be with docker itself by running the commands mentioned above. Please help!

Trying to mount a host volume on a Docker container on Windows. What is the source folder relative to?

I'm having trouble on trying to mount a volume to my docker container.
Here is my project structure
cloudRun
distService/
index.js
Dockerfile
package.json // THIS IS THE package.json FOR THE DOCKER IMAGE
package.json // THIS IS MY MAIN PROJECT package.json
In my main project package.json I have the following scripts:
"docker-run": "./scripts/docker/docker-run.sh",
"docker-inspect": "docker exec -ti hello-world sh" // THIS IS USED TO INSPECT THE RUNNING CONTAINER
docker-run.sh
// STOPS ALL CONTAINERS
// REMOVES ALL CONTAINERS
// REMOVES ALL IMAGES
// BUILDS A NEW IMAGE FROM SCRATCH
docker build --tag hello-world:latest ./cloudRun
// TRYING TO RUN THE CONTAINER WITH A MOUNTED /distService VOLUME
docker run --name hello-world -p 3000:3000 -v //distService:/distService hello-world:latest
This is the Dockerfile:
FROM node:12-slim
WORKDIR /
COPY ./package.json ./package.json
RUN npm install
ENTRYPOINT npm start
It's all working. Except for the fact that the container is seeing /distService as an empty folder.
I know this because when I open a new terminal window and run:
npm run docker-inspect
I get to enter the folders and ls them. And this is what I'm getting: there is a distService folder, but the ls command comes back empty. PS: I did a ls on node_modules just to show that it works and the distService is indeed empty.
QUESTION
When I pass -v //folder:/folder, what is the source folder relative to? How can I be sure that I'm picking the right folder?
Environment:
Windows 10.
Docker for Windows installed
Docker Engine v19.03.13
UPDATE
I ran the container and inspected it with: docker inspect <CONTAINER_ID> to see what folder is being mounted. But it didn't help much. This is what came back:
"HostConfig": {
"Binds": [
"//distService/:/distService/"
],
// OTHER STUFF
"Mounts": [
{
"Type": "bind",
"Source": "/distService", // WHAT IS THIS PATH RELATIVE TO ?
"Destination": "/distService",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
With docker run local folder paths are always relative to the root.
To get a relative path either use the PWD environment variable:
docker run -v $(PWD)/folder:/folder ...
Or, docker-compose does support relative paths if they start with a period:
version: '3.8'
services:
nginx:
image: nginx
volumes:
- ./folder:/www
This is what I had to do to make it work.
Basicallly I had to put the full absolute path starting from c:/.
-v c:/Users/my-user/my-project/distService:/distService
Full command:
docker run --name hello-world -p 3000:3000 -v c:/Users/USER/my-project/distService:/distService hello-world:latest
I had the idea after I found this on the official docs:

Nodeman not refreshing with Docker

I'm trying to figure why Nodemon is not restarting from within docker when passing in an environment variable. It worked previously when I was not trying pass in an env variable and instead in my Dockerfile the final command was CMD ["npm", "run", "devNoClient"]
I can see Nodemon launching in the terminal but doesn't restart the server when I update a file.
Makefile
node_dev:
echo 'Starting Node dev server in Docker Container'
docker build -t node_dev .
docker run -it --env-file variables.env -p 8080:8080 node_dev
Dockerfile
WORKDIR /chord-app
# copy package.json into the container
COPY package.json /chord-app/
# install dependencies
RUN npm install
# Copy the current directory contents into the container at /chord-app
COPY . /chord-app/
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Env is required to persist variable into built image.
# Docker run can now accept variable and it will be assigned here.
# default is run in dev mode
ENV run_mode_env=devNoClient
# Run the app when the container launches
# Due to variable, CMD syntax must change for this to work https://stackoverflow.com/a/40454758
CMD npm run $run_mode_env
package.json
"scripts": {
"devNoClient": "nodemon --exec babel-node src/server/start.js",
},
I realized it was not working because I don't have any binding volumes to my local machine when starting my docker image. So the container does not what files on my machine to watch for saves so it can restart the server with nodemon.

How do I write a dockerfile to execute a simple bash script?

I'm trying to write a docker image to run a simple webserver though netcat.
So I have in my docker build folder:
Dockerfile
index.html
run_netcat_webserver.sh
The run_netcat_webserver.sh is very simple and it works fine:
#!/bin/bash
while true ; do nc -l 8080 < index.html ; done
Here is my naive Dockerfile that of course is not working:
FROM ubuntu:14.04
CMD run_netcat_webserver.sh
How should I proceed to make this work inside a docker container?
You need to make the script part of the container. To do that, you need to copy the script inside using the COPY command in the Docker file, e.g. like this
FROM ubuntu:14.04
COPY run_netcat_webserver.sh /some/path/run_netcat_webserver.sh
CMD /some/path/run_netcat_webserver.sh
The /some/path is a path of your choice inside the container. Since you don't need to care much about users inside the container, it can be even just /.
Another option is to provide the script externally, via mounted volume. Example:
FROM ubuntu:14.04
VOLUME /scripts
CMD /scripts/run_netcat_webserver.sh
Then, when you run the container, you specify what directory will be mounted as /scripts. Let's suppose that your script is in /tmp, then you run the container as
docker run --volume=/tmp:/scripts (rest of the command options and arguments)
This would cause that your (host) directory /tmp would be mounted under the /scripts directory of the container.
Simple example
Write a "Dockerfile"
FROM ubuntu:14.04 - # image you are using for building the container
ENTRYPOINT ["/bin/echo"] - # command that will execute when container is booted
Verify that you are in the same directory as the "Dockerfile".
sudo docker build .
and you are done
sample output:
ahanjura#ubuntu:~$ vi Dockerfile
ahanjura#ubuntu:~$ sudo docker build .
Sending build context to Docker daemon 1.39 GB
Step 1 : FROM ubuntu:14.04
---> 67759a80360c
Step 2 : ENTRYPOINT /bin/echo
---> Running in c0e5f48f7d45
---> 7a96b36f528e
Removing intermediate container c0e5f48f7d45
Successfully built 7a96b36f528e

Resources