Docker安装与测试

mac安装

brew install docker

安装完成后

docker --version 看一下是否安装成功

  • docker 容器centos中yum install ngninx 报错:
安装nginx yum install nginx 时报错:No package nginx available.

解决方法:

先安装epel:

yum install epel-release
  • 查看本地的镜像
wenqidongdeMBP:opt wenqidong$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
cdinformation/mogu   bind_port           0224d89fcb69        36 minutes ago      468MB
cdinformation/mogu   latest              c44920a24934        2 hours ago         333MB
centos               latest              1e1148e4cc2c        3 months ago        202MB
  • 查看本地正在运行的容器,查看所有容器,启动一个容器,进入一个容器

    wenqidongdeMBP:opt wenqidong$ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    wenqidongdeMBP:opt wenqidong$ docker ps -a
    CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                           PORTS               NAMES
    8978e4a8f620        cdinformation/mogu   "/bin/bash"         About an hour ago   Exited (1) 3 minutes ago                             blissful_liskov
    d7b53a8cd3a0        cdinformation/mogu   "/bin/bash"         2 hours ago         Exited (137) About an hour ago                       gifted_kilby
    f9f9bff71b22        centos:latest        "/bin/bash"         3 hours ago         Exited (1) 2 hours ago                               nervous_brown
    wenqidongdeMBP:opt wenqidong$ docker start 8978e4a8f620
    8978e4a8f620
    wenqidongdeMBP:opt wenqidong$ docker ps
    CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
    8978e4a8f620        cdinformation/mogu   "/bin/bash"         About an hour ago   Up 5 seconds        127.0.0.1:80->8888/tcp   blissful_liskov
    wenqidongdeMBP:opt wenqidong$ docker attach 8978e4a8f620
    [root@8978e4a8f620 /]# 
    • docker ps 查看当前正在运行的容器
    • docker ps -a查看所有容器
    • docker start 8978e4a8f620 启动8978e4a8f620容器
    • docker attach 8978e4a8f620 进入8978e4a8f620容器
  • 移除容器、移除镜象

    • 移除容器:docker rm container_iddocker rm container_name
    • 移除镜像: docker rmi image_iddocker rm image_name
    • 因为容器是根据镜像创建的,所以在移除镜像前,需要先移除所有根据该镜像创建的容器

创建容器

作为入门级操作,我们先从镜像启动一个容器,即docker run操作。

[root@xxx ~]# docker run -it centos:latest /bin/bash

这里-it是两个参数:-i和-t。前者表示打开并保持stdout,后者表示分配一个终端(pseudo-tty)。此时如果使用exit退出,则容器的状态处于Exit,而不是后台运行。

如果想让容器一直运行,而不是停止,可以使用快捷键 ctrl+p ctrl+q 退出,此时容器的状态为Up。除了这两个参数之外,run命令还有很多其他参数。其中比较有用的是-d后台运行。

  • dockerhub的登陆
docker login

按提示输入用户名和密码,首次登陆后电脑会记录登陆状态,后续就可以直接push本地镜像到远端仓库了.

  • docker的commit操作
docker commit -m "some description message" -a "用户名" 72f1a8a0e394 cdinformation/mogu:tagname
  • 72f1a8a0e394 需要commit 的container ID

  • cdinformation/mogu:tagname 这个是dockerhub 的 用户名/仓库名称:标签名称,标签名称可以随意写,尽量有意义

  • commit 之后会生成一个新的本地镜像,可以通过docker images查看

  • commit 之后就可以通过docker push dockerhub_username/repository:tagname将本地镜象推送到前端仓库,供别人使用.

  • docker中的centos中用不了systemctl,会提示错误:

    Failed to get D-Bus connection: Operation not permitted
    • 因为docker 中的linux不是一个完整的系统

    However, let me point out that containers are not really designed to operate the same way as a typical full stack operating system. There is no kernel and, as a result (by default), no service management system (which relies on kernel functionality to operate as expected).

    Now, you CAN get systemd installed, but it is still experimental and not recommended (or supported officially). Containers should be both ephemeral and 'specific', meaning that you are doing one thing generally and one thing only. If you have something you want to run as a 'service', you can generally accomplish that by starting the daemon directly (for example, with Apache web server, you could run '/sbin/apachectl -D FOREGROUND' which would start the apache daemon for HTTP service).

    简单的说,就是要在docker的centos中启动httpd, mysqld这些,只能找到那个bin文件,通过参数命令启动;而不能使用systemctl start httpd

    you need to use the CMD directive to run Apache from your Dockerfile, here is an example:

    FROM    centos:7
    RUN yum update && yum install httpd httpd-tools -y
    EXPOSE  80
    CMD     ["/usr/sbin/httpd","-D","FOREGROUND"]

https://hub.docker.com/ user: cdinformation/2019

如何通过本地浏览器访问容器中的nginx

  • 在创建容器时,需要指定端口映射
docker run -it -p 80:8808 cdinformation/mogu

前面的80是本地浏览器端口,后面的8808是容器中nginx的端口;

  • 删除容器: docker rm

  • 删除所有容器: docker rm $(docker ps -a -q)

  • 移除镜像: docker rmi

  • 移除所有镜像: docker rmi $(docker ps -a -q)

  • Docker中文文档

Docker官方英文资源

docker官网:

Docker Windows 入门:

Docker CE(社区版) Ubuntu:

Docker mac 入门:

Docker 用户指引:

Docker 官方博客:

Docker Hub:

Docker开源:

Docker中文资源

Docker中文网站:

Docker安装手册:

Docker 国内镜像

网易加速器:

官方中国加速器:

ustc的镜像:

daocloud:(注册后使用)