Linux 上Docker的安装与使用入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/powerccna/article/details/77897693
1. 安装Docker
 yum install docker.x86_64 

2. 启动Docker
service docker start
把Docker 加入到开机自启动
chkconfig docker on

3. 去docker 仓库查找image
[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 到本地
docker pull docker.io/centos

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

6. 启动容器,放到后台,不退出,并查看启动的容器id
[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. 进入容器
docker exec -ti abe3a1f3063d /bin/bash

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

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

10. 关闭并删除容器
[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

11. 重启启动容器
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


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

13. copy宿主机器的文件到容器:
docker cp /host-path/file <containerId>:/containter-path
docker cp /home/msg/name.tar 5ed1587b3923:/home/software

            
阅读更多

没有更多推荐了,返回首页