Docker Series 2: Docker Installation Configuration and Basic Use

Keywords: Linux Docker Nginx yum CentOS

1. Introduction to Basic Environment

1. Introduction of basic software and hardware environment

  • 64-bit CPU required

  • linux Kernel Version 3.10 and above

  • Kernel supports cggroups and namespace

  • System version is CentOS7.4 [centos6 is also available but unstable]

  • Docker version 18.06

  • K8S version 1.16 [k8s version must be compatible with docker version]

2. View the relationship between k8s and docker version

Step 1: Sign in to github

https://github.com/kubernetes/kubernetes/releases

Step 2: View the CHANGELOG file for the specified version of k8s

Step 3: Search docker version to see supported docker versions

3. Configure yum source

1) Use a mirror of the Ali Cloud at https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/, as follows

2) Configure yum source

[root@host1 ~]# cat /etc/yum.repos.d/docker.repo 
[docker]
name=aliyun docker repo
enabled=1
gpgcheck=0
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/

Check to see if the configuration was successful

[root@host1 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
repo id                                       repo name                                        status
docker                                        aliyun docker repo                                   58
base/7/x86_64                                 CentOS-7 - Base                                  10,097
extras/7/x86_64                               CentOS-7 - Extras                                   307
updates/7/x86_64                              CentOS-7 - Updates                                  997


2. Install and configure docker

1. Install docker 18.06.0

[root@host1 ~]# yum install docker-ce-18.06.0.ce

2. Create a json file to speed up docker image download

[root@host1 ~]# mkdir /etc/docker
[root@host1 ~]# cat /etc/docker/daemon.json
{
    "registry-mirrors": ["https://registry.docker-cn.com"]
}

3. Start Services

[root@host1 ~]# systemctl start docker
[root@host1 ~]# systemctl status docker

Take a look at the docker version

[root@host1 ~]# docker --version
Docker version 18.06.0-ce, build 0ffa825

View docker information

[root@host1 ~]# docker info


3. Operations on Mirroring

1. About firewalls

The docker needs to start the iptables service, but in centos7, firewalld is used by default to manage it, not iptables, so it may cause problems with docer usage because iptables are required for port mapping.This can be solved in the following ways

[root@host1 ~]# systemctl disable firewalld          ^C
[root@host1 ~]# yum install iptables-services -y       ^C
[root@host1 ~]# systemctl enable iptables             ^C

2. View local mirrors

[root@host1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

3. Search for mirrors, such as nginx mirrors

[root@host1 ~]# docker search nginx

From the results of the search above, you can see that some of the results of the search are just one thing, and some of the results are slashed

  • There is no slash separation, we call it the top warehouse, usually official

  • Separated so-called: user warehouse or like a warehouse, generally means that the user registers his own account, then makes a warehouse, and puts some mirror for others to use.

4. Pull out a few mirrors

1) Pull the nginx image, to save bandwidth, select the alpine version here

[root@host1 ~]# docker image pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
bdf0201b3a05: Pull complete 
3d0a573c81ed: Pull complete 
8129faeb2eb6: Pull complete 
3dc99f571daf: Pull complete 
Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Status: Downloaded newer image for nginx:1.14-alpine

2) Pull busybox mirror

[root@host1 ~]# docker image pull busybox
Using default tag: latest
latest: Pulling from library/busybox
322973677ef5: Pull complete 
Digest: sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084
Status: Downloaded newer image for busybox:latest

3) View local downloaded images

[root@host1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              b534869c81f0        2 weeks ago         1.22MB
nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB


  • REPOSITORY: Warehouse name

  • TAG: Label

  • IMAGE ID: Unique ID value

  • CREATED: When was it created

  • SIZE: Size

4) View the details of the mirror

[root@host1 ~]# docker image ls --no-trunc
REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE
busybox             latest              sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b   2 weeks ago         1.22MB
nginx               1.14-alpine         sha256:8a2fb25a19f5dc1528b7a3fabe8b3145ff57fe10e4f1edac6c718a3cf4aa4b73   8 months ago        16MB

4. Operations on containers

1. Container operations include the following

  • start container:

  • Stop container:stop

  • Force Stop: kill

  • Start container directly after creation:run

  • Pause container: pause

  • Unpause (continue running) container: unpause

  • You can also list all containers: docker ps or docker container ls

