Docker Introduction (1) - Warehouses, Containers, Mirrors

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and then publish them to any popular Linux machine, as well as virtualization. Containers are completely sandboxed and do not have any interfaces with each other.

The advent of Docker has allowed us to solve many of the problems in engineering. Here's a look at the features and advantages of Docker

Docker characteristics

  • Warehouse, as its name implies, is a whole warehouse for docker storage. It can be classified by redis, mysql, nginx, etc. or by engineering a1, a2, a3.

  • Mirror - the static reflection of the running environment is the "running environment" which is well integrated but not up to date.

  • Container - A "runtime environment" that runs by booting the mirror, which is a small operating system in a container.

Advantage

  • Isolation - (Separated Test Environments) Virtual Machine era often encounters a common set of test environments that cannot be tested independently, resulting in multiple simultaneous development blocked in the test environment.

  • Continuous integration - (rapid expansion) requires only a simple import and export operation, which can quickly build a second set of environment. Is it more convenient than this?

Warehouse

Download Warehouse

Because docker mirror source is overseas, it needs help DaoCloud accelerator Accelerate

➜  docker pull centos
Using default tag: latest
latest: Pulling from library/centos
45a2e645736c: Pull complete
Digest: sha256:6e45de12a92ce6fcc0e3d0ffca8000833675f96679b3f1ecf71485c339826f27
Status: Downloaded newer image for centos:latest

image

View Mirrors

➜  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              104bec311bcd        2 weeks ago         129 MB

Edit Mirror

After the editor exits, the modified content will not be saved and the image will need to be saved by itself (similar to the submission of maven).

➜  docker run -t -i ubuntu /bin/bash
root@a61446f099f3:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@a61446f099f3:/# touch a
root@a61446f099f3:/# ls
a  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@a61446f099f3:/# exit

Save the mirror

Docker commit-m [submit version information]-a [author] [ID of container editing mirror: a61446f099f3] [warehouse name of target mirror: tag]

➜  docker commit -m 'first commit' -a 'ellis' a61446f099f3 ubuntu:test
sha256:9a0c4d4bb4e8766cf250bb5620fa4d8364fc59bb3ea5919cd7b72f751ddb4019

➜  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              test                9a0c4d4bb4e8        3 minutes ago       129 MB
ubuntu              latest              104bec311bcd        2 weeks ago         129 MB

Query Mirror

➜  docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relati...   3625      [OK]
mysql/mysql-server              Optimized MySQL Server Docker images. Crea...   237                  [OK]
centurylink/mysql               Image containing mysql. Optimized to be li...   47                   [OK]
sameersbn/mysql                                                                 41                   [OK]
zabbix/zabbix-server-mysql      Zabbix Server with MySQL database support       20                   [OK]
zabbix/zabbix-web-nginx-mysql   Zabbix frontend based on Nginx web-server ...   11                   [OK]
appcontainers/mysql             Centos/Debian Based Customizable MySQL Con...   8                    [OK]
marvambass/mysql                MySQL Server based on Ubuntu 14.04              7                    [OK]
dnhsoft/mysql-utf8              Inherits the official MySQL image configur...   4                    [OK]
bitnami/mysql                   Bitnami MySQL Docker Image                      3                    [OK]
alterway/mysql                  Docker Mysql                                    3                    [OK]
frodenas/mysql                  A Docker Image for MySQL                        3                    [OK]
drupaldocker/mysql              MySQL for Drupal                                2                    [OK]
yfix/mysql                      Yfix docker built mysql                         2                    [OK]
coscale/mysql                   CoScale custom configuration of the offici...   1                    [OK]
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL data...   1                    [OK]
lysender/mysql                  MySQL base image using Ubuntu 16.04 Xenial      1                    [OK]
projectomakase/mysql            Docker image for MySQL                          0                    [OK]
nanobox/mysql                   MySQL service for nanobox.io                    0                    [OK]
dockerizedrupal/mysql           mysql-for-docker                                0                    [OK]
cloudposse/mysql                Improved `mysql` service with support for ...   0                    [OK]
1maa/mysql                      MySQL database                                  0                    [OK]
captomd/mysql                   CaptoMD mysql configuration                     0                    [OK]
tozd/mysql                      MySQL (MariaDB fork) Docker image.              0                    [OK]
treenity/mysql                  Mysql5.7 with OSx permission fixs               0                    [OK]

