Search code examples
dockerdockerfiledocker-multi-stage-build

How to have two images in a Dockerfile without using Docker Compose?


How to have two images in the dockerfile and that are linked?

I don't want use docker compose, I want something like this

FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm i
COPY . /usr/src/app
EXPOSE 4000
CMD [ "npm", "start" ]

FROM mongo:latest as mongo 
WORKDIR /data
VOLUME ["/data/db"]
EXPOSE 27017

But I do not know how to join the images

Thank you


Solution

    • Compose is a tool for defining and running multi-container Docker applications.

    • Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

    Creating one image for node and mongo means you will have both of them inside the container, up and running (more resources, harder to debug, less stable container, logs will be hard to follow, etc).

    Create separate images, so you can run any image independently:

    $ docker run -d -p 4000:4000 --name node myNode

    $ docker run -d -p 27017:27017 --name mongo myMongo

    And I strongly recommend to you using compose files, it will give you much more control over your whole environment.