0

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

mohan08p
  • 5,002
  • 1
  • 28
  • 36
MrSkineto
  • 3
  • 2

2 Answers2

1
  • 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.

Yasser
  • 1,058
  • 8
  • 6
  • Thanks, but i dont want use docker-compose because i really want just one image (myApp), if i use docker-compose i would have two images (node and mongo). It is possible to have several images in a container but when you run this container only one image (allMyApp for example) ? thanks! – MrSkineto Apr 17 '18 at 16:40
  • Yes you can sure: 1. Create your docker file with node and mongo-your first post-. For further reading about docker multi stage builds: https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds 2. Run your container and do any further customization. 3. Create final image with the current changes (allMyApp): $ docker commit your_name/allMyApp:version1 Now you have an image with all your configuration. – Yasser Apr 17 '18 at 20:58
  • Yeah, but how i link mongodb image with nodejs image in the dockerfile? In docker-compose its with 'link', in dockerfile? – MrSkineto Apr 18 '18 at 15:23
  • If you mean how to construct images in the yml file, then your docker file is good to go. You don't need to add anything between the images. When you build that docker file, it will create node, mongo in one image and two exposed ports 4000 and 27017 – Yasser Apr 18 '18 at 17:18
  • No, i don't want yml file. When I run my container i get this error ```UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]``` Because i dont link my nodejs image with mongodb image, how can do it? – MrSkineto Apr 19 '18 at 08:22
0

I thing that the simply solution is to create your own image from mongo image and install node manually.

You can have multiple FROM in a Dockerfile :

FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another. Simply make a note of the last image ID output by the commit before each new FROM instruction. Each FROM instruction clears any state created by previous instructions.

From : https://docs.docker.com/engine/reference/builder/#from

Vincent Faliès
  • 298
  • 3
  • 10
  • I want have in my dockerfile FROM (Client Side App Image) FROM (Back End App Image) FROM (Mongo Image) and Back End App Image depends on mongo. Is this possible? thanks! – MrSkineto Apr 17 '18 at 16:42