Docker Initial (Mirror, Container, Warehouse)

image

Mirror image is the premise of Docker running container

Getting Mirror

docker pull NAME[:TAG] // Do not specify TAG, select latest tag by default

Running Mirror

docekr run -t -i ubuntu /bin/bash

View mirror information

docker images
  • Add a mirror label
docker tag ubuntu:latest my/ubuntu:latest
  • View Mirror Details
docker inspect image id
docker inspect -f {{".Architecture"}} id  // Query an item

Searching for Mirrors

docker search TERM
--automated=false Show only auto-created images
--no-trunc=false Non-truncated display of output information
-s=0 Display only mirrors rated above designated stars

delete mirror

docker rmi IMAGE[IMAGE...] where IMAGE can be label or id
  • Delete running images
Mandatory deletion of docker rmi-f Ubuntu (not recommended)
Recommendations: 1. Delete containers; 2. Delete mirrors with id
docker rm id  
docker rmi ubuntu 

create mirror

  • Creation Based on Existing Mirrors
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Options include:
- a,--author="" author information
 - m,--message="" submit information
 - p,--pause=true, suspend container operation on submission

Here is a presentation:

$ winpty docker run -ti ubuntu bash
root@39b31ce63c14:/# touch test
root@39b31ce63c14:/# 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

An image can be imported directly from an operating system template file, and 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 and imported it using the following commands:

[root@izwz909ewdz83smewux7a7z ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:14.04
sha256:57a7c0bb864c4185d5d9d6eb6af24820595482b9df956adec5fde8d16aa9cb7c
[root@izwz909ewdz83smewux7a7z ~]# docker images
  • Creation Based on Dockerfile

Save and load mirrors

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

Save a mirror image

$ 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
chong@L MINGW64 ~
$ docker save -o myubuntu_14.04.tar myubuntu:coderluo

DT pro with vide

chong@L MINGW64 ~
$ docker load < myubuntu_14.04.tar

Upload Mirror Image

docker push NAME[:TAG]

container

A container is a running instance of a mirror with an additional writable file layer

Create containers

New container

Creating containers with docker create is stopped and can be started with docker start

docker create -it ubuntu:latest

Create and start the container

root@ubuntu_server:/home/coderluo# docker run ubuntu /bin/echo 'i am coderluo'
i am coderluo

Equivalent to docker create first and then docker start command

Actions that docker run needs to perform:

  1. Check if there is a corresponding image locally, and no download from the shared repository.
  2. Create and start a container with a mirror.
  3. Allocate a file system and mount a read-write layer outside the read-only mirror layer.
  4. Bridge a virtual interface from the bridge interface configured by the host to the container.
  5. Assign an ip to the container;
  6. Execute user-specified applications;
  7. The container is closed after execution.

Next, we open a bash terminal to allow user interaction:

docker run -ti ubuntu bash

- t: Option lets Docker assign a pseudoterminal and bind it to the container's standard input

- i: Keep the standard input of the container open

Exit can exit the container, and the container will be terminated after exit, because for the Docker container, when the running application exits, the container will not be necessary to run.

Guardian operation

More commonly, Docker containers are required to run in a guardian state in the background. This can be achieved by adding the - d parameter:

$ 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

Termination container

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

It first sends a SIGTERM signal to the container and waits for a period of time (default 10s). The SIGKILL signal is sent to terminate the container.

Note: docker kill sends SIGKILL directly to force the termination of the container.

$ docker stop cae
cae

When the application running in the Docker container terminates, the container also terminates automatically. For example, when the terminal container opened above exit s the terminal, the created container will terminate.

Container ID information for all States of docekr ps-a-q can be used.

$ 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

Enter the container

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

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
root@b9953944f4cc:/# 

Disadvantage: When multiple windows are attach ed to the same container at the same time, all windows are displayed synchronously, and one block is blocked completely.

exec

Docker 1.3 has introduced a tool exec that can execute commands directly in containers.

Enter the previously created container and start a bash:

$ docker exec -ti b99 bash
root@b9953944f4cc:/#

nsenter

Third-party support, interested in their own google, personal feelings and exec similar

Delete containers

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

  • - f,--force=false forcibly terminates and deletes a running container
  • - l,--link=false deletes the connection of the container, but retains the container
  • - v,--volumes=false deletes containers 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

chong@L 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 container migration can be achieved directly by importing commands.

Import container

The exported file can be imported using the docker import command and mirrored.

$ 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 docker load of the previous mirror module is that:

The docker import is used to import a container snapshot to the local image library. It discards all historical records and metadata information (i.e. only the snapshot status of the container at that time). The docker load command loads the mirror file to save the completed records, and the volume is also large. And container snapshot import can redefine metadata information such as labels.

Warehouse

A warehouse is a place where mirrors are stored centrally.

Many people tend to confuse warehouses with registered servers. Here is the difference between registered servers and warehouses.

Registered servers are places where warehouses are stored. Each server can have multiple warehouses, and there are multiple mirrors under each warehouse. For example, ubuntu is a warehouse, and there are many different versions of the mirrors below. His server is the registration server.

Create and use private warehouses

Creating Private Warehouse Using Regisry Mirror

A simple local private warehouse environment can be built using the registry image provided by the government:

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

Description of parameters:

  • - 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. Default the directory / var/lib/registry to store the mirror files in the registry container

After running, test all mirrors in our private warehouse:

$ curl http://Warehouse Host ip:5000/v2/_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":[]}

It's empty now, because it's just running and there's no mirror content in it.

Managing Private Warehouse Mirrors

View existing mirrors on a test machine (non-warehouse machine) if no mirrors are currently available for download using docker pull;

  1. Label the mirror

    The format is docker tag IMAGE [: TAG] [REGIS TRYHOST/] [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 is the address and port of the private mirror registration server

  1. Upload to Mirror 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 shown in the curl command above, you can see the mirror in the warehouse.

  1. Test Download Mirror
$ docker rmi -f image id  # Delete local mirrors
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

$ docker pull 192.168.137.200:5000/ubuntu:v1 #Download Private Warehouse Mirror
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 mirrors
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 tag s for all a mirror:

$ 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"]}

Tags: Linux Docker Ubuntu curl snapshot

Posted on Sun, 18 Aug 2019 03:21:52 -0700 by ryanwood4