您的当前位置:首页正文

docker基础安装使用

2022-04-14 来源:汇智旅游网


Docker基础安装使用

2020-02-22 作者:刘子毅

创建yum源

[root@bogon ~]# [root@bogon ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo Loaded plugins: fastestmirror, langpacks

adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo

Could not fetch/save url https://download.docker.com/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#7 - \"Failed connect to download.docker.com:443; Operation now in progress\" [root@bogon ~]# [root@bogon ~]#

链接无法解析改为使用阿里的yum源,操作如下: [root@bogon ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo Loaded plugins: fastestmirror, langpacks

adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo

repo saved to /etc/yum.repos.d/docker-ce.repo

安装最新的docker

[root@bogon ~]#

[root@bogon ~]# yum install docker-ce Loaded plugins: fastestmirror, langpacks

Existing lock /var/run/yum.pid: another copy is running as pid 9034. Another app is currently holding the yum lock; waiting for it to exit... The other application is: PackageKit Memory : 119 M RSS (1.0 GB VSZ)

Started: Fri Feb 21 19:28:47 2020 - 07:27 ago State : Sleeping, pid: 9034

Another app is currently holding the yum lock; waiting for it to exit... The other application is: PackageKit Memory : 119 M RSS (1.0 GB VSZ)

Started: Fri Feb 21 19:28:47 2020 - 07:29 ago State : Sleeping, pid: 9034

Another app is currently holding the yum lock; waiting for it to exit... The other application is: PackageKit Memory : 119 M RSS (1.0 GB VSZ)

Started: Fri Feb 21 19:28:47 2020 - 08:12 ago

State : Sleeping, pid: 9034 ^C

Exiting on user cancel.

此处发下yum被进程号为9034占用,直接kill [root@bogon ~]# ps aux | grep yum

root 9034 2.5 12.2 1041184 121668 ? SN 19:28 0:13 /usr/bin/python /usr/share/PackageKit/helpers/yum/yumBackend.py get-updates none

root 10322 0.0 0.0 112708 976 pts/0 S+ 19:37 0:00 grep --color=auto yum

[root@bogon ~]# kill -9 9034

[root@bogon ~]# ps aux | grep yum

root 10338 0.0 0.0 112708 976 pts/0 R+ 19:37 0:00 grep --color=auto yum

[root@bogon ~]# [root@bogon ~]# [root@bogon ~]# [root@bogon ~]#

重新安装docker,如下:

[root@bogon ~]# yum install docker-ce

BDB2053 Freeing read locks for locker 0x9ba: 9034/140061293627200 BDB2053 Freeing read locks for locker 0x9bc: 9034/140061293627200 Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com

* extras: mirrors.huaweicloud.com * updates: mirrors.aliyun.com

docker-ce-stable | 3.5 kB 00:00:00 (1/2): docker-ce-stable/x86_64/primary_db | 38 kB 00:00:00 (2/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00 Resolving Dependencies

--> Running transaction check

---> Package docker-ce.x86_64 3:19.03.6-3.el7 will be installed --> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-19.03.6-3.el7.x86_64 --> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-19.03.6-3.el7.x86_64

--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-19.03.6-3.el7.x86_64 --> Running transaction check

---> Package container-selinux.noarch 2:2.107-3.el7 will be installed ---> Package containerd.io.x86_64 0:1.2.10-3.2.el7 will be installed

---> Package docker-ce-cli.x86_64 1:19.03.6-3.el7 will be installed --> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================

Package Arch Version Repository Size

============================================================================================================================================ Installing:

docker-ce x86_64 3:19.03.6-3.el7 docker-ce-stable 24 M Installing for dependencies:

container-selinux noarch 2:2.107-3.el7 extras 39 k

containerd.io x86_64 1.2.10-3.2.el7 docker-ce-stable 23 M

docker-ce-cli x86_64 1:19.03.6-3.el7 docker-ce-stable 40 M

Transaction Summary

============================================================================================================================================ Install 1 Package (+3 Dependent packages)

Total download size: 87 M Installed size: 363 M Is this ok [y/d/N]: y Downloading packages: warning:

/var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-19.03.6-3.el7.x86_64.rpm:

Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY ] 1.3 MB/s | 39 MB 00:00:36 ETA

Public key for docker-ce-19.03.6-3.el7.x86_64.rpm is not installed (1/4): docker-ce-19.03.6-3.el7.x86_64.rpm | 24 MB 00:00:36

warning: /var/cache/yum/x86_64/7/extras/packages/container-selinux-2.107-3.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY ] 1.3 MB/s | 45 MB 00:00:32 ETA

Public key for container-selinux-2.107-3.el7.noarch.rpm is not installed (2/4): container-selinux-2.107-3.el7.noarch.rpm | 39 kB 00:00:41

(3/4): containerd.io-1.2.10-3.2.el7.x86_64.rpm | 23 MB 00:01:01 (4/4): docker-ce-cli-19.03.6-3.el7.x86_64.rpm | 40 MB 00:00:42

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Total 1.1 MB/s | 87 MB 00:01:19

Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg Importing GPG key 0x621E9F35:

Userid : \"Docker Release (CE rpm) \" Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35

From : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg Is this ok [y/N]: y

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5:

Userid : \"CentOS-7 Key (CentOS 7 Official Signing Key) \" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5

Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Is this ok [y/N]: y

Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 2:container-selinux-2.107-3.el7.noarch 1/4

Installing : containerd.io-1.2.10-3.2.el7.x86_64 2/4

Installing : 1:docker-ce-cli-19.03.6-3.el7.x86_64 3/4

Installing : 3:docker-ce-19.03.6-3.el7.x86_64 4/4

Verifying : 3:docker-ce-19.03.6-3.el7.x86_64 1/4

