在本文中,我们将学习如何安装Docker。Dockers是一个非常好的工具,可以在自己的容器上运行应用程序。它像虚拟机一样工作,具有更高的可移植性和资源友好性,并且更加依赖于主机OS。有两种在CentOS 7上安装Docker的方法。第一种方法是使用现有OS进行安装,或者使用称为Docker Machine的工具旋转服务器并进行自动安装。
在此出于演示目的,我们将安装在现有OS上。
在开始安装之前,我们需要64位的CentOS 7,并且需要具有sudo权限的非root用户。Docker的安装可在官方的CentOS 7存储库中获得,它不是最新版本。为了获取最新版本,我们将从Dockers官方存储库安装Dockers。
$ sudo yum check-update
现在运行以下命令以添加官方Docker存储库并下载最新版本的Dockers并进行安装。
$ curl -fsSL https://get.docker.com/ | sh
请记住,您必须先注销然后重新登录才能生效!
安装完成后,我们需要使用以下命令启动Docker守护程序
$ sudo systemctl start docker
要知道docker服务是否在运行,我们可以运行以下命令
$ sudo systemctl status docker
我们需要使docker在引导时启动,我们需要运行以下命令
$ sudo systemctl enable docker
我们需要将当前用户添加到安装启用的系统上的Docker组中,否则我们将无法以非root用户身份运行docker命令行实用程序。
$ sudo usermod –aG docker $(whoami)
要么
$ sudo usermond –aG docker username
在我们刚刚安装并运行Docker的过程中,我们需要学习一些带有一些子命令和参数的命令行实用程序
$ docker [option] [command] [arguments]
要查看所有子命令,我们需要键入以下命令
$ docker CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@centos-linux-1 ~]# docker Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help Print usage -l, --log-level=info Set the logging level --tls Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Sub Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container network Manage Docker networks pause Pause all processes within a container port List port mappings or a specific mapping for the CONTAINER ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop a running container tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within a container update Update configuration of one or more containers version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code
Docker映像用于运行Docker容器,该容器从Dockers Hub提取映像。
要检查我们是否有权访问Dockers Hub,请运行以下命令
$ docker run hello-world
我们可以使用docker命令和子命令搜索来搜索Docker Hub上可用的映像,例如,我们将搜索CentOS映像,以下是在Docker Hub上搜索CentOS映像的命令。
[root@centos-linux-1 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 2417 [OK] ansible/centos7-ansible Ansible on Centos7 80 [OK] jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 26 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK] million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 10 [OK] torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] jdeathe/centos-ssh-mysql CentOS-6 6.7 x86_64 / MySQL. 7 [OK] nickistre/centos-lamp LAMP on centos setup 4 [OK] centos/mariadb55-centos7 3 [OK] nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK] consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] darksheer/centos Base Centos Image -- Updated hourly 1 [OK] timhughes/centos Centos with systemd installed and running 1 [OK] grayzone/centos auto build for centos. 0 [OK] ustclug/centos USTC centos 0 [OK] januswel/centos yum update-ed CentOS image 0 [OK] kz8s/centos Official CentOS plus epel-release 0 [OK] grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK] jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK] ericuni/centos centos dev 0 [OK] dmglab/centos CentOS with some extras - This is for the ... 0 [OK] repositoryjp/centos Docker Image for CentOS. 0 [OK]
我们可以从Dockers Hub下载映像,以下是使用pull命令下载Docker映像的命令
$ docker pull centos docker pull centos Using default tag: latest latest: Pulling from library/centos 0653bff3c5cf: Extracting 29.52 MB/70.6 MB Message from syslogd@centos-linux-1 at Jul 11 08:06:44 ... 0653bff3c5cf: Pull complete Digest: sha256:81e4f2f663eaa1bf46ff9be348396dd7053734b257ef4147d7133d6f25bbf7cf Status: Downloaded newer image for centos:latest
我们可以使用run子命令来运行我们下载的图像
$ docker run centos
并使用以下命令查看下载到我们计算机的图像
$ docker images docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
当我们将容器映像下载到本地系统后,我们将使用带有–i和–t开关的CentOS最新映像来运行容器,从而为容器提供交互式外壳
$ docker run –it centos [root@0aac55bf786f /]#
提示将更改为容器ID“ 0aac55bf786f”,这是我们下载的容器的ID
我们可以使用以下命令在容器上安装一些软件,我们将在容器上安装mysql服务器
[root@0aac55bf786f /]# yum install mysql-server
在容器上安装mysql之后,我们可以使用–m开关使用以下命令将更改提交到Dockers集线器,该命令用于提交消息,而–a用于添加作者姓名
General Syntax $ docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
Example $ docker commit -m "added MySQL-server" -a "Chandra Prakash" 0aac55bf786f finid/centos-mysql_db sha256:77993fb3b476e02324b8bf7ebfebe1e792b93df2eecc70208997ac95455c65e8
要确认图像已提交并在本地存储库中列出,可以运行以下命令。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE finid/centos-mysql_db latest 77993fb3b476 28 seconds ago 268.4 MB centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
在设置和配置之后,我们了解了Docker及其容器的基础,以及谁可以拉取图像并使用它们并安装一些软件,借助Docker,我们可以做很多事情,在以后的文章中,我们将学到更多。