编程知识 cdmana.com

Introduction to docker (2) use of container and image

1. Docker client

docker The client is very simple , We can type in docker Order to see Docker All command options on the client side .

[[email protected] jenkins]# docker

By command docker <command> --help Learn more about the designated Docker Command usage . For example, we want to see docker stats Specific use of instructions :

[[email protected] jenkins]# docker stats --help
Usage:	docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

2. Docker Mirror image (Image) Use

When running the container , If the image used does not exist locally ,docker Will automatically from docker Download in the image warehouse , The default is Docker Hub Public image source download .

  1. Look at the existing image (Image)
[[email protected] jenkins]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB
  • Description of each option :
    • REPOSTITORY: Represents the repository source for the mirror
    • TAG: Mirrored label ( There can be more than one repository source TAG, Represents different versions of the repository source )
    • IMAGE ID: Mirror image ID
    • CREATED: Image creation time
    • SIZE: Image size
  1. Get a new image
[[email protected] jenkins]# docker pull <REPOSITORY:TAG>

Use REPOSTITORY:TAG To define different mirrors , Such as ubuntu Warehouse source 15.10 Version of :ubuntu:15.10.

2.1 Find image

We can Docker Hub Website to search for images . You can also use docker search Command to search for images . For example, we need a httpd The mirror image of our web service .

[[email protected] jenkins]# docker search httpd
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                   The Apache HTTP Server Project                  3248                [OK]                
centos/httpd                                                                            33                                      [OK]
  • NAME: The name of the mirror repository source
  • DESCRIPTION: Description of mirror image
  • OFFICIAL: whether docker Official release

2.2 create mirror

  • When we are from docker When the image downloaded in the image warehouse cannot meet our needs , We can change the image in two ways .
    1. Use Dockerfile Command to create a new image
    2. Updates the image from the container that has been created , And submit the image
  1. You need to create a Dockerfile file , The file name must be Dockerfile
[[email protected] jenkins]# vim Dockerfile
FROM centos
RUN yum -y install httpd
RUN yum -y install net-tools
RUN yum -y install elinks
CMD ["/bin/bash"]
  1. Build a mirror image , Use docker build Build the image , Finally, you need to specify Dockerfile File path ;
    • See the last two Successfully Then build successfully .
    • It's based on what's written in the file , Use centos Mirror instantiates a container , Enter the container and execute three yum command
[[email protected] jenkins]# docker build -t jenkins/centos-http-net /home/jenkins
Successfully built 09266c896243
Successfully tagged jenkins/centos-http-net:latest
  1. Look at the built image
[[email protected] jenkins]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB
jenkins/centos-http-net   latest    09266c896243   10 seconds ago   581MB
  1. Image building process In the huge amount of information that the build command outputs , Is to perform Dockerfile Each line in , The key lines are as follows
Step 1/5 : FROM centos  #  call centos
 5e35e350aded   # centos Mirror image id
  
Step 2/5 : RUN yum install httpd -y
 Running in a16ddf07c140  #  Run a temporary container to execute install httpd
Removing intermediate container a16ddf07c140  #  Delete the temporary container when finished id
 b51207823459  #  Make a mirror image 
  
Step 3/5 : RUN yum install net-tools -y
 Running in 459c8823018a #  Run a temporary container to execute install net-tools
Removing intermediate container 459c8823018a #  Delete the temporary container when finished id
 5b6c30a532d4  #  Regenerate into a mirror image 
 
Step 4/5 : RUN yum install elinks -y
 Running in a2cb490f9b2f  #  Run a temporary container to execute install elinks
Removing intermediate container a2cb490f9b2f #  Delete the temporary container when finished id
 24ba4735814b #  Make a mirror image 
 
Step 5/5 : CMD ["/bin/bash"]
 Running in 792333c88ba8  #  Run the temporary container , perform /bin/bash
Removing intermediate container 792333c88ba8  #  Delete the temporary container when finished id
 09266c896243  #  Create a mirror image 
Successfully built 09266c896243  #  The mirror image of the final success id It's the final image created id

Each step generates a mirror image , All belong to one docker commit The results of the implementation of In this process, a total of three mirror layers are generated , Will be stored in graph in , Including the relationship between layers , see docker images The image generated in id Whether it is the last generated image id,FROM and CMD It's not a mirror layer

adopt docker history You can also see the simple construction process , There are three yum They are the three mirror layers formed

