How to update the Docker container using git code during development

Source: Internet
Author: User
Tags chmod config ssh using git git clone git commands docker cp docker run

In the development debugging process, you need to constantly update the code to the Git library, and then use the latest code in the GIT library to update the running Docker container, there are now two ways, it is recommended to use the second.

1. Execute the git command directly in the container and update the directories and files in the container (not recommended)

1) First, when building the container image file, define the GIT library in the Dockfile file and set the code

Run Apt-get install-y git

run git config--global user.name "xiangbin Han"
run git config--global user.email "yo Uremail@mydomain.com "

run mkdir/home/myname/.ssh/
run chmod 700/home/myname/.ssh/
ADD id_rsa/home/ myname/.ssh/
run chmod 600/home/myname/.ssh/id_rsa

run echo ' stricthostkeychecking no\nuserknownhostsfile/ Dev/null ' >/home/myname/.ssh/config
run chmod 644/home/myname/.ssh/config

run su-myname-c \
  "git Clone ssh://myname@gerrit.server.com:29418/myparent/myproject/home/myname/myproject \
  ; cd/home/myname/ MyProject \
  git fetch ssh://myname@gerrit.server.com:29418/myparent/myproject ${refs} \
  git checkout Fetch_head "

RUN chown-r myname:mygroup/home/myname

2) Second, start the container based on the container image file of the build
Docker run-it--name Container_name image_name:version

3) When you need to update a directory or file in a running container, perform the following Docker command Docker exec

Docker exec container_name bash-c "Cd/home/myname/myproject;git fetch ssh://myname@gerrit.server.com:29418/myparent/ MyProject ${gerrit_refspec};git Checkout Fetch_head "


Note that the disadvantage of this approach is that you must be able to execute GIT commands inside the container, and therefore have to add the private key to the container, resulting in a security risk.


2. Execute the Docker CP command directly on the container instance to update the directories and files in the container (recommended)

Docker CP ~/myproject/. Container_name:/home/myname/myproject

3. When you start the container instance, load volume for the container (recommended)

Docker run-v/mnt/temp/myproject/:/home/myname/myproject ...

Additional notes:

In a production environment, because the code is stable, the code can be added directly to the mirror when the image is built.

That is, set the following in Dockfile:

RUN COPY. /home/myname/myproject


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.