Docker Study Part 1

Docker Tutorial

What is Docker

Docker is an open source platform, which provides the ability to package an application together with its environment (e.g. Java, database drivers, Tomcat and configuration files) and run it in an isolated container.


Docker Logo

Docker v.s. Virtual Machine

  1. Virtual machine uses hypervisor to simulate hardware while Docker directly uses the actual host machine hardware and resources.
  2. Virtual machine needs to load the guest operating system while Docker directly uses the host machine OS kernel.
    Docker v.s. Virtual Machine
Virtual MachineDocker
Disk UsageLarge (GB)Small (MB or KB)
Bootstrap SpeedSlow (Minutes)Fast (Seconds)
Where to runIn HypervisorIn Host OS kernel
ConcurrencyLess than 100 in a hostHundreds or even thousands
Resource UtilizationLowHigh

Installation (CentOS 8)

1. Check if Docker is already installed

Here is the Official Installation Guidance.

# Query if you have already installed Docker.
> rpm -qa | grep Docker
# To remove the old version
>  sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Install the tools

yum install -y yum-utils

3. Set up Docker repository

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

> sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

You may get errors like this in China.

> [Errno 14] curl#35 - TCP connection reset by peer
  [Errno 12] curl#35 - Timeout

And you need to set up a mirror for the repository:

> sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4. Update the yum

> yum makecache fast

5. Install Docker

> yum install docker-ce docker-ce-cli containerd.io

6. Start Docker

> systemctl start docker

7. Test

> docker version

> docker run hello-world

> docker images

8. (Optional) Uninstall

> systemctl stop docker

> yum -y remove docker-ce docker-ce-cli containerd.io

> rm -rf /var/lib/docker

9. (Optional) Set official Docker images mirror

To speed up the downloading of official Docker images in China, we can configure the mirror following the instructions here.

> sudo mkdir -p /etc/docker
> sudo tee /etc/docker/daemon.json <<-'EOF'
 {
  	"registry-mirrors": ["https://qiyb9988.mirror.aliyuncs.com"]
 }
 EOF
> sudo systemctl daemon-reload
> sudo systemctl restart docker

Docker Basic Commands

在这里插入图片描述

Images

1. docker Images

[root@kyleyu-shanghai ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    300e315adb2f   3 months ago   209MB
# TAG: Version, will use latest by default if you don't specify it.

2. docker search

[root@kyleyu-shanghai ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10631     [OK]
mariadb                           MariaDB Server is a high performing open sou…   3989      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   779                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   528       [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87
......

3. docker pull

[root@kyleyu-shanghai ~]# docker pull redis
Using default tag: latest # By default uses latest if you don't specify TAG.
latest: Pulling from library/redis
a076a628af6f: Already exists # Download layer by layer. This layer is already downloaded.
f40dd07fe7be: Pull complete
ce21c8a3dbee: Pull complete
ee99c35818f8: Pull complete
56b9a72e68ff: Pull complete
3f703e7f380f: Pull complete
Digest: sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494 # It is a signature
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@kyleyu-shanghai ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists
......
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

4. docker rmi

[root@kyleyu-shanghai ~]# docker rmi mysql:5.7

# $(command) can pipe the results of command as the arguments of another command.
[root@kyleyu-shanghai ~]# docker rmi $(docker images -aq)

Container

1. docker run

[root@kyleyu-shanghai ~]# docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
-d, --detach                         Run container in background and print container ID
-e, --env list                       Set environment variables
-i, --interactive                    Run container in interactive mode, normally use with -t.
-t, --tty                            Allocate a pseudo-TTY
-P, --publish-all                    Publish all exposed ports to random ports
-p, --publish list                   Publish a container's port(s) to the host [hostPort:containerPort]
# The /bin/bash is the argument to the container.
[root@kyleyu-shanghai ~]# docker run -it centos /bin/bash

# The host has changed to the container.
[root@c2ae55a0d4bc /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# Exit from container and STOP the container.
# If we do not want to stop container, type in ctrl + P + Q
[root@c2ae55a0d4bc /]# exit
exit
[root@kyleyu-shanghai ~]#
# The background docker container will automatically close itself if there are no active commands to run.

[root@kyleyu-shanghai ~]# docker run -d --name="centos01" centos
a14c7b6801145049bc10e706fa51ecd04fc74bb00d52ab2e2d7da670d42c507b
[root@kyleyu-shanghai ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@kyleyu-shanghai ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
a14c7b680114   centos    "/bin/bash"   58 seconds ago   Exited (0) 57 seconds ago             centos01

2. docker ps

# Show all containers that are running by default.
[root@kyleyu-shanghai ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# -a option will show all the container including stopped containers.
[root@kyleyu-shanghai ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                            PORTS     NAMES
c2ae55a0d4bc   centos    "/bin/bash"   3 minutes ago   Exited (127) About a minute ago             cool_spence
......

3. docker start / restart / stop / kill

[root@kyleyu-shanghai ~]# docker restart c2ae55a0d4bc

4. docker rm

[root@kyleyu-shanghai ~]# docker rm 41ab679baef2
41ab679baef2
[root@kyleyu-shanghai ~]# docker rm -f $(docker ps -aq)
c2ae55a0d4bc
e8f11cbd9ded
32bb136fbdf6
058d784777ca
0f9aa5c85a41

5. docker logs

[root@kyleyu-shanghai ~]# docker logs -ft a19b633d5fa5
[root@a19b633d5fa5 /]# ls
2021-03-20T17:50:21.123524295Z bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
2021-03-20T17:50:26.466033188Z [root@a19b633d5fa5 /]# mkdir test
2021-03-20T17:50:32.156609411Z [root@a19b633d5fa5 /]# cd test/
2021-03-20T17:50:34.353478893Z [root@a19b633d5fa5 test]# ls
2021-03-20T17:50:37.065848809Z [root@a19b633d5fa5 test]# exit
2021-03-20T17:50:37.065868199Z exit

6. docker top

# Check the process stats in the running container.
[root@kyleyu-shanghai ~]# docker top --help

Usage:  docker top CONTAINER [ps OPTIONS]

Display the running processes of a container

[root@kyleyu-shanghai ~]# docker top da2ee7044553

7. docker inspect

# Inspect the metadata of container or image.
[root@kyleyu-shanghai ~]# docker inspect centos
[
    {
        "Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1"
        ],
        ......
        ......

8. docker exec / attach

[root@kyleyu-shanghai ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
da2ee7044553   centos    "/bin/bash"   11 minutes ago   Up 11 minutes             centos02

# Enter the running container da2ee7044553 and opening a new tty in a new process.
[root@kyleyu-shanghai ~]# docker exec -it da2ee7044553 /bin/bash
[root@da2ee7044553 /]# exit
exit

# Directly enter the running container but do not open a new tty nor new process.
[root@kyleyu-shanghai ~]# docker attach da2ee7044553
[root@da2ee7044553 /]# exit
exit

9. docker cp

# Copy test file from container to host machine.

[root@kyleyu-shanghai ~]# docker cp 2b7d4beb435b:/test .
[root@kyleyu-shanghai ~]# ls
download  dump.rdb  test
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aliengod

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值