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
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 .
- 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
- 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 ：
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 .
DockerfileCommand to create a new image
- Updates the image from the container that has been created , And submit the image
- You need to create a Dockerfile file , The file name must be Dockerfile
jenkins]# vim Dockerfile FROM centos RUN yum -y install httpd RUN yum -y install net-tools RUN yum -y install elinks CMD ["/bin/bash"]
- Build a mirror image , Use
docker buildBuild 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
jenkins]# docker build -t jenkins/centos-http-net /home/jenkins Successfully built 09266c896243 Successfully tagged jenkins/centos-http-net:latest
- 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
- 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
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 "src","dest"] src: Only documents ADD ENV ENV MY_VERSION 1.3 RUN yum -y install http-$MY_VERSION EXPOSE EXPOSE 80 VOLUME VOLUME /root/htdocs /usr/local/apahce2/htdocs WORKDIR CMD CMD ["nginx", "-g", "daemon off"] ENTRYPOINTMAINTAINER COPY COPY src dest COPY [
4.1 RUN/CMD/ENTRYPOINT difference
RUN： Execute the command and create a new mirror layer , It is mainly used to install software package
CMDThey 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 .
CMDUse 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
- shell Format ： Will always call a shell Program to execute commands , Such as