2. Start container operation

run command to start the container, including the following parameters

  • -t: Here is a terminal specified. Without a terminal, you cannot log on to this container.

  • -i, --interactive: This option is required if you want interactive access

  • --name string: Here is the name of the specified container

  • --rm: Automatically delete container objects when the container stops

  • -d, --detach: Let the current container work in the background

  • --network string: Specifies which network the container is joined to, if not, a bridge network by default

3. Start container based on busybox

[root@host1 ~]# docker run --name bbox -it busybox
/ #
  • The container is started and entered into the container's shell

At this point, you can see that the busybox container has started in other terminals

[root@host1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
dc696a322096        busybox             "sh"                About a minute ago   Up About a minute                       bbox

Looking at the information in the busybox container, you can see that the process with id 1 is sh instead of init at this time

/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 sh
    6 root      0:00 ps

Simulate apache in a busybox container

/ # mkdir /data/html -p
/ # echo "test page">>/data/html/index.html
/ # 
/ # httpd -f -h /data/html/

You can also check the container details in bash

[root@host1 ~]# docker inspect bbox2 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
  • You can see that the address of this container is 172.16.0.3

In fact, after installing docker, a virtual network card docker0 is generated by default. Physical machines communicate with docker0 and virtual machines

[root@host1 ~]# ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:d5:cc:54:6d brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:d5ff:fecc:546d/64 scope link 
       valid_lft forever preferred_lft forever
6: veth96dc278@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
10: veth693182e@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
  • The address of docker 0 is 172.17.0.1

curl accesses apache in the container on the physical machine

[root@host1 ~]# curl 172.17.0.3
test page

3. Stop and Restart Containers

Execute exit to exit container

/ # exit
[root@host1 ~]#

After exiting the container, it just stops, but it still exists

  • docker ps: view running containers

  • Docker ps-a: View all containers, including those that have been stopped

[root@host1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@host1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
dc696a322096        busybox             "sh"                2 hours ago         Up 2 hours                                       bbox

Restart the container that started this stop, using the start command, with two possible options

  • -i: interactive mode

  • -a: attach to terminal

[root@host1 ~]# docker start -i -a bbox

4. Delete stopped containers

[root@host1 ~]# docker rm bbox
bbox
[root@host1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES

Add the rm option when creating containers to automatically delete them when they stop

[root@host1 ~]# docker run --rm -it --name bbox2 busybox
/ # 
/ # exit

[root@host1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

When deleting a container, you can either specify a name to delete it or specify the ID of the container to delete it.

  • When you specify an ID, you don't need to write all the ID contents, just write the first part.

[root@host1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
9c2d1bbe32e8        busybox             "sh"                3 seconds ago       Exited (0) 1 second ago                       bbox

[root@host1 ~]# docker rm 9c2d
9c2d

[root@host1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

The following command deletes all stopped containers at once

[root@host1 ~]# docker rm $(docker ps -a -q)

5. Delete Mirrors, Export Mirrors, Import Mirrors

  • rmi: Delete local mirrors

  • load: Import the mirror file

  • save: export the mirror file

See what mirrors are present

[root@host1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              b534869c81f0        2 weeks ago         1.22MB
nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

Export a busybox mirror under tmp

[root@host1 ~]# docker save busybox:latest>/tmp/busybox-latest.tar.gz
[root@host1 ~]# ls /tmp/
busybox-latest.tar.gz

Remove local busybox image

[root@host1 ~]# docker rmi b53
Untagged: busybox:latest
Untagged: busybox@sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084
Deleted: sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b
Deleted: sha256:eac247cb7af5edc34d3620e8bce653d4af7d4e3a0427d487a97530c7fac0b841

[root@host1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

Import mirror file from tmp

[root@host1 ~]# docker load</tmp/busybox-latest.tar.gz 
eac247cb7af5: Loading layer [==================================================>]  1.437MB/1.437MB
Loaded image: busybox:latest

[root@host1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              b534869c81f0        2 weeks ago         1.22MB
nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB












Posted by rupturedtoad on Thu, 19 Dec 2019 18:20:55 -0800