Image Management
build
Syntax: docker build [options] path | URL | -
Function: Builds an image using the Dockerfile in the specified path.
Parameter description: Common parameters are as follows. For details about more parameters, see the docker help build command section.
Table 1 Parameter description
Dockerfile Command
Dockerfile is used to describe how to build an image and automatically build a container. The format of all Dockerfile commands is instruction arguments.
FROM Command
Syntax: FROM image or FROM image:tag
Function: Specifies a basic image, which is the first command for all Dockerfile files. If the tag of a basic image is not specified, the default tag name latest is used.
RUN Command
Syntax: RUN command (for example, run in a shell - `/bin/sh -c`) or
RUN [executable, param1, param2 ... ] (in the exec command format)
Function: Runs any command in the image specified by the FROM command and then commits the result. The committed image can be used in later commands. The RUN command is equivalent to:
docker run image command
docker commit container_id
Remarks
The number sign (#) is used to comment out.
MAINTAINER Command
Syntax: **MAINTAINER **name
Function: Specifies the name and contact information of the maintenance personnel.
ENTRYPOINT Command
Syntax: ENTRYPOINT cmd param1 param2... or ENTRYPOINT ["cmd", "param1", "param2"...]
Function: Configures the command to be executed during container startup.
USER Command
Syntax: **USER **name
Function: Specifies the running user of memcached.
EXPOSE Command
Syntax: EXPOSE port [port...]
Function: Enables one or more ports for images.
ENV Command
Syntax: ENV_ key value_
Function: Configures environment variables. After the environment variables are configured, the RUN commands can be subsequently used.
ADD Command
Syntax: ADD_ src dst_
Function: Copies a file from the src directory to the dest directory of a container. src indicates the relative path of the source directory to be built. It can be the path of a file or directory, or a remote file URL. dest indicates the absolute path of the container.
VOLUME Command
Syntax: VOLUME ["mountpoint"]
Function: Creates a mount point for sharing a directory.
WORKDIR Command
Syntax: **workdir **path
Function: Runs the RUN, CMD, and ENTRYPOINT commands to set the current working path. The current working path can be set multiple times. If the current working path is a relative path, it is relative to the previous WORKDIR command.
CMD command
Syntax: CMD ["executable","param1","param2"] (This command is similar to the exec command and is preferred.)
CMD ["param1","param2"] (The parameters are the default parameters for ENTRYPOINT.)
CMD command param1 param2 (This command is similar to the shell command.)
Function: A Dockerfile can contain only one CMD command. If there are multiple CMD commands, only the last one takes effect.
ONBUILD Commands
Syntax: ONBUILD [other commands]
Function: This command is followed by other commands, such as the RUN and COPY commands. This command is not executed during image build and is executed only when the current image is used as the basic image to build the next-level image.
The following is a complete example of the Dockerfile command that builds an image with the sshd service installed.
Example:
Run the following command to build an image using the preceding Dockerfile:
$ sudo docker build -t busybox:latest
Run the following command to view the generated image:
docker images | grep busybox
history
Syntax: docker history [options] image
Function: Displays the change history of an image.
Parameter description:
-H, --human=true
--no-trunc=false: Does not delete any output.
-q and --quiet=false: Display only IDs.
Example:
$ sudo docker history busybox:test
IMAGE CREATED CREATED BY SIZE COMMENT
be4672959e8b 15 minutes ago bash 23B
21970dfada48 4 weeks ago 128MB Imported from -
images
Syntax: docker images [options] [name]
Function: Lists existing images. The intermediate image is not displayed if no parameter is configured.
Parameter description:
-a and --all=false: Display all images.
-f and --filter=[]: Specify a filtering value, for example, dangling=true.
--no-trunc=false: Does not delete any output.
-q and --quiet=false: Display only IDs.
Example:
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest e02e811dd08f 2 years ago 1.09MB
import
Syntax: docker import URL|- [repository[:tag]]
Function: Imports a .tar package that contains rootfs as an image. This parameter corresponds to the docker export command.
Parameter description: none.
Example:
Run the following command to generate a new image for busybox.tar exported using the docker export command:
$ sudo docker import busybox.tar busybox:test
sha256:a79d8ae1240388fd3f6c49697733c8bac4d87283920defc51fb0fe4469e30a4f
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox test a79d8ae12403 2 seconds ago 1.3MB
load
Syntax: docker load [options]
Function: Reloads an image from .tar package obtained by running the docker save command. This parameter corresponds to the docker save command.
Parameter description:
-i and --input="" can be used.
Example:
$ sudo docker load -i busybox.tar
Loaded image ID: sha256:e02e811dd08fd49e7f6032625495118e63f597eb150403d02e3238af1df240ba
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest e02e811dd08f 2 years ago 1.09MB
login
Syntax: docker login [options] [server]
Function: Logs in to an image server. If no server is specified, the system logs in to https://index.docker.io/v1/ by default.
Parameter description:
-e and --email="": Email address.
-p and --password="": Password.
-u and --username="": User name.
Example:
$ sudo docker login
logout
Syntax: docker logout [server]
Function: Logs out of an image server. If no server is specified, the system logs out of https://index.docker.io/v1/ by default.
Parameter description: none.
Example:
$ sudo docker logout
pull
Syntax: docker pull [options] name[:tag]
Function: Pulls an image from an official or private registry.
Parameter description:
-a and --all-tags=false: Download all images in a registry. (A registry can be tagged with multiple tags. For example, a busybox registry may have multiple tags, such as busybox:14.04, busybox:13.10, busybox:latest. If -a is used, all busybox images with tags are pulled.)
Example:
Run the following command to obtain the Nginx image from the official registry:
$ sudo docker pull nginx Using default tag: latest latest: Pulling from official/nginx 94ed0c431eb5: Pull complete 9406c100a1c3: Pull complete aa74daafd50c: Pull complete Digest: sha256:788fa27763db6d69ad3444e8ba72f947df9e7e163bad7c1f5614f8fd27a311c3 Status: Downloaded newer image for nginx:latest
When an image is pulled, the system checks whether the dependent layer exists. If yes, the local layer is used.
Pull an image from a private registry.
Run the following command to pull the Fedora image from the private registry, for example, the address of the private registry is 192.168.1.110:5000:
$ sudo docker pull 192.168.1.110:5000/fedora
push
Syntax: docker push name[:tag]
Function: Pushes an image to the image registry.
Parameter description: none.
Example:
Run the following command to push an image to the private image registry at 192.168.1.110:5000.
Label the image to be pushed. (The docker tag command is described in the following section.) In this example, the image to be pushed is busybox:sshd.
$ sudo docker tag ubuntu:sshd 192.168.1.110:5000/busybox:sshd
Run the following command to push the tagged image to the private image registry:
$ sudo docker push 192.168.1.110:5000/busybox:sshd
During the push, the system automatically checks whether the dependent layer exists in the image registry. If yes, the layer is skipped.
rmi
Syntax: docker rmi [options] _image _[image...]
Function: Deletes one or more images. If an image has multiple tags in the image library, only the untag operation is performed when the image is deleted. If the image has only one tag, the dependent layers are deleted in sequence.
Parameter description:
-f and --force=false: Forcibly delete an image.
--no-prune=false: Does not delete parent images without tags.
Example:
$ sudo docker rmi 192.168.1.110:5000/busybox:sshd
save
Syntax: docker save [options] _image _[image...]
Function: Saves an image to a TAR package. The output is STDOUT by default.
Parameter description:
-o and --output="": Save an image to a file rather than STDOUT.
Example:
$ sudo docker save -o nginx.tar nginx:latest
$ ls
nginx.tar
search
Syntax: **docker search **options TERM
Function: Searches for a specific image in the image registry.
Parameter description:
--automated=false: Displays the automatically built image.
--no-trunc=false: Does not delete any output.
-s and --stars=0: Display only images of a specified star level or higher.
Example:
Run the following command to search for Nginx in the official image library:
$ sudo docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 11873 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 1645 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 739 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS... 74 bitnami/nginx Bitnami nginx Docker Image 70 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp... 51 [OK]
Run the following command to search for busybox in the private image library. The address of the private image library must be added during the search.
$ sudo docker search 192.168.1.110:5000/busybox
tag
Syntax: docker tag [options] image[:tag] [registry host/][username/]name[:tag]
Function: Tags an image to a registry.
Parameter description:
-f or --force=false: Forcibly replaces the original image when the same tag name exists.
Example:
$ sudo docker tag busybox:latest busybox:test