转 Linux 上Docker的安装与使用入门

https://blog.csdn.net/powerccna/article/details/77897693

  1. 安装Docker
    [plain] view plain copy
    yum install docker.x86_64

  2. 启动Docker
    [plain] view plain copy
    service docker start
    把Docker 加入到开机自启动
    chkconfig docker on

  3. 去docker 仓库查找image
    [plain] view plain copy
    [root@ ~]# docker search centos
    INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    docker.io docker.io/centos The official build of CentOS. 3827 [OK]
    docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 103 [OK]
    docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8… 90 [OK]
    docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 33

  4. 下载image 到本地
    [plain] view plain copy
    docker pull docker.io/centos

  5. 查看刚刚下载下来的image
    [plain] view plain copy
    [root@ ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    docker.io/centos latest d123f4e55e12 2 weeks ago 196.6 MB

  6. 启动容器,放到后台,不退出,并查看启动的容器id
    [plain] view plain copy
    [root@ ~]# docker run -dt docker.io/centos /bin/bash
    abe3a1f3063de369beb91d3679f11e5f61c0fad91e418022e9b868fb2632cb33
    [root@smsgw1 ~]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    abe3a1f3063d docker.io/centos “/bin/bash” 9 seconds ago Up 8 seconds cocky_wozniak

  7. 进入容器
    [plain] view plain copy
    docker exec -ti abe3a1f3063d /bin/bash

  8. 进入容器后安装JDK 8
    [plain] view plain copy
    [root@abe3a1f3063d /]# yum install java-1.8.0-openjdk.x86_64

安装完后保持为新的镜像
[plain] view plain copy
docker commit 5ed1587b3923 centos/java8
9. 安装这个是 为了运行tomcat, 刚才启动docker的时候,没有带上端口映射的参数。我们退出去再启动下
[plain] view plain copy
[root@abe3a1f3063d /]# exit

  1. 关闭并删除容器
    [plain] view plain copy
    [root@ ~]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    abe3a1f3063d docker.io/centos “/bin/bash” 11 minutes ago Up 11 minutes cocky_wozniak
    [root@smsgw1 ~]# docker stop abe3a1f3063d
    abe3a1f3063d
    [root@ ~]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    abe3a1f3063d docker.io/centos “/bin/bash” 12 minutes ago Exited (137) 17 seconds ago cocky_wozniak
    [root@smsgw1 ~]# docker rm abe3a1f3063d

  2. 重启启动容器
    [plain] view plain copy
    docker run -dt -p 8080:8080 -p 8990:8990 centos/java8 /bin/bash
    指定port range:
    docker run -dt -p 7000-8000:7000-8000 centos/java8 /bin/bash

挂接本地磁盘到容器 docker run -dt -p 7000-8000:7000-8000 -v /usr/local/home:/usr/local/container/home centos/java8 /bin/bash

  1. 从新进入镜像,就像在 Linux下一样,执行你想做的操作吧。
    [plain] view plain copy
    docker exec -ti 5ed1587b3923 /bin/bash

  2. copy宿主机器的文件到容器:
    docker cp /host-path/file :/containter-path
    [plain] view plain copy
    docker cp /home/msg/name.tar 5ed1587b3923:/home/software

你可能感兴趣的:(转 Linux 上Docker的安装与使用入门)