2

I'm using docker-compose (on windows) to bring up a mongoDB along with a couple of nodeJS processes (on a remote CentOS machine via SSH). The nodeJS containers are supposed to have the code in my project directory mounted into /home/node/app so they can execute it with the command node web/run. However, when I use docker context to deploy this group of containers to my remote host via SSH, I get an error saying the script at /home/node/app/web/run is not found, suggesting my code was not copied into/mounted into the container.

You can see that I'm mounting the current directory (my project) below:

  web:
    image: node:15.12.0
    user: node
    working_dir: /home/node/app
    volumes:
      - ./:/home/node/app:ro
    ports:
      - 80:80
    command: node web/run
    depends_on:
      - events-db

Am I misunderstanding how volumes work in Docker? What are the best practices for getting my code into a container so it can be executed?

1 Answers1

1

What are the best practices for getting my code into a container so it can be executed?

Write a Dockerfile to build an image containing it. COPY your application code in. If you need a build step (for instance, transpiling Typescript to plain Javascript, or running a builder like Webpack) RUN it in the Dockerfile.

Am I misunderstanding how volumes work in Docker?

When you have a bind-mount, for example volumes: ['.:/home/node/app'], the Docker daemon always looks on the system it is running on. Volumes and bind mounts never come from another system, even if you're launching the container from somewhere else.

With the setup you show, you need to separately copy the application code, and run it there. There's not really any benefit to using Docker in this case: you can just install Node on both hosts, scp your application code across, and ssh other-host node web/run.

On the other hand, say you do package your application into an image. You might have a pair of YAML files like:

# docker-compose.yml
version: '3.8'
services:
  web:
    image: me/myapp
    ports:
      - 80:80
    depends_on:
      - events-db
    # USER, CMD, etc. taken from the Dockerfile
# docker-compose.override.yml
version: '3.8'
services:
  web:
    build: .

On your local system, you can docker-compose up --build the container, run, and test it locally. If that works, you can docker-compose push the built image to Docker Hub (or, with image: registry.example.com/me/myapp syntax, a cloud-hosted registry or something you run yourself). Then you can copy only the docker-compose.yml file to the remote system (but not the override file), and run the exact image you built and tested locally, without separately copying the application code.

David Maze
  • 130,717
  • 29
  • 175
  • 215