First, the use of Docker hub
Docker Hub is the official Docker maintenance warehouse, which already contains a lot of mirrors, generally our needs directly in the official warehouse search can be resolved. In the official public warehouse we can search and download images without having to log in.
Search by Docker Search command
Docker pull command to download
Search Image Command Detailed usage: Docker search [OPTIONS] Term
[Email protected]:~# sudo docker pull Ubuntu
Using default Tag:latest
Latest:pulling from Library/ubuntu
Af49a5ceb2a5:pull Complete
8f9757b472e7:pull Complete
E931b117db38:pull Complete
47b5e16c0811:pull Complete
9332eaf1a55b:pull Complete
Digest:sha256:3b64c309deae7ab0f7dbdd42b6b326261ccd6261da5d88396439353162703fb5
status:downloaded newer image for Ubuntu:latest
Once you're done, you can use the image at any time, such as creating a container to run the Bash app.
$ sudo docker run -t -i ubuntu /bin/bash[email protected]:/#
Docker images list mirrors
$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu latest 99ec81b80c55 4 weeks ago 266 MB
In the listing information, you can see several field information
- from which warehouse, such as Ubuntu
- Mirrored tags, such as 14.04
- It's
ID
number (unique)
- Creation time
- Mirror size
Where the mirrors ID
uniquely identify the mirror, notice ubuntu:14.04
and ubuntu:trusty
have the same image ID
, stating that they are actually the same mirror.
TAG
Information is used to mark different images from the same warehouse. For example, ubuntu
there are multiple mirrors in the warehouse, and information is available TAG
to differentiate the distribution, for example,,,, 10.04
, and 12.04
12.10
13.04
14.04
so on. For example, the following command specifies that mirroring is used ubuntu:14.04
to start a container.
$ sudo docker run -t -i ubuntu /bin/bash
If you do not specify a specific tag, the tag information is used by default latest
.
REPOSITORY TAG IMAGE ID CREATED SIZE
Ubuntu latest 4CA3A192FF2A 2 weeks ago 128.2 MB
The Docker commit command to submit the updated copy.
$ sudo docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v24f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c
Where -m
you specify the descriptive information for the submission, like the version Control tool we use, -a
you can specify updated user information, then the ID of the container used to create the mirror, and finally specify the warehouse name and tag information for the target image. The ID information for this image is returned when the creation is successful.
Use docker images
to view the newly created image.
$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu latest 99ec81b80c55 4 weeks ago 266 MBouruser/sinatra v2 3c59e02ddd1a 10 hours ago 446.7 MB
After that, you can use the new mirror to start the container
$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash[email protected]:/#
Docker Save and Docker load Import export Image
If you want to export the image to a local file, you can use the docker save
command.
$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu latest c4ff7513909d 5 weeks ago 225.4 MB...$sudo docker save -o ubuntu.tar ubuntu
You can use the docker load
local file from the export to import to the local mirror library, for example
$ sudo docker load --input ubuntu.tar
Or
$ sudo docker load < ubuntu.tar
This imports the image and its associated metadata information (including tags, and so on).
Docker rmi Remove Mirror
$ sudo docker rmi ouruser/sinatra:v2Untagged: ouruser/sinatra:v2Deleted: 5bc342fa0b91cabf65246837015197eecfa24b2213ed6a51a8974ae250fedd8dDeleted: ed0fffdcdae5eb2c3a55549857a8be7fc8bc4241fb19ad714364cbfd7a56b22fDeleted: 5c58979d73ae448df5af1d8142436d81116187a7633082650549c52c3a2418f0
* Note: Delete docker rm
all containers that depend on this image before deleting the image.
container of the 5.Docker Command (container)Docker run boot container
There are two ways to start a container, one is to create a new container based on the mirror and start, and the other is to restart the container in the terminating state (stopped).
Because Docker containers are too lightweight, many times users are deleting and creating new containers at any time.
New and started
The required commands are mainly for docker run
.
For example, the following command outputs a "Hello world" and then terminates the container.
$ sudo docker run ubuntu /bin/echo ‘Hello world‘Hello world
This is almost indistinguishable from local direct execution /bin/echo ‘hello world‘
.
The following command launches a bash terminal that allows the user to interact.
$ sudo docker run -t -i ubuntu /bin/bash[email protected]:/#
Where the -t
option allows Docker to assign a pseudo terminal (Pseudo-tty) and bind to the container's standard input, leave -i
the container's standard input open.
In interactive mode, users can enter commands through the terminal they create, such as
[email protected]:/# lsbin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
More often, you need to let the Docker container run in the background in a daemon (daemonized) mode. At this point, you can add -d
parameters to implement.
For example, the following command runs the container in the background.
$ sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
When the container starts, it returns a unique ID, or the docker ps
container information can be viewed through a command.
$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1e5535038e28 ubuntu /bin/sh -c ‘while tr 2 minutes ago Up 1 minute insane_babbage
To get the output information for a container, you can pass the docker logs
command.
$ sudo docker logs insane_babbagehello worldhello worldhello world. . .
When used docker run
to create a container, the standard operations that Docker runs in the background include:
- Checks if there is a specified image locally and does not exist to download from the public repository
- Create and start a container with mirroring
- Assign a file system and mount a layer of read-write layers outside the mirrored layer
- Bridging a virtual interface into a container from the bridge interface configured by the host host
- Configure an IP address from the address pool to the container
- Executing user-specified applications
- Container is terminated when execution is complete
start a terminated container
The command can be used to docker start
start a terminated container directly.
The core of the container is the application that is executed, and the resources required are required for the application to run. In addition, there are no other resources. You can use ps
or view process information in a pseudo terminal top
.
[email protected]:/# ps PID TTY TIME CMD 1 ? 00:00:00 bash 11 ? 00:00:00 ps
Visible, only the specified bash app is running in the container. This feature makes Docker highly utilized for resources and is a genuine lightweight virtualization.
Can be used docker stop
to terminate a running container.
In addition, the container terminates automatically when the application specified in the Docker container is terminated. For example, in the previous section, only a container for a terminal was started, and when the user exit
exits the terminal by command or, the container that is Ctrl+d
created terminates immediately.
The container of the terminating state can be seen with the docker ps -a
command. For example
sudo docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTSNAMESba267838cc1b ubuntu "/bin/bash" 30 minutes ago Exited (0) About a minute ago trusting_newton1e5535038e28 ubuntu /bin/sh -c ‘while tr 2 minutes ago Up 1 minute insane_babbage
A container in the terminating state, which can be docker start
restarted by command.
In addition, the docker restart
command terminates a run-state container and then restarts it.
When using
-d
parameter, the container starts to enter the background when it is started. There are a number of ways to get into a container at some point, including using
docker attach
commands or
nsenter
tools.
docker attach
Is the command that comes with Docker. The following example how to use this command.
$ sudo docker run -idt ubuntu243c32535da7d142fb0e6df616a3c3ada0b8ab417937c853a9e1c251f499f550$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES243c32535da7 ubuntu:latest "/bin/bash" 18 seconds ago Up 17 seconds nostalgic_hypatia$sudo docker attach nostalgic_hypatia[email protected]:/#
An example is given above.
Import and export containers for Docker import and Docker export
If you want to export a local container, you can use the docker export
command.
$ sudo docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7691a814370e ubuntu:14.04 "/bin/bash" 36 hours ago Exited (0) 21 hours ago test$ sudo docker export 7691a814370e > ubuntu.tar
This will export the container snapshot to a local file.
You can use the docker import
import from a container snapshot file as a mirror, for example
$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEtest/ubuntu v1.0 9d37a6082e97 About a minute ago 171.3 MB
Alternatively, you can import by specifying a URL or a directory, such as
$sudo docker import http://example.com/exampleimage.tgz example/imagerepo
* Note: Users can use both to docker load
import a mirrored storage file to a local mirror library, or to docker import
import a container snapshot to a local mirror library. The difference between the two is that the container snapshot file discards all history and metadata information (that is, only the snapshot state of the container at that time), while the mirrored storage file will hold the full record and be larger in size. In addition, metadata information such as labels can be re-specified when importing from a container snapshot file.
Delete ContainerDocker RM to delete a container that is in the signaled state. For example
$sudo docker rm trusting_newtontrusting_newton
If you want to delete a running container, you can add -f
parameters. Docker sends a SIGKILL
signal to the container.
More commands can be viewed in the terminal input Docker--help.
How to use the Docker on Ubuntu 16.04