delete mirror

  • When multiple tags exist in the same image, only the specified tags are deleted without affecting the image file.

  • When only one label exists in the image, deletion will perform an operation to delete the image file. AUFS Layer.

➜  docker rmi mysql:latest
Untagged: mysql:latest
Untagged: mysql@sha256:116171866ef2a194368a1760c9ef8c80a75ff6bd28fd25d836d29ce1fcc8173a
Deleted: sha256:594dc21de8de7cdae01ecbd4d8a4dedead73756984896a00fce13cbc8c24f38e
Deleted: sha256:597ae92858cafe843838c2f7b5768fb657d8e67aa46fa9313267c8fed1e5339f
Deleted: sha256:31913f2e2b61577119ac02c90a7c803355dc471880c6b0b9da09aef6efb60979
Deleted: sha256:f6d702dc6a290c6e5d7a53879f3b0251daa24d41924841f90543666d3e237ff5
Deleted: sha256:5105103084f4c6ec91437ffa86e7d2929a3ac53805eb585c378c7935c03c6b42
Deleted: sha256:50a36ecefa69b886268dd91785e88074b3f48447f9b9a3d90c7d56ad2b76a8c3
Deleted: sha256:12c2fd29169d6fb77177a6c892ac289df304a0694bc79c5f9939035c5f695f1e
Deleted: sha256:d8e052f68f5ab9766b5927174e52b8ff23e5c7427afcc30aef6cc53c24a4d4e3
Deleted: sha256:9042efb93af5246dbc36ffd103a8b87e8d39e797e06656fb5df053ca77378eb9
Deleted: sha256:c08bac17c414ebfb8d22b46b79d4c1fc41d16a4452c924c306cbec673d215d43
Deleted: sha256:30499d7d4e67556242cabe7be7b1bac2a94d7ff00972c6236475858a91b4b2a7
Deleted: sha256:b6ca02dfe5e62c58dacb1dec16eb42ed35761c15562485f9da9364bb7c90b9b3
  • When the container created by the mirror exists, it is impossible to delete the mirror file. We need to delete all the containers that depend on the mirror first, and then delete the mirror.

➜  docker run ubuntu:test echo 'hello world'
hello world

➜  docker rmi ubuntu:test
Error response from daemon: conflict: unable to remove repository reference "ubuntu:test" (must force) - container 2761fed757c1 is using its referenced image 9a0c4d4bb4e8

➜  docker docker rm 2761fed757c1
8d1488449db9

➜  docker rmi -f 9a0c4d4bb4e8
Untagged: ubuntu:test
Deleted: sha256:9a0c4d4bb4e8766cf250bb5620fa4d8364fc59bb3ea5919cd7b72f751ddb4019

create mirror

There are three ways to create a mirror:
1. Container creation based on existing mirrors
Refer to the Save Mirror example above.

2. Import based on local template
OPEN-VZ Template Download template

➜  cat ubuntu-14.04-x86_64-minimal.tar.gz | docker import - ubuntu:14.04

3. Create based on Dockerfile (store image to local file)

➜  docker save -o ubuntu_14.04.tar ubuntu:latest

➜  docker load --input ubuntu_14.04.tar 
or
➜  docker load < ubuntu_14.04.tar

Upload Mirror Image

First you need to log in, then set the tag of the corresponding account to upload. The following example uploads the warehouse of test to user xxxx.

➜  docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ${xxxx}
Password:
Login Succeeded

➜  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              104bec311bcd        2 weeks ago         129 MB
ubuntu              latest              104bec311bcd        2 weeks ago         129 MB
mysql               5.7                 594dc21de8de        2 weeks ago         400.2 MB

➜  docker tag test:latest xxxx/test:latest

➜  docker docker push xxxx/test:latest
The push refers to a repository [docker.io/xxxx/test]
5972ebe5b524: Preparing
3d515508d4eb: Pushing [==================================================>] 4.608 kB
bbe6cef52379: Preparing
87f743c24123: Pushing [==================================================>] 15.87 kB
32d75bc97c41: Preparing

container

Create/Start/Close

From startup to shutdown

➜  docker create -it test:latest
11ad937149a8750487e72cf0a696b85927388acbadcba3138d821fbe06ba9c18

