Getting started with docker (image, container, warehouse)

Time:2019-12-10

image

Image is the premise of docker running container

Get mirror image

Docker pull name [: tag] // do not specify tag, select latest tag by default

Running mirroring

docekr run -t -i ubuntu /bin/bash

View image information

docker images
  • Add image label
docker tag ubuntu:latest my/ubuntu:latest
  • View image details
Docker inspection image ID
Docker inspect - F {". Architecture"}} ID // query a content

Search for mirrors

docker search TERM
--Automated = false only shows automatically created images
--No TRUNC = false output information does not truncate display
-S = 0 shows only images rated above the designated star level

delete mirror

Docker RMI image [image...] where image can be a label or ID
  • Delete a running image
Docker RMI - f Ubuntu force delete (not recommended)
Recommendation: 1. Delete the container; 2. Delete the image with ID
docker rm id  
docker rmi ubuntu

create mirror

  • Create based on existing image
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Options include:
-A, -- author = "" author information
-M, -- message = "" submit message
-P, -- pause = true pause container on commit

Here is a demonstration:

$ winpty docker run -ti ubuntu bash
[email protected]:/# touch test
[email protected]:/# exit
#View container ID
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
39b31ce63c14        ubuntu              "bash"              12 minutes ago      Exited (0) 11 minutes ago                       friendly_chebyshev

$ docker commit -m "added a new file" -a "coderluo" 39b test:coderluo
sha256:489150941c65c552268ddacd10d9fe05c01e30c8c3bd111e4217d727e8f724c4
  • Import based on local template

You can import an image directly from an operating system template file. It is recommended to use the template provided by OpenVZ to create it. The download address is:

https://wiki.openvz.org/Download/template/precreated

For example, I downloaded a Ubuntu, which can be imported using the following command:

