Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

Best practice for running Symfony 5 project with Docker and Docker-Swarm

I have an existing Symfony 5 project with a mysql database and a nginx Webserver. I wanna dockerize this project, but on the web I have found different opinions how to do that.

My plan is to write a multi-stage Docker file with at least a dev and a prod stage and let this build with docker-swarm. In my opinion it is useful to install the complete code during the build and having multiple composer.json files (one for every stage). In the web I have found opinions to not install the app new on every build but to copy the vendor and var folder to the container. Another opinion was to start the installation after the build process of the container is ready. But I think with that the service is not ready, when the app is successfully deployed.

What are you thinking is the best practice here?

question from:https://stackoverflow.com/questions/65841744/best-practice-for-running-symfony-5-project-with-docker-and-docker-swarm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Build exactly the same image for all environments

Do not build 2 different images for prod and dev. One of the main docker benefits is, that you can provide exactly the same environment for production and dev.

You should control your environment with ENV vars. for example, you can enable Xdebug for dev and disable it for prod.

Composer has option to install dev and production packages. You should use this feature.

If you decide to install some packages to dev. Try to use the same Dockerfile for both environment. Do not use Dockerfile.prod and Dockerfile.dev It will introduce some mess in the future.

Multistage build

You can do multistage build described in the official Docker documentation if your build environment requires much more dependencies than your runtime.

Example of it is compilation of the program. During compilation you need a lot of libraries, and you produce single binary. So your runtime does not need all dev libraries.

First stage can be build in second stage you just copy binary and it is.

Build all packages into the docker image

You should build your application when Docker image is building. All libraries and packages should be copied into image, you should not install them when the application is starting. Reasons:

  • Application starts faster when everything is installed
  • Some of the libraries can be changed in future or removed. You will be in troubles and probably you will spend a lot of time to do debugging.

Implement health checks

You should implement Health-Check. Applications require external dependencies, like Passwords, API KEY, some non sensitive data. Usually, We inject data with environment variables.

You should check if all required variables are passed, and have good format before your application is started. You can implement Health-Check or you can check it in the entrypoint.

Test your solution before it is released

You should implement mechanism of testing your images. For example in the CI:

  • Run your unit tests before image is built
  • Build the Docker image
  • Start new application image with dummy data. If you require PostgreSQL DB you can start another container,
  • Run integration tests.
  • Publish new version of the image only if all tests pass.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...