➜  docker start 11ad937149a8
11ad937149a8

➜  docker stop 11ad937149a8
11ad937149a8

//Equivalent to

➜  docker run ubuntu /bin/echo "Hello world"
Hello world

Enter the container

- t lets Docker assign a pseudo-terminal and bind it to the container's standard input
- i Keep the standard input of the container open

➜  docker run -t -i test:latest /bin/bash
root@b7a06bcfe3e2:/#

Background operation

Execution instructions
➜  docker run -d test:latest /bin/sh -c "while true;do echo hello world; sleep 1; done"
7a964175acc1b91a9358c6a4ae7eff53e57d053ffabbd956de4ad8212298ed08

//View container processes
➜  docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS               NAMES
7a964175acc1        test:latest         "/bin/sh -c 'while tr"   9 seconds ago        Up 8 seconds                                        desperate_fermat

//View Container Log
➜  docker logs 7a964175acc1
hello world
hello world

//Termination container
➜  docker stop 7a964175acc1
7a964175acc1
or 
➜  docker kill 7a964175acc1
7a964175acc1

//Restart container
➜  docker restart ecb24cdd42f2
ecb24cdd42f2

Back to the container

When the - d parameter is used, the container starts up and runs in the background. The container that needs to enter these background runs will use attach.

➜  docker attach 7a964175acc1
hello world
hello world

But when attach exits, the background process ends. Since Docker 1.3, you can start a bash directly with exec.

➜  docker exec -ti 7a964175acc1 /bin/bash
root@7a964175acc1:/#

Delete containers

- 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 ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
ecb24cdd42f2        test:latest         "/bin/sh -c 'while tr"   19 minutes ago      Exited (0) 8 minutes ago                          small_wescoff

➜  docker rm ecb24cdd42f2
ecb24cdd42f2

➜  docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
7a964175acc1        test:latest         "/bin/sh -c 'while tr"   20 minutes ago      Up 5 minutes                                      desperate_fermat
➜  docker rm 7a964175acc1
Error response from daemon: You cannot remove a running container 7a964175acc1b91a9358c6a4ae7eff53e57d053ffabbd956de4ad8212298ed08. Stop the container before attempting removal or use -f
➜  docker rm -f 7a964175acc1
7a964175acc1

Export container

➜  docker docker export 67926c2742d8 > test_for_run.tar
➜  docker ls
test_for_run.tar

Import container

➜  cat test_for_run.tar| docker import - mdxdjh/test:v1.0
sha256:22673bf214f309ebdcb129380bb71d6af0a0fae1053fa9863b73484d61926798
➜  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
mdxdjh/test         v1.0                22673bf214f3        2 seconds ago        110.9 MB

Create data volumes

/ tmp is the local path and / var/tmp is the container
ro read-only, rw read-write

➜  ls /tmp
a

➜  docker run -tid --name myTmp -v /tmp:/var/tmp:ro test1:latest
31f0d584d5592c5bf147b4dba3a80f6dff3bac7da79ae94ec8b5efc90f1ee05b

➜  docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
31f0d584d559        test1:latest        "/bin/bash"         10 seconds ago      Up 8 seconds                            elegant_golick

➜  docker exec -ti 31f0d584d559 /bin/bash
root@31f0d584d559:/# ls /var/tmp/
a

Data Volume Sharing

  • First create a data volume myTmp, share it to another Tmp1 by binding, and then share it to another Tmp2 by another Tmp1 to achieve cascade sharing effect.

  • Deleting another Tmp1 and another Tmp2 will not result in the loss of myTmp volume content. Deleting myTmp (parent) will result in the cascade deletion of another Tmp1 and another Tmp2 data.


➜  docker run -ti --volumes-from  myTmp --name anotherTmp1 test1:latest
root@fe2e007b1f96:/# ls /var/tmp/
a

➜  docker run -ti --volumes-from anotherTmp1 --name anotherTmp2 test1:latest
root@9a35e7fc68cb:/# ls /var/tmp/
a

Delete data volumes

- v Deletes associated containers

➜  docker rm -v 9a35e7fc68cb

Keywords: Linux Docker MySQL Ubuntu Zabbix

Added by radhoo on Sun, 07 Jul 2019 04:55:51 +0300