发布日:2022-03-18 14:34 阅读数:
sudo apt update
接下来,安装一些允许apt
使用包通过HTTPS的必备软件包:
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common
然后将官方Docker存储库的GPG密钥添加到您的系统:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
将Docker存储库添加到APT源:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
接下来,使用新添加的repo中的Docker包更新包数据库:
sudo apt update
确保您要从Docker repo而不是默认的Debian repo安装:
apt-cache policy docker-ce
虽然Docker的版本号可能不同,但您会看到这样的输出:
docker-ce: Installed: (none) Candidate: 18.06.1~ce~3-0~debian Version table: 18.06.1~ce~3-0~debian 500 500 https://download.docker.com/linux/debian stretch/stable amd64 Packages
sudo apt install docker-ce
现在应该安装Docker,守护进程启动,并启用进程启动进程。检查它是否正在运行:
sudo systemctl status docker
输出应类似于以下内容,表明该服务处于活动状态并正在运行:
● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-07-05 15:08:39 UTC; 2min 55s ago Docs: https://docs.docker.com Main PID: 21319 (dockerd) CGroup: /system.slice/docker.service ├─21319 /usr/bin/dockerd -H fd:// └─21326 docker-containerd --config /var/run/docker/containerd/containerd.toml
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'.
如果要在运行docker
命令时避免键入sudo
,请将用户名添加到docker
组中:
sudo usermod -aG docker ${USER}
要应用新的组成员身份,请注销服务器并重新登录,或键入以下内容:
su - ${USER}
系统将提示您输入用户密码以继续。
通过键入以下内容确认您的用户现已添加到docker组:
id -nG
sammy sudo docker
如果您需要将用户添加到您未登录的docker
组中,请使用以下方式明确声明该用户名:
sudo usermod -aG docker username
docker [option] [command] [arguments]
要查看所有可用的子命令,请键入:
docker
从Docker 18开始,可用子命令的完整列表包括:
attach Attach local standard input, output, and error streams 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 to files or directories 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 Docker objects kill Kill one or more running containers 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 pause Pause all processes within one or more containers 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 one or more containers 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 (streamed to STDOUT by default) 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 one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes
要查看特定命令可用的选项,请键入:
docker docker-subcommand --help
要查看有关Docker的系统范围信息,请使用:
docker info
docker run hello-world
输出将指示Docker正常工作:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 9db2ca6ccae0: Pull complete Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ...
docker search ubuntu
该脚本将对Docker Hub进行爬网,并返回名称与搜索字符串匹配的所有图像的列表。在这种情况下,输出将类似于:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 8320 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 214 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 170 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 128 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 88 [OK] neurodebian NeuroDebian provides neuroscience research s… 53 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 43 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components=m… 39 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK] tutum/ubuntu Simple Ubuntu docker images with SSH access 18 i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 13 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 12 [OK] ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12 eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 [OK] darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 [OK] codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 [OK] 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3 [OK] pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 2 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK] ossobv/ubuntu Custom ubuntu image from scratch (based on o… 0 smartentry/ubuntu ubuntu with smartentry 0 [OK] 1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK] pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0 paasmule/bosh-tools-ubuntu Ubuntu based bosh-cli 0 [OK] ...
docker pull ubuntu
您将看到以下输出:
Using default tag: latest latest: Pulling from library/ubuntu 6b98dfc16071: Pull complete 4001a1209541: Pull complete 6319fc68c576: Pull complete b24603670dc3: Pull complete 97f170c87c6f: Pull complete Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d Status: Downloaded newer image for ubuntu:latest
docker images
输出应类似于以下内容:
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 16508e5c265d 13 days ago 84.1MB hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB
docker run -it ubuntu
您的命令提示符应该更改以反映您现在正在容器内工作的事实,并应采用以下形式:
root@d9b100f2f636:/#
apt update
然后在其中安装任何应用程序。我们安装Node.js:
apt install nodejs
这将从官方Ubuntu存储库中安装容器中的Node.js. 安装完成后,验证是否已安装Node.js:
node -v
您将看到终端中显示的版本号:
v8.10.0
docker ps
您将看到类似于以下内容的输出:
CONTAINER ID IMAGE COMMAND CREATED
在本教程中,您启动了两个容器; 一个来自hello-world
图像,另一个来自ubuntu
图像。两个容器都不再运行,但它们仍然存在于您的系统上。
要查看所有容器 - 活动和非活动,请 使用-a
开关运行docker ps
:
docker ps -a
您将看到类似于此的输出:
d9b100f2f636 ubuntu "/bin/bash" About an hour ago Exited (0) 8 minutes ago sharp_volhard 01c950718166 hello-world "/hello" About an hour ago Exited (0) About an hour ago festive_williams
要查看您创建的最新容器,请将其传递给-l
交换机:
docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b100f2f636 ubuntu "/bin/bash" About an hour ago Exited (0) 10 minutes ago sharp_volhard
要启动已停止的容器,请使用docker start
,后跟容器ID或容器名称。让我们启动基于Ubuntu的容器,其ID为 d9b100f2f636
:
docker start d9b100f2f636
容器将启动,您可以使用docker ps
来查看其状态:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b100f2f636 ubuntu "/bin/bash" About an hour ago Up 8 seconds sharp_volhard
要停止正在运行的容器,请使用docker stop
,后跟容器ID或名称。这次,我们将使用Docker分配容器的名称,即sharp_volhard
:
docker stop sharp_volhard
一旦您决定不再需要容器,请使用该docker rm
命令将其删除,再次使用容器ID或名称。使用该docker ps -a
命令查找与hello-world
映像关联的容器的容器ID或名称,然后将其删除。
docker rm festive_williams
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
docker commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs
docker images
你会看到这样的输出:
REPOSITORY TAG IMAGE ID CREATED SIZE sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB hello-world latest e38bc07ac18e 2 months ago 1.85kB
docker login -u docker-registry-username
docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs
然后你可以使用以下方法推送自己的图像
docker push docker-registry-username/docker-image-name
要将ubuntu-nodejs
图像推送到sammy存储库,命令将是:
docker push sammy/ubuntu-nodejs
上传图像时,该过程可能需要一些时间才能完成,但完成后,输出将如下所示:
The push refers to a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Pushed 5f70bf18a086: Pushed a3b5c80a4eba: Pushed 7f18b442972b: Pushed 3ce512daaf78: Pushed 7aae4540b42d: Pushed ...
如果推送尝试导致此类错误,那么您可能没有登录:
OutputThe push refers to a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Preparing 5f70bf18a086: Preparing a3b5c80a4eba: Preparing 7f18b442972b: Preparing 3ce512daaf78: Preparing 7aae4540b42d: Waiting unauthorized: authentication required
编辑:航网科技 来源:腾讯云
本文版权归原作者所有 转载请注明出处
Copyright © 2011-2020 www.hangw.com. All Rights Reserved 深圳航网科技有限公司 版权所有 增值电信业务经营许可证:粤B2-20201122 - 粤ICP备14085080号
微信扫一扫咨询客服
全国免费服务热线
0755-36300002