docker

 DockerFile 

Dockerfile contains instructions on how the image should be built. Here are some of the most common instructions that you can meet in a Dockerfile:

  • FROM is used to specify a base image for this build. It's similar to the builder configuration which we defined in a Packer template, but in this case instead of describing characteristics of a VM, we simply specify a name of a container image used for build. This should be the first instruction in the Dockerfile. 
  • ADD and COPY are used to copy a file/directory to the container. See the difference between the two.
    • ADD allows <src> to be an URL
    • If the <src> parameter of ADD is an archive in a recognised compression format, it will be unpacked 
  • ENV sets an environment variable available within the container.
  • WORKDIR changes the working directory of the container to a specified path. It basically works like a cdcommand on Linux.
  • VOLUME: The VOLUME command will mount a directory inside your container and store any files created or edited inside that directory on your hosts disk outside the container file structure, bypassing the union file system.The idea is that your volumes can be shared between your docker containers and they will stay around as long as there's a container (running or stopped) that references them.The fundamental difference between VOLUME and -v is this: -v will mount existing files from your operating system inside your docker container and VOLUME will create a new, empty volume on your host and mount it inside your container.

        docker run -v /<container_dirctory> <image name>   // will create a volume under <container_directory> inside of the container, and it will presist if the container stop. 

        docker run -v <host machine directory>:/<container_dirctory> <image name>  // will mount the dir on hostmachie <host machine directory> on container dir <container_dirctory>, the files will be shared between the host and the container

  docker run -it --volumes-from container1 --name container2 busybox    // create the 2nd container which will share the volums from the first container 

       

 

  • All three instructions (RUN, CMD and ENTRYPOINT) can be specified in shell form or exec form.
    • Shell form

      <instruction> <command>   

e,g 

ENV name John Dow
ENTRYPOINT echo "Hello, $name"

when container runs as docker run -it <image> will produce output

Hello, John Dow
    • Exec form

      This is the preferred form for CMD and ENTRYPOINT instructions.

      <instruction> ["executable", "param1", "param2", ...]

When instruction is executed in exec form it calls executable directly, and shell processing does not happen. For example, the following snippet in Dockerfile

ENV name John Dow
ENTRYPOINT ["/bin/echo", "Hello, $name"]

when container runs as docker run -it <image> will produce output

Hello, $name

Note that variable name is not substituted.

  • RUN is used to run a command inside the image. Mostly used for installing packages and construct the images.  can do somthing like 
    RUN pip install -r requirements.txt // requirements.txt incldues all the python package requried
  • CMD sets a default command, which will be executed when a container starts. This should be a command to start your application. 
  • ENTRYPOINT configures a container that will run as an executable. Compared with CMD, ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.

http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/

Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.

Choose CMD if you need to provide a default command and/or arguments that can be overwritten from command line when docker container runs.

 

 

Build docker iamge for example app

(git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples )

In the directory which includes Dockerfile run

export PROJECT_ID="$(gcloud config get-value project -q)"

docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .   

it will build the image using the Dockerfile in the current directory and tag it with a name, such as gcr.io/my-project/hello-app:v1. The gcr.io prefix refers to Google Container Registry, where the image will be hosted

 

To push image to google repo 

 

gcloud auth configure-docker

 

gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1

 

To push image to Github 

> docker login 

for existing images 

> docker tag local-image:tagname <<hub-user>/<reponame>:tagname

for new build images 

>docker build -t <hub-user>/<repo-name>[:<tag>]

To push to repo 

 docker push <hub-user>/<repo-name>:<tag>


docker commit

Create a new image from a container’s changes

 
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]  
e.g
docker commit c3f279d17e0a svendowideit/testimage:version3


posted @ 2018-05-24 17:33  anyu686  阅读(165)  评论(0编辑  收藏  举报