What is the difference between virtual machines (VMs) and containers? Why should you start containerizing your legacy app? I am answering all the questions you may have before containerizing. This article is giving you a way to migrate to a containerized app with Docker and Docker-compose.
Containersare taking the lead on the virtual machines, as it makes installation quicker and easier, it runsfaster and it takes less space on your computer. To understand the technological reasons behind this, read this great article.
If you’re using an old app deployed using VMs, here is a way to migrate to a containerized app with Docker and docker-compose.
2.Questions to answer before containerizing
- First take a white board and draw a schema of the architecture using, for example, the C4 model. Make sure that for service you have the language/framework, how it communicates with the other services, the crons that may be executed, etc. Be careful, draw your production app, or at least your staging app to help you to as iso prod as possible.
Then make sure you are able to run it completely on your laptop « the old way ». It helps you find non-authenticated routes you can easily call to test, understand the db provisioning which is done, etc.Take this opportunity to measure the installation time to check the difference with your new docker environnement ;)
For each part answer these questions to list the steps you will need in your Dockerfile:
Which process starts the app? For example for a node app it could be
node ./server.js. Which files need to be inside the docker during the runtime?
Do base images already exist in docker hub? Are they maintained? If yes, are an “alpine” version, usually the lighter one, available?
Which environment variables are set? You should look for
Build each Dockerfile. Start with the
COPYthe necessary files, declare the environment variables with the
RUNthe necessary commands to build the binary file and use the
CMDinstruction to be sure it will be executed. Remember, one container equals to one process.Then test each part individually by running
docker build -t <IMAGE_TAG>and then
docker run -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE_TAG>
- Finally, connect all your services with docker-compose.To benefit from live reloading and to keep your fixtures in the potential database, mount some volumes.
- Test that your deployment into staging is still working.
When you’re containerizing your application, it’s easy to separate the reverse proxy, the app and the database, and it often tempting to do more by turning your monolithic app into microservices. Nevertheless it’s not always a good idea as it can turn the containerization into a big thing. First containerize your monolithic app, then you will be able to improve it and create easily microservices one after the other.
Later on you could even transform each services into Kubernetes resources to have an iso software factory!