Basic concepts:
Container. Each container can be viewed as a standalone host. The creation of container usually has an image as its template. Analogy to a virtual machine can be understood as image is the mirror of the virtual machine, and container is a running virtual machine. A virtual machine image can create multiple running virtual hosts and be independent of each other. Note: Once created, container will persist if not removed with the RM command. So remember to delete it when you're done with it.
Mirror. The image is equivalent to the container template, and after container is created, it depends entirely on what image it uses. Image can be created through container (which is equivalent to saving the state of the container at this time as a snapshot), or by Dockerfile (a text file, which uses some of the Docker-defined notation). The method created by Dockerfile enables the environment configuration and code to be managed together with the repository.
The warehouse where the image is stored. As long as you can connect to registry, everyone can easily get a mirror from the repository through the pull command. Docker uses the default repository for Docker hubs, which can be used to build Mirror connected to the Docker hub at home, which speeds up image acquisition. Daocloud
A lightweight Linux virtual machine, primarily to make it possible for non-Linux systems to use Docker. It is essentially a VirtualBox virtual host + a command-line tool that can manage this virtual host. Due to the existence of this virtual host, container needs to acquire some physical system resources (such as USB devices) on a non-Linux system, not only to configure the Docker command, but also to configure the resource configuration of the Boot2docker virtual host.
Common Command Table
General:
Operation |
Command |
Example |
View the Docker version |
docker version |
docker version |
View Docker Information |
docker info |
docker info |
View a command Help information |
docker help [command] |
docker help attach |
View Docker Help information |
docker --help |
docker --help |
Operation |
Command |
Example |
Create container |
docker create |
docker create chenhengjie123/xwalkdriver |
Create and Run container |
docker run |
docker run chenhengjie123/xwalkdriver /bin/bash |
After creating and running container, enter its bash console |
docker run -t -i image /bin/bash |
docker run -t -i ubuntu /bin/bash |
Create and run container and let it run in the background, and port mappings |
docker run -p [port in container]:[port in physical system] -d [image] [command] |
docker run -p 5000:5000 -d training/webapp python app.py |
View all container information that is running |
docker ps |
docker ps |
View the last created container |
docker ps -l |
docker ps -l |
View all container, including the running and closed |
docker ps -a |
docker ps -a |
Outputs the stdout information for the specified container (used to look at log, the effect is similar to tail-f, and is output in real time. ) |
docker logs -f [container] |
docker logs -f nostalgic_morse |
Get container Specify port mapping relationship |
docker port [container] [port] |
docker port nostalgic_morse 5000 |
View the list of container processes |
docker top [container] |
docker top nostalgic_morse |
View container More information |
docker inspect [container] |
docker inspect nostalgic_morse |
Stop Continer |
docker stop [container] |
docker stop nostalgic_morse |
Force Stop Container |
docker kill [container] |
docker kill nostalgic_morse |
Start a container that has stopped |
docker start [container] |
docker start nostalgic_morse |
Restart the container (if container is turned off, it starts directly) |
docker restart [container] |
docker restart nostalgic_morse |
Delete Container |
docker rm [container] |
docker rm nostalgic_morse |
When you need to specify container in a command, you can use either its name or its ID.
Operation |
Command |
Example |
Create an image from container |
docker commit [container] [imageName] |
docker commit nostalgic_morse ouruser/sinatra:v2 |
Create an image from Dockerfile |
docker build -t [imageName] [pathToFolder] |
docker build ouruser/sinatra:v3 . |
View all local image |
docker images |
docker images |
Searching for mirrors in registry |
docker search [query] |
docker search ubuntu |
Get the image from registry (if no tag name is specified, this tag is used by default latest) |
docker pull [imageName] |
docker pull ubuntu:14.04 ,docker pull training/webapp |
tag to Image |
docker tag [imageId] [imageName] |
docker tag 5db5f8471261 ouruser/sinatra:devel |
Upload local image to registry (this will upload all tags) |
docker push [imageName] |
docker push ouruser/sinatra |
Delete local image |
docker rmi [image] |
docker rmi training/sinatra |
Note: This tag is used by default when the tag name is not specified in the image latest. However, the meaning of latest is not the same as the head of the VCS, it is not the latest image, just the image that represents the tag name latest. If there is no image with the tag name called latest, it will be an error.
Summarize
Although Docker is a virtualization technology, it is more like managing system software or code. Inside, ps
top
rm
commands make it very kind to people who use Linux commands (though their semantics are a little different ...). ),,, start
stop
restart
makes you feel like you're in control of the service, and,, push
pull
commit
, tag
makes you feel like you're using Git. So programmers feel very friendly and easy to get started with.
Docker also integrates the operating environment into CI as it can be built using Dockerfile for image, and Docker hub supports automatic building from Dockerfile, such as GitHub.
The drawback is that Docker currently only supports the container technology on Linux, so it has to add one more virtual machine layer to run on a non-Linux system. This will cause some problems that will not occur on Linux (IP address, hardware resources, file mappings, etc.), and because Linux-based, some Windows programs will be acclimatized, more generic than virtual machines.
But the benefit is that the system resources are much lower. A computer that can only open several virtual machines generally can open dozens of container, and the startup time of container is usually within a few seconds, much faster than the virtual machine. In addition, since the image of Docker is essentially an incremental image in addition to some special base mirrors, the repetition does not consume additional resources, so several images that appear to have a number of G will have a repeating portion of the underlying image used (which in most cases will be partially duplicated). Then they will actually occupy much less space.
https://testerhome.com/topics/2760 Common commands: Installing Docker:
- Ubuntu Installation:Image Management
Docker images: List all local mirrors
Docker search <image_id/name>: Find Imagedocker pull <image_id>: Download Imagedocker push <IMAGE_ID> : Upload Imagedocker rmi <image_id>: Delete IMAGE container management
Docker run-i-T <IMAGE_ID>/bin/bash:-i: Standard input to container-T: Assigning a virtual terminal/bin/bash: Execute bash script
-D: Run as Daemon (background)
-P: Default matching Docker container's 5000 port number to the host's-P <HOT_PORT>:<CONTAINER_PORT>: Specify port number--name: Specify container name--rm: Delete container on exit
Docker stop <container_id>: Stop CONTAINER
Docker start <container_id>: Restart CONTAINER
-L: Show last started container
-
A: Displays the stopped container at the same time, showing only the start status by default
Docker attach <CONTAINER_ID> connect to the boot container docker logs <CONTAINER_ID> : Output container log
Docker CP <CONTAINER_ID>:p ath hostpath: Copy the files in the container to the host directory
<container_ Id>
<container_id>: Viewing changes in a container
<container_id>: View container details (output as JSON)
-F: Find specific information, such as - ' {{. Networksettings.ipaddress}} '
Docker commit-m "comment" -a "author" <CONTAINER_ID> ouruser/imagename:tag
Docker extc-it <CONTAINER> & Lt COMMAND>: Executes the command in the container and outputs the result
- Network Management
Docker run-p: Randomly assigned port number Docker Run-p 5,000:5,000: Bind a specific port number (5000 ports for all network interfaces of the host are bound to 5000 ports of the container) Docker run-p 127.0.0.1:500 0:5,000: Port number of the specific interface of the binding host Docker run-d-P 127.0.0.1:5000:5000/udp training/webapp python app.py: Binding UDP port number Docker port < Container_id> 5000: View the 5000 port of the container corresponding to the IP and port number of the local machine
To connect containers using Docker linking:
-
Docker run-d-P--name web-v/webapp training/webapp python app.py
-
You can also attach a container volume to a host directory or a host file,< the contents of the container directory or file > are replaced with the contents of the < host directory or file >, and the default container has read and write access to the directory
.
Docker run-d-P--name web-v < host directory >:< container directory > Training/webapp python app.py< /pre> can change permissions to read-only by specifying RO
-----:< Container directory >:r trainingwebapp python apppy
Dock Er run-d-v/dbdata--name db1 training/postgres echo data-only container for Postgres
first starts a container and adds a number to the container According to volume/dbdata, and then start another container, share this data volume
Docker run
-D--volumes-from db1--name DB2 training/ Postgres
DB2 uses the DB1 container volume at this time, and when the container DB1 is deleted, the container volume is not deleted, but only if all containers are no longer using the container volume
Docker RM- V: Delete Container volume
In addition to sharing data, the container volume is used to back up, restore, and migrate data
Docker run--volumes-from db1-v/home/backup:/backup ubuntu tar cvf/backup/backup.tar/dbdata
Start a container data volume using the DB1 container's data volume while creating a new data volume pointing to the host directory/home/backup, compressing the/dbdata directory data to/backup/backup.tar
docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
Docker run--volumes-from dbdata2-v/home/backup:/backup busybox tar Xvf/backup/backup.tar
Start a container and extract the contents of the Backup.tar to the backup of the container
Warehouse Management
Docker Login: Login
Docker-image container Basic operations-common commands