Copy the files from the host to the docker container, then copy and click

I am using docker in Ubuntu. At the development stage, I cloned all the source code from Git to the host, edited them in WebStorm, and they started with Node.js inside the -v /host_dev_src:/container_src container with -v /host_dev_src:/container_src so that I can check.

Then, when I wanted to send them for testing: I made a container and clicked the new version. But when I pulled out and ran the image on a test machine, the source code was missing. This makes sense since there is no /host_src in the test machine.

My current workaround is to clone the source code on the test machine and run the docker using -v /host_test_src:/container_src . But I would like to know whether it is possible to copy the source code directly to the container and avoid this manipulation. I would rather just copy, paste and run the image file with the source code, especially since our test machines do not have an Internet connection.

PS: It seems docker cp only supports copying a file from the container to the host.

+5
source share
4 answers

One solution is to complete the git clone step in the Docker file, which adds the source code to the image. During development, you can override this code with the -v argument to docker run so you can make changes without rebuilding. When it comes to testing, you just check your changes and create a new image. You now have a fully self-contained image for testing.

Please note that if you have a manual VOLUME your Docker file, you need to ensure that this occurs after the step of git clone.

The problem with this approach is that if you use a compiled language, you want your binaries to be in the final image. In this case, the git clone should be replaced with some code that either extracts or compiles the binaries.

+2
source

Please process your source codes with data, then pack them as a data container, see https://docs.docker.com/userguide/dockervolumes/

Step 1 Create app_src Image app_src

Put one Dockerfile in your git repository like

 FROM BUSYBOX ADD . /container_src VOLUME /container_src 

Then you can create the original image, for example

 docker build -t app_src . 

During the development period, you can always use your old solution -v /host_dev_src:/container_src .

Step 2 Transfer this docker image as an application image

You can transfer this app_src image to a test system similar to your application image, possibly through the docker registry

Step 3 Launch as a Data Container

In the test system, run the app container above it. (I use ubuntu for demonstration)

 docker run -d -v /container_src --name code app_src docker run -it --volumes-from code ubuntu bash root@dfb2bb8456fe :/# ls /container_src Dockerfile hello.c root@dfb2bb8456fe :/# 

Hope this helps

(give loans https://github.com/toffer/docker-data-only-container-demo , which I get detailed ideas)

+1
source

Adding Adrian to the answer, I make a git clone and then do

 CMD git pull && start-my-service 

to run the latest code in a verified branch. This is clearly not for everyone, but it works on some software release models.

+1
source

You can also try two Dockerfiles. The base could be found, how to start an application from the predefined folder, but do not declare its volume. During development, you will run this container with your host folder installed as the volume. The other, package one, inherits base one and copies / adds files from your host directory, again without volumes, so that you carry all the files to the tester host.

0
source

Source: https://habr.com/ru/post/1210647/


All Articles