[[email protected] ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:14.04
sha256:57a7c0bb864c4185d5d9d6eb6af24820595482b9df956adec5fde8d16aa9cb7c
[[email protected] ~]# docker images
  • Create based on dockerfile

Save out and load images

You can use the docker save and docker load commands to save and load images

Save mirrors

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            coderluo            489150941c65        About an hour ago   64.2MB
ubuntu              latest              a2a15febcdf3        43 hours ago        64.2MB
ubuntu              14.04               271ca7812183        3 months ago        188MB
[email protected] MINGW64 ~
$ docker save -o myubuntu_14.04.tar myubuntu:coderluo

Loading mirroring

[email protected] MINGW64 ~
$ docker load < myubuntu_14.04.tar

Upload mirroring

docker push NAME[:TAG]

container

The container is a running instance of the image with an additional writable file layer

Create container

New container

Use docker create to create a container and the market is in a stopped state. You can use docker start to start

docker create -it ubuntu:latest

New and start container

[email protected]_server:/home/coderluo# docker run ubuntu /bin/echo 'i am coderluo'
i am coderluo

Equivalent to docker create and then docker start command

Actions to be performed by docker run:

  1. Check whether there is a corresponding image in the local area. If not, download it from the shared warehouse;
  2. Create and start a container with the image;
  3. Allocate a file system and mount a read-write layer outside the read-only image layer;
  4. Bridge a virtual interface to the container from the bridge interface configured by the host;
  5. Assign an IP to the container;
  6. Execute user specified application;
  7. Close the container after execution;

Next, we open a bash terminal to allow users to interact:

docker run -ti ubuntu bash

-t :Option allows docker to assign a pseudo terminal and bind to the container’s standard input

-i :Keep container standard input open

Exit allows you to exit the container. After exiting, the container will be in the termination state. For the docker container, when the running application exits, the container is not necessary to run;

Guard operation

It is common to require docker containers to run in the form of daemons in the background. You can add-dParameters to achieve:

$ docker run -d ubuntu sh -c "while true; do echo hello world; sleep 1; done"
caedc06b26723ec1aff794a053835d2b0b603702bea8a5bb3a39e97b0adf5654

$ docker logs cae
hello world
hello world
hello world
hello world
hello world
hello world

Terminating container

docker stop [-t|--time[=10]]

It will first send SIGTERM signal to the container and wait for a period of time (default 10s). Then send sigkill signal to terminate the container.

Note: docker kill will send sigkill directly to forcibly terminate the container.

$ docker stop cae
cae

When the application running in the docker container ends, the container will also automatically terminate. For example, if the terminal container opened above exits the terminal, the created container will also terminate.

have access todocekr ps -a -qContainer ID information for all States.

$ docker ps -a -q
90bcf718ad13
caedc06b2672
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
caedc06b2672        ubuntu              "sh -c 'while true; …"   17 minutes ago      Up About a minute                       epic_swartz
$ docker restart cae
cae
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
caedc06b2672        ubuntu              "sh -c 'while true; …"   18 minutes ago      Up 8 seconds                            epic_swartz

Container entry

When the container is started in the background, the user cannot enter the container. If you need to enter the container for operation, you can use the following methods:

Attach command

$ docker run -idt ubuntu
b9953944f4cc4a17d09bba846d40eea25523098d188d44484f814132e3a04ae7
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b9953944f4cc        ubuntu              "/bin/bash"         7 seconds ago       Up 5 seconds                            laughing_chatterjee
$ docker attach laughing_chatterjee
[email protected]:/# 

Disadvantages: when multiple windows are attached to the same container at the same time, all windows are displayed synchronously, and one block blocks all windows.

exec

From docker 1.3, exec, a tool that can execute commands directly in containers, has been introduced.

Enter the previously created container and start a bash:

$ docker exec -ti b99 bash
[email protected]:/#

nsenter

Third party support. You can Google if you are interested. I feel like exec

Delete container

docker rm [OPTIONS] CONTAINER [CONTAINER...]

  • -F, — force = false to forcibly terminate and delete a running container
  • -L, — link = false delete the connection of the container, but keep the container
  • -V, — volumes = false delete container mounted data volumes
$ docker rm 90b
90b

$ docker rm b99
Error response from daemon: You cannot remove a running container b9953944f4cc4a17d09bba846d40eea25523098d188d44484f814132e3a04ae7. Stop the container before attemptin
g removal or force remove

[email protected] MINGW64 ~
$ docker rm -f b99
b99

Import and export containers

Export container

docker export CONTAINER
docker export cae > test_for_run.tar

The exported files can be transferred to other machines, and the container migration can be realized directly through the Import command.

Import container

The exported file can be imported using the docker Import command to become a mirror.

$ cat Desktop/test_for_run.tar | docker import - test/ubuntu:v1.0                                                       sha256:aa9dd6a88eb02d192c0574e1e2df171d0ec686a21048cba9a70fcd9ce3ba7d76
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.0                aa9dd6a88eb0        11 seconds ago      64.2MB

The difference between this and the loading image of docker load in the previous image module is as follows:

Docker import is used to import a container snapshot to the local image library. It discards all historical records and metadata information (that is, only the snapshot status of the container at that time). When docker load command loads the image file, the completed records will be saved, and the volume will be large. In addition, container snapshot import can re define metadata information such as labels.

Warehouse

The warehouse is a place for centralized image storage

Many people are easy to confuse the warehouse with the registration server. Here’s the difference between registering a server and a warehouse.

The registration server is the place where the warehouse is stored. Each server can have multiple warehouses, and there are multiple images under each warehouse. For example, Ubuntu is a warehouse, and there are many different versions of images below. His server is the registration server.

Create and use private warehouses

Creating a private warehouse using a registry image

You can use the official registry image to build a set of local private warehouse environment:

docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry

Parameter Description:

  • -D. background operation
  • -P, port mapping
  • -V. bind the host’s / opt / data / registry to / var / lib / registry to store the data in the local path. By default, the directory where the image files are stored in the registry container is / var / lib / registry

After running, test all the images in our private warehouse:

$curl http: // warehouse host IP: 5000 / V2 / 65124; catalog
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    20  100    20    0     0     20      0  0:00:01 --:--:--  0:00:01   160{"repositories":[]}

Now it’s empty, because it’s just running, and there’s no image content in it.

Manage private warehouse images

View the existing image on a test machine (not a warehouse machine). If there is no image currently, use docker pull to download it;

  1. Label image

    The format is:docker tag IMAGE[:TAG] [REGISTRYHOST/] [USERNAME/] NAME[:TAG]

docker tag ubuntu:latest 192.168.137.200:5000/ubuntu:v1
$ docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.137.200:5000/ubuntu   v1                  a2a15febcdf3        3 days ago          64.2MB

192.168.137.200:5000 address and port of private image registration server

  1. Upload to image server

$ docker push 192.168.137.200:5000/ubuntu
The push refers to repository [192.168.137.200:5000/ubuntu]
122be11ab4a2: Pushed
7beb13bce073: Pushed
f7eae43028b3: Pushed
6cebf3abed5f: Pushed
v1: digest: sha256:ca013ac5c09f9a9f6db8370c1b759a29fe997d64d6591e9a75b71748858f7da0 size: 1152
$ curl http://192.168.137.200:5000/v2/_catalog
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    28  100    28    0     0     28      0  0:00:01 --:--:--  0:00:01   198{"repositories":["ubuntu"]}

As found by the curl command above, you can see the image in the warehouse.

  1. Test Download Image
$docker RMI - f image ID - delete local image
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

$docker pull 192.168.137.200:5000 / Ubuntu: V1 download private warehouse image
v1: Pulling from ubuntu
35c102085707: Pull complete
251f5509d51d: Pull complete
8e829fe70a46: Pull complete
6001e1789921: Pull complete
Digest: sha256:ca013ac5c09f9a9f6db8370c1b759a29fe997d64d6591e9a75b71748858f7da0
Status: Downloaded newer image for 39.108.186.135:5000/ubuntu:v1
$docker images view local image
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.137.200:5000/ubuntu   v1                  a2a15febcdf3        3 days ago          64.2MB

List all mirrors:

$ curl 39.108.186.135:5000/v2/_catalog
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    28  100    28    0     0     28      0  0:00:01 --:--:--  0:00:01   254{"repositories":["ubuntu"]}

All the tags of a mirror warehouse:

$ curl http://39.108.186.135:5000/v2/ubuntu/tags/list
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    32  100    32    0     0     32      0  0:00:01 --:--:--  0:00:01   128{"name":"ubuntu","tags":["v1"]}