Verifying : 1:docker-ce-cli-19.03.6-3.el7.x86_64 2/4

Verifying : containerd.io-1.2.10-3.2.el7.x86_64 3/4

Verifying : 2:container-selinux-2.107-3.el7.noarch 4/4

Installed:

docker-ce.x86_64 3:19.03.6-3.el7

Dependency Installed:

container-selinux.noarch 2:2.107-3.el7 containerd.io.x86_64

0:1.2.10-3.2.el7 docker-ce-cli.x86_64 1:19.03.6-3.el7

Complete!

安装成功,启动docker进程:

[root@bogon ~]#

[root@bogon ~]# systemctl start docker [root@bogon ~]# systemctl enable docker

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service /usr/lib/systemd/system/docker.service. [root@bogon ~]#

to

查看docker状态

[root@bogon ~]# systemctl status docker

● docker.service - Docker Application Container Engine

Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2020-02-21 19:42:29 PST; 22s ago Docs: https://docs.docker.com Main PID: 10553 (dockerd)

CGroup: /system.slice/docker.service

└─10553 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Feb 21 19:42:27 docker dockerd[10553]: time=\"2020-02-21T19:42:27.535045863-08:00\" level=info msg=\"scheme \\\"unix\\\" not registered, fallback to default scheme\" module=grpc

Feb 21 19:42:27 docker dockerd[10553]: time=\"2020-02-21T19:42:27.535081253-08:00\" level=info msg=\"ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/container... module=grpc

Feb 21 19:42:27 docker dockerd[10553]: time=\"2020-02-21T19:42:27.535107105-08:00\" level=info msg=\"ClientConn switching balancer to \\\"pick_first\\\"\" module=grpc

Feb 21 19:42:27 docker dockerd[10553]: time=\"2020-02-21T19:42:27.635007791-08:00\" level=info msg=\"Loading containers: start.\"

Feb 21 19:42:28 docker dockerd[10553]: time=\"2020-02-21T19:42:28.354209593-08:00\" level=info msg=\"Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemo... IP address\"

Feb 21 19:42:29 docker dockerd[10553]: time=\"2020-02-21T19:42:29.097833464-08:00\" level=info msg=\"Loading containers: done.\"

Feb 21 19:42:29 docker dockerd[10553]: time=\"2020-02-21T19:42:29.182055014-08:00\" level=info msg=\"Docker daemon\" commit=369ce74a3c graphdriver(s)=overlay2 version=19.03.6 Feb 21 19:42:29 docker dockerd[10553]: time=\"2020-02-21T19:42:29.182193961-08:00\" level=info msg=\"Daemon has completed initialization\"

Feb 21 19:42:29 docker systemd[1]: Started Docker Application Container Engine.

Feb 21 19:42:29 docker dockerd[10553]: time=\"2020-02-21T19:42:29.258868440-08:00\" level=info msg=\"API listen on /var/run/docker.sock\" Hint: Some lines were ellipsized, use -l to show in full. [root@bogon ~]#

创建名为konggu的容器,并使用httpd的镜像源:

[root@bogon ~]# docker create --name konggu httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd bc51dd8edc1b: Pull complete dca5bc65e18f: Pull complete ccac3445152a: Pull complete 8515f2015fbc: Pull complete e35494488b8c: Pull complete

Digest: sha256:b783a610e75380aa152dd855a18368ea2f3becb5129d0541e2ec8b662cbd8afb Status: Downloaded newer image for httpd:latest

7a694fe59d001ea5b132de17e8bf3e0a1400853acab2a16b1da2507a3ceb3aa4

查看容器状态

[root@bogon ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7a694fe59d00 httpd \"httpd-foreground\" 48 seconds ago Created konggu

启动容器:

[root@bogon ~]# docker start konggu konggu

再次查看状态

[root@bogon ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7a694fe59d00 httpd \"httpd-foreground\" About a minute ago Up 2 seconds 80/tcp konggu [root@bogon ~]# docker container ls

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7a694fe59d00 httpd \"httpd-foreground\" 2 minutes ago Up 49 seconds 80/tcp konggu

进入容器:

[root@bogon ~]# docker exec -it konggu bash root@7a694fe59d00:/usr/local/apache2#

root@7a694fe59d00:/usr/local/apache2# root@7a694fe59d00:/usr/local/apache2# root@7a694fe59d00:/usr/local/apache2# ls

bin build cgi-bin conf error htdocs icons include logs modules [root@bogon ~]# docker container ls

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7a694fe59d00 httpd \"httpd-foreground\" 7 minutes ago Up 5 minutes 80/tcp konggu

删除容器

[root@bogon ~]# docker stop konggu konggu

[root@bogon ~]# docker rm konggu konggu

[root@bogon ~]# docker container ls

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

重新创建带端口号的容器

[root@bogon ~]# docker run --name konggu -d -p 8080:80 httpd

19f0c0b40353114a639fd02f3d808f8b142c714ccec24f9cf14f5cb66af02409 [root@bogon ~]# docker container ls

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

19f0c0b40353 httpd \"httpd-foreground\" 12 seconds ago Up 10 seconds 0.0.0.0:8080->80/tcp konggu

[root@bogon ~]# docker exec -it konggu bash root@19f0c0b40353:/usr/local/apache2#

root@19f0c0b40353:/usr/local/apache2# cd htdocs/ root@19f0c0b40353:/usr/local/apache2/htdocs# ls index.html

root@19f0c0b40353:/usr/local/apache2/htdocs# echo \"konggu\" > index.html root@19f0c0b40353:/usr/local/apache2/htdocs#

成功修改了web内容,实验成功。

因篇幅问题不能全部显示,请点此查看更多更全内容