简体   繁体   中英

Create a new image or share source code in a volume in a docker container in development?

I'm wondering what would be the best practice to update the source code in a docker container that is run within( in JAVA and python). I could either modify my code, create a new image and run a container. In Java, I would also need to compile the code. Another solution would be to just use a volume to share the code between my machine and the container so that each time I modify the behavior of my code I won't need to create a new docker image.

I'm asking all of this for development purposes, not deploying Is there a best practice between these two solutions? What are the pros and cons? Is there a difference for a compiled language like a java compared to python?

this was my question and I found it in this way For development, you can bind a volume. in this example, I assume you have exposed a port on 3000 also instead of an absolute project address NOTE if you are in the project directory within your shell you can use $(pwd)

docker run -d -p 3000:3000 -v absolute-project-address:/app image-name

also you can add your volume name for storing data

docker run -d -p 3000:3000 -v absolute-project-address:/app -v volumeName:/app/data image-name 

But for the production, you must create an image

You should definitely make a new image instead of sharing the code via a mounted volume. Then whole docker ideology is to produce a self-contained "service", which can be portable across server (ie copy the image). Yuo docker file and image build process will track versions of your code, ie in your docker file you can pull a specific branch, or you can tag the image with a specific tag. In case of a mounted volume, you never sure what version of your code is actually. run.

Hope this reason alone will help you to decide.

您应该创建一个新映像,如果您认为创建新映像并进行部署是您的任务,那么可以设置CI / CD管道,该管道将自动编译Java代码,创建映像并在您每次部署时为您部署将代码推送到GIT。

In general the best practice would be to create a new image when you have new source code and run a new container from that image. Docker can quickly create and run new images, so you should take advantage of that. In addition you can know what version of your code you're running by tagging your images.

You mention that you would need to compile the Java code in the Docker case, but you would need to in either case. You could just compile your code into a Jar and then ADD (or COPY) the Jar into your container and run it there.

A couple of quick downsides that come to mind with the external code approach: you would need some external way to manage what version of code is running, docker tags take care of that for you. You would also need some way to alert the Docker container that a new executable file is ready and then have Docker run that. In addition you would need to make sure that your Docker container is running the same version of Java (or Python) that your system is running, this too is taken care of in Docker since you can build from a Java base image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM