I have a simple express.js server that, for some operations, needs to spawn some docker containers. I want to dockerize this NodeJs server using a Dockerfile, so that it will spawn docker containers inside a docker container. My Dockerfile currently looks like this:
FROM docker:23.0.6-dind-alpine3.18
RUN apk update
RUN apk add --no-cache nodejs
RUN apk add --no-cache npm
RUN apk add --no-cache iptables bash
RUN apk add --no-cache --upgrade bash
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --force --verbose
COPY . .
RUN npm run completeBuild
EXPOSE 4000
CMD ["npm","start"]
I build it with:
docker build -t trydocker .
And I run it with:
docker run --privileged -it -p 4000:4000 -p 2375:2375 -p 2376:2376 trydocker
The server process gets spawned correctly and I can access my express server via localhost:4000.
But when I try to call docker pull
from inside the container I get an error that says that my docker daemon is not running.
Cannot connect to the Docker daemon at tcp://docker:2375. Is the docker daemon running?
Instead, if I remove the last directive CMD ["npm","start"]
from the Dockerfile, the docker daemon starts correctly, but my server is not online!
Is there a way to have both running?
I have looked at some related issues, like this one, but it did not help.