简体   繁体   中英

Running docker-compose in another Dockerized Container

I'm setting up my development environment and this time around I am using a docker container to run everything... like tmux, vim, etc.

When I run the image that runs my editor I use -v /var/run/docker.sock:/var/run/docker.sock and that way when I use the docker command in the shell of the editor container it links to docker on the host computer and I can run additional docker containers without an issue. This way I can code in my editor container and spin up other containers as dev envs.

However, if I try to run the same Dockerfile from the editor container that worked using docker build and docker run using a docker-compose.yml and docker-compose up I get the following output:

OUTPUT

Recreating frontend_tests_1 ... done
Recreating frontend_web_1   ... done
Attaching to frontend_tests_1, frontend_web_1
tests_1  | npm ERR! path /app/package.json
tests_1  | npm ERR! code ENOENT
tests_1  | npm ERR! errno -2
tests_1  | npm ERR! syscall open
web_1    | npm ERR! path /app/package.json
web_1    | npm ERR! code ENOENT
web_1    | npm ERR! errno -2
web_1    | npm ERR! syscall open
tests_1  | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
tests_1  | npm ERR! enoent This is related to npm not being able to find a file.
tests_1  | npm ERR! enoent 
web_1    | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
web_1    | npm ERR! enoent This is related to npm not being able to find a file.
web_1    | npm ERR! enoent 
tests_1  | 
tests_1  | npm ERR! A complete log of this run can be found in:
tests_1  | npm ERR!     /root/.npm/_logs/2019-03-24T22_08_49_446Z-debug.log
web_1    | 
web_1    | npm ERR! A complete log of this run can be found in:
web_1    | npm ERR!     /root/.npm/_logs/2019-03-24T22_08_49_447Z-debug.log
frontend_web_1 exited with code 254
frontend_tests_1 exited with code 254

If I preform the same steps on the host machine using the same directory, I get the following normal output:

EXPECTED

Recreating frontend_web_1   ... done
Recreating frontend_tests_1 ... done
Attaching to frontend_web_1, frontend_tests_1
web_1    | 
web_1    | > frontend@0.1.0 start /app
web_1    | > react-scripts start
web_1    | 
tests_1  | 
tests_1  | > frontend@0.1.0 test /app
tests_1  | > react-scripts test --env=jsdom
tests_1  | 
web_1    | Starting the development server...
web_1    | 
tests_1  |  PASS  src/App.test.js
tests_1  |   ✓ renders without crashing (22ms)
tests_1  | 
tests_1  | Test Suites: 1 passed, 1 total
tests_1  | Tests:       1 passed, 1 total
tests_1  | Snapshots:   0 total
tests_1  | Time:        1.352s
tests_1  | Ran all test suites related to changed files.
tests_1  | 
tests_1  | Watch Usage
tests_1  |  › Press p to filter by a filename regex pattern.
tests_1  |  › Press t to filter by a test name regex pattern.
tests_1  |  › Press q to quit watch mode.
tests_1  |  › Press Enter to trigger a test run.
web_1    | Compiled successfully!
web_1    | 
web_1    | You can now view frontend in the browser.
web_1    | 
web_1    |   Local:            http://localhost:3000/
web_1    |   On Your Network:  http://0.0.0.0:3000/
web_1    | 
web_1    | Note that the development build is not optimized.
web_1    | To create a production build, use yarn build.
web_1    | 
^CGracefully stopping... (press Ctrl+C again to force)
Stopping frontend_tests_1   ... done
Stopping frontend_web_1     ... done

Dockerfile.dev

FROM node:alpine

WORKDIR '/app'

COPY package.json .

RUN npm install

COPY ./ ./


CMD ["npm", "run", "start"]

docker-compose.yml

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "8080:8080"
    volumes:
      - /app/node_modules
      - .:/app
  tests:
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - /app/node_modules
      - .:/app
    command: ["npm", "run", "test"]

Docker Compose volumes: directives always reference paths on the host. (Same for docker run -v .) If you're launching Docker in some form from a container, there's no way to inject one container's local filesystem into another.

For the application you're describing, I'd suggest two paths:

  1. If your goal is to run the application in development mode, with debuggers and live reloading, put away Docker entirely and just use npm/yarn locally. (I bet you have vim and tmux installed on your host anyways for basic administration of that's the tooling you use, and installing Node is strictly easier than installing Docker.)

  2. If your goal is to run the application in Docker for production or pre-production testing, remove the volumes: directives from the Dockerfile. After having built and tested your application from step 1, docker build an image and run the image itself, without attempting to replace its code.

Also remember that anyone or anything that can run Docker commands has unrestricted root access over the host. I don't see an obvious benefit to running Docker Compose in a container the way you're describing.

I figured out what was up. When I start up the editor container, I mount a volume where all my projects are stored (ie -v path/to/host/projects:/path/to/container/projects).

When docker-compose complies the .yml file (specifically when it turns abbreviated paths like . into full paths) it does so on the editor container but then when it sends the commands to docker, it is sending it to docker on the host due to -v /var/run/docker.sock:/var/run/docker.sock . So docker tries to mount the volume using the container's working path on the host, which docker obviously can not find. My current (crappy) work around is to mount the volume to the same path name (ie -v /path/to/projects:/path/to/projects). It is not clean but it works :)

Open to any ideas for a better solution!

I was getting this when using docker-compose as well. I solved it by changing .:/app to .:/src/app under volumes inside docker-compose.yml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM