Multiple images inside one container

2020-02-08 05:51发布

问题:

So, here is the problem, I need to do some development and for that I need following packages:

  1. MongoDb
  2. NodeJs
  3. Nginx
  4. RabbitMq
  5. Redis

One option is that I take a Ubuntu image, create a container and start installing them one by one and done, start my server, and expose the ports.

But this can easily be done in a virtual box also, and it will not going to use the power of Docker. So for that I have to start building my own image with these packages. Now here is the question if I start writing my Dockerfile and if place the commands to download the Node js (and others) inside of it, this again becomes the same thing like virtualization.

What I need is that I start from Ubuntu and keep on adding the references of MongoDb, NodeJs, RabbitMq, Nginx and Redis inside the Dockerfile and finally expose the respective ports out.

Here are the queries I have:

  1. Is this possible? Like adding the refrences of other images inside the Dockerfile when you are starting FROM one base image.
  2. If yes then how?
  3. Also is this the correct practice or not?
  4. How to do these kind of things in Docker ?

Thanks in advance.

回答1:

Keep images light. Run one service per container. Use the official images on docker hub for mongodb, nodejs, rabbitmq, nginx etc. Extend them if needed. If you want to run everything in a fat container you might as well just use a VM.

You can of course do crazy stuff in a dev setup, but why spend time setting up something that has zero value in a production environment? What if you need to scale up one of the services? How do set memory and cpu constraints on each service? .. and the list goes on.

Don't make monolithic containers.

A good start is to use docker-compose to configure a set of services that can talk to each other. You can make a prod and dev version of your docker-compose.yml file.

Getting into the right frame of mind

In a perfect world you would run your containers in clustered environment in production to be able to scale your system and have concurrency, but that might be overkill depending on what you are running. It's at least good to have this in the back of your head because it can help you to make the right decisions.

Some points to think about if you want to be a purist :

  • How do you have persistent volume storage across multiple hosts?
  • Reverse proxy / load balancer should probably be the entry point into the system that talks to the containers using the internal network.
  • Is my service even able run in a clustered environment (multiple instances of the container)

You can of course do dirty things in dev such as mapping in host volumes for persistent storage (and many people who use docker standalone in prod do that as well).

Ideally we should separate docker in dev and docker i prod. Docker is a fantastic tool during development as you can have redis, memcached, postgres, mongodb, rabbitmq, node or whatnot up and running in minutes sharing that compose setup with the rest of the team. Docker in prod can be a completely different beast.

I would also like to add that I'm generally against the fanaticism that "everything should be running in docker" in prod. Run services in docker when it makes sense. It's also not uncommon for larger companies to make their own base images. This can be a lot of work and will require maintenance to keep up with security fixes etc. It's not necessarily the first thing you jump on when starting with docker.