[[email protected] jenkins]# docker history chai/centos-http-net:latest 
IMAGE         CREATED          CREATED BY                                      SIZE    COMMENT
09266c896243  17 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
24ba4735814b  17 minutes ago   /bin/sh -c yum install elinks -y                121MB               
5b6c30a532d4  18 minutes ago   /bin/sh -c yum install net-tools -y             112MB               
b51207823459  18 minutes ago   /bin/sh -c yum install httpd -y                 145MB               
5e35e350aded  4 months ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>     4 months ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>     4 months ago     /bin/sh -c #(nop) ADD file:45a381049c52b5664…   203MB

3. Docker Containers (Container)

Mirror image (Image) The concept of a more biased environment package , This environment package can be moved to any Docker To run on the platform ; And the container (Container) It's an instance of your running environment package , let me put it another way container yes images A concrete form of expression of .

  • Container related orders :
docker container ls, Columns appear in the running container 
docker container ls -a, List all containers , Including those that are not running 
docker ps -a, List all containers , Including those that are not running 
docker run -it centos, How to run containers interactively , In this case, after running contianer Will not quit , But this conversation closes this ,contianer Or will you quit 
docker container rm container_id, You can delete a container ( Be careful , there id It can be a complete container id, It can also be abbreviated , As long as it's OK with other id Just separate them )
docker rm container_id, The default is to delete container Of , So it's with docker container rm id It's the same 
docker images, and docker image ls It's the same , Display all images of the machine 
docker image rm image_id, Delete image, It can be image_id perhaps image_name
docker rmi id, Is and docker image rm id Same , Is shorthand. 
docker container ls -aq, It only shows the container id, Useful when deleting containers in bulk 
docker rm $(docker container ls -aq), Delete all containers 
docker rm $(docker ps -aq), Also delete all containers 
docker rm $(docker container ls -f "status=exited" -q), Delete all containers that are not running 

4. Dockerfile File syntax

Commonly used to build image instructions

FROM  #  Appoint base Mirror image 
MAINTAINER #  Specify mirror author , After the root arbitrary string 
COPY #  Take the document from host Copy it into the mirror image 
  COPY src dest
  COPY ["src","dest"]
  src: Only documents 
ADD #  Usage and COPY equally , The only difference src It can be a compressed package , Decompress to dest Location ,src It can also be a directory 
ENV #  Setting environment variables can be referenced by the next mirror layer , And it will be added to the image 
  ENV MY_VERSION 1.3
  RUN yum -y install http-$MY_VERSION
  #  When entering the container of the image echo $MY_VERSION Will be output 1.3
EXPOSE #  Specifies the port on which processes in the container listen ( Interface ), Will be in docker ps -a Medium ports It shows that 
  EXPOSE 80
VOLUME #  The container volume , I'll talk about it later , hold host The path of mount Into the container 
  VOLUME /root/htdocs /usr/local/apahce2/htdocs
WORKDIR #  Set the working path for the subsequent mirror layer 
        #  If not set ,Dockerfile Every command in the file will return to its initial state 
        #  After setting once , Will always be layered after the execution of the route , need WORKDIR / Back to root 
CMD #  The default command to run after the container is started , When the constructed image is instantiated as a container , The default command executed after entering 
    #  This order will be docker run Start command instead of 
    #  Such as :docker -it --rm centos echo "hello"
    # echo "hello" Will replace CMD Running commands 
  CMD ["nginx", "-g", "daemon off"]  #  The container after the image is instantiated , Run after entering nginx Start the service 
ENTRYPOINT #  The command to run when the container starts , It won't be docker run Instead of 

4.1 RUN/CMD/ENTRYPOINT difference

  1. RUN: Execute the command and create a new mirror layer , It is mainly used to install software package
  2. ENTRYPOINT and CMD They all count as start-up commands , That is, instructions that must be started before the container can be executed , Generally used to start the running program ; When ENTRYPOINT and CMD Simultaneous existence ,ENTRYPOINT take effect .
  3. ENTRYPOINT and CMD Use format :
    • shell Format : Will always call a shell Program to execute commands , Such as ENTRYPOINT echo "hello $name"
    • exec Format :CMD [" command ", " Options ", " Parameters "]ENTRYPOINT [" command ", " Options ", " Parameters "]
      • exec Cannot be called in the format ENV Variables defined , If you have to let exec Format to read variables , Its command position is to use a shell Environmental Science . Because reading variables is using shell To read . Such as :ENTRYPOINT ["/bin/sh", "-c", "echo hello,$ Variable name "]
      • When using exec When the format ,ENTRYPOINT The first parameter of is identified as a command ,CMD The parameters of are changed to... In order ENTRYPOINT Arguments to the command

版权声明
本文为[Super_ Charles]所创,转载请带上原文链接,感谢

Scroll to Top