Basic usage of docker

For the network configuration of RHEL/CentOS under vmware, please refer to: http://jisonami.iteye.com/blog/2306735

RHEL/CentOS only fully natively supports docker in the 7 series. All previous examples were demonstrated using CentOS7, and all operations were performed in Under root privileges.
This article introduces the most commonly used basic usage of docker, including
1. Introduction and installation
of docker 2. Docker image and container

1. Introduction and installation

of docker 1. Introduction to
docker Docker is a lightweight container technology that directly uses the resources of the operating system instead of way of a virtual machine.
Docker is a virtualization technology at the operating system level, which simulates the operating environment of the operating system. A virtual machine is a virtualized operating system. The former occupies less resources than the latter, so it is very efficient.
Docker containers start much faster than virtual machines, generally measured in seconds.

2. Docker installation
yum install docker

Second, the three keywords of docker image and container
docker: warehouse, image,
container The relationship between these three is like this, the warehouse stores many images, and each image can run many containers
Official warehouse It is http://index.docker.io

1. Relevant commands and usage demonstrations for operating docker images
1) Find images from the warehouse
Command : docker search image name
Take redis as an example
docker search docker.io/redis

2) Download the image from the warehouse to the local
command : docker pull image name
with tag name, take downloading redis image as an example,
docker pull redis:2.8.21
If there is no tag name, it will download the latest ( latest) version
docker pull redis

3) View image list
Command : docker images

4) Delete image
Command : docker rmi Image id
deleted redis image as an example
First check the image list and find the image id to be deleted
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/redis 2.8.21 1a721decd792 10 months ago 109 MB
delete redis image with IMAGE ID 1a721decd792
docker rmi 1a721decd792

5) delete all images
docker rmi $(docker images -q)

6) image export import
Export command: docker save -o tar package name Image repository: image tag
Example :
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/redis 2.8.21 1a721decd792 10 months ago 109 MB
[root@ localhost ~]# docker save -o redis2.8.21.tar docker.io/redis:2.8.21
[root@localhost ~]# ls
anaconda-ks.cfg redis2.8.21.tar

Import command: docker load < tar package name
Example : Delete the original image first and then import it. Before deleting the image, you need to delete all containers based on the image
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0        1a721decd792        "/entrypoint.sh redis"   52 minutes ago      Exited (0) 26 minutes ago                       tiny_mccarthy
[root@localhost ~]# docker rm 985b07080de0
985b07080de0
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/redis     2.8.21              1a721decd792        10 months ago       109 MB
[root@localhost ~]# docker rmi 1a721decd792
Untagged: docker.io/redis:2.8.21
Deleted: 1a721decd792ad1d3c4ebc315d34b1f8d4dfb97e6d8013efe6c523e637361bd6
Deleted: 1e0f123a26af5e26e27b7bcef888957657e21992f94beeb7f15b311731a94522
Deleted: 917499e60e0beb9aef579ab3bda18852c3dae3068608464a442fd1004929f350
Deleted: bdd393eaafef5acb1fa55708831d05d4203619bb46a84cf687fe9d85ce01d4e7
Deleted: 3d78ebcf73261a592159a96275f31eb08165ed128d7a775d97725d8d11e7e1ef
Deleted: 2da1436f270c273773122e93f8668672b8b7923af8d68374de4cfa5feeaeccd3
Deleted: fb03d161db1463c2208715b1df9af461a00cb2b74b64478ec131f727c5204629
Deleted: 87b88b774507ab4e2ee76c90b00f6fd9a0c8e7457c265e8256fcb6ac4fef8e28
Deleted: 6846105605cc29e8e7a65244961d5f82343ec2fb2ac824372ec23c198318a3b4
Deleted: d60d207d76b30a03d1da1100a2ed24f8a798104c73be5058ddb30abc6145be48
Deleted: b2508f29bec6010c47e4ca1ebd899252baa8e0645421017728897322368bbb34
Deleted: 7bf6d72c51dd7602557d3f7c1474f659d56819444a49a7f924df8e014cd756c3
Deleted: 2658b5be63c6886e82374815fd97e0593976fda3c749f531c88d2ec968d40709
Deleted: 047a294381b2ec81cbfb1d5710b65d641548b99d5e35dab761179ccddc36bacd
Deleted: 9bce9474876b400ca739a85e246c2677ebacc8440558fab04c764a76c7a52d78
Deleted: bbe78c1a5a535fac669e3225d5c3bb4396b6b2f9decb560ffb6351396da8c345
Deleted: 9d3ceacde91b4c7d6c1275032adb558d668afd5489c007f3512b39793ddf992d
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
[root@localhost ~]# docker load < redis2.8.21.tar
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/redis 2.8.21 1a721decd792 10 months ago 109 MB

Note:
It is also possible to export an image using the image id instead of "mirror repository:image tag", but the metadata (ie repository information and Tag name, etc.)
The information you see after re-importing is as follows
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
<none> <none> 1a721decd792 10 months ago 109 MB

You can see that the imported image has no metadata (ie warehouse information and label name, etc.), this is because the mirror id is used when exporting. The
solution is to use "mirror warehouse: mirror label" instead of the mirror id when exporting the mirror.

2, related commands for operating docker containers and Usage demonstration
1) View running containers
docker ps
View all containers including closed containers
docker ps -a

2) Start a container from an image
Command: docker run option image name <command>
The command after the image can be omitted.

Take starting the redis container as an example.
docker run -d -p 6379:6379 1a721decd792 After
starting the redis container, we can use the redis-cli command line client to connect to the redis server
[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
Parameter explanation:
-d means start a container running in the background
-p means specify the container port number, The specified format can be
ip:host port:container port| ip::container port|host port:container port|container port
If no host port is specified, it will be randomly generated

Take the example of starting bash and getting the console standard input
docker run - t -i 1a721decd792 /bin/bash
parameter explanation:
-t Let Docker allocate a pseudo terminal (pseudo-tty) and bind it to the standard input of the
container -i Keep the standard input of the container open

3. Close the running container
command : docker stop container id
Take closing the redis container just started as an example.
First use the docker ps command to find the information of the running container, and close the container according to the found container id.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0 1a721decd792 "/entrypoint.sh redis" 6 seconds ago Up 4 seconds 0.0.0.0:6379->6379/tcp tiny_mccarthy
[root@localhost ~]# docker stop 985b07080de0
985b07080de0

4. Start the closed container
Command : docker start container id
Take the redis container that has just been shut down as an example.
First use the docker ps -a command to find all container information, and close the container according to the found container id
[root@ localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0 1a721decd792 "/entrypoint.sh redis" 2 minutes ago Exited (0) 2 minutes ago tiny_mccarthy
[root@localhost ~]# docker start 985b07080de0
985b07080de0

5. Restart the running container
Command : docker restart The container id
first passes the docker ps command Find the running container information and close the container according to the found container id
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0 1a721decd792 "/entrypoint.sh redis" 6 seconds ago Up 4 seconds 0.0.0.0 :6379->6379/tcp tiny_mccarthy
[root@localhost ~]# docker restart 985b07080de0
985b07080de0

6. Enter the running background docker container
1) The attach command
docker attach container id
(take redis as an example, if the docker attach command is used, it is directly connected to the input stream of redis-server, no matter how many docker attaches are opened, it is only connected to the input stream, and all windows are refreshed synchronously)
Here is an example of starting bash and getting console standard input, thinking that the redis container is not intuitive
[root@localhost ~]# docker run -d -t -i 1a721decd792 /bin/bash
168a188ae07eb437d2ef33f22a30624c667c3eea2be5300ef51641df440c1610 #Starting the container is the complete container id returned, Here the short container id queried by docker ps
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning .
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
168a188ae07e 1a721decd792 "/entrypoint.sh /bin/" 22 seconds ago Up 21 seconds 6379/tcp clever_saha
[root@localhost ~]# docker attach 168a188ae07e
root@168a188ae07e:/data# pwd
/data

2) nsenter command to
install util-linux package , the nsenter command is in this package, the util-linux in the centos7 warehouse is exactly the version 2.23 required to connect the docker container
yum install util-linux
docker gets the pid
docker inspect --format "{{ .State.Pid}}" container id
Use the nsenter command to connect
nsenter --target 3212 --mount --uts --ipc --net --pid

Complete example: (Take redis as an example, if you use the docker attach command, you can directly connect to the input stream of redis-server, No matter how many docker attaches are opened, only connect to this input stream, and refresh all windows synchronously)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0 1a721decd792 "/entrypoint.sh redis" 20 minutes ago Up 6 seconds 0.0.0.0:6379->6379/tcp tiny_mccarthy
[root@localhost ~]# docker start 985b07080de0
985b07080de0
[root ~localhost@localhost ]# docker inspect --format "{{ .State.Pid}}" 985b07080de0
8660
[root@localhost ~]# nsenter --target 8660 --mount --uts --ipc --net --pid
root@985b07080de0: /# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>

7. Delete the closed container
Command : docker rm container id
Here, delete the first record as an example
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
168a188ae07e 1a721decd792 "/entrypoint.sh /bin/" 4 minutes ago Exited (130) 33 seconds ago clever_saha
985b07080de0 1a721decd792 "/entrypoint.sh redis" 16 minutes ago Exited (0) 7 minutes ago tiny_mccarthy
[root@localhost ~]# docker rm 168a188ae07e
168a188ae07e

8. Delete all closed containers
docker rm $(docker ps -a -q)

9. Container export and import
1) Container Export as tar package
Command : docker export container id > export tar package name
Example:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985b07080de0 1a721decd792 "/entrypoint.sh redis" 27 minutes ago Exited (0) 18 seconds ago tiny_mccarthy
[root@localhost ~]# docker export 985b07080de0 > redis.tar
[root@localhost ~]# ls
anaconda-ks.cfg redis.tar

2) Import the image from the tar package
Here is the image after import, not the container, and the image metadata needs to be renamed
Command: cat export the exported tar package | docker import - local warehouse name: label name
Example:
[root@localhost ~]# cat redis.tar | docker import - testimport/redis:2.8.21
3d970cce18b58b71b5f83cb2778649a5b36c07de5cc824a758dcc2000668740b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
testimport/redis 2.8.21 3d970cce18b5 6 seconds ago 107.3 MB docker.io/redis 2.8.21
1a721decd792 10 months ago 109 MB If the command is added, the imported image must add a command at the end when starting the container, otherwise the startup will fail. That is, the original docker run -d -p 6379:6379 3d970cce18b5 must now be docker run -d -p 6379:6379 3d970cce18b5 redis -server example: [root@localhost ~]# docker run -d -p 6379:6379 3d970cce18b5 redis-server 7ad52d24fb8cad1d4bcb302355cbcd3e3d0ccb44ff6bdf5cb096271ecfc92419








Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ad52d24fb8c 3d970cce18b5 "redis-server" 3 seconds ago Up 2 seconds 0.0.0.0:6379->6379/tcp cranky_poincare

here is a pit
docker ps the command viewed may be wrong, it may be Because of an explicit length limit, like
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
168a188ae07e 1a721decd792 "/entrypoint.sh /bin/" 4 minutes ago Exited (130) 33 seconds ago clever_saha
985b07080de0 1a721decd792 "/entrypoint.sh redis" 16 minutes ago Exited (0) 7 minutes ago tiny_mccarthy
started with /bin/ The bash command, here is "/entrypoint.sh /bin/" explicitly, it should be "/entrypoint.sh /bin/bash"
without a command when starting redis, here is "/entrypoint.sh redis" ", it should be "/entrypoint.sh redis-server"

Please note that save/load exports and imports images and export/import exports container snapshots and imports images cannot be mixed
redis2.8.21.tar is the image exported by save
redis.tar It is the container snapshot exported by export
[root@localhost ~]# ls
anaconda-ks.cfg redis2.8.21.tar redis.tar
[root@localhost ~]# cat redis2.8.21. tar | docker import - docker.io/redis:2.8.21
Error response from daemon: EOF
[root@localhost ~]# docker load < redis.tar
Error response from daemon: open /var/lib/docker/tmp/docker-import-994843880/repo/bin/json: no such file or directory


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326671791&siteId=291194637