Docker容器安装及基本体验

Docker安装及基本使用目录

安装Docker

卸载旧版的Docker

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

安装依赖

sudo yum install -y yum-utils

设置docker仓库

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker 引擎

sudo yum install -y docker-ce docker-ce-cli containerd.io

启动 Docker

sudo systemctl start docker

测试Docker 是否安装成功

执行Docker run hello-word 见到以下输出 即安装成功

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

基本使用

Docker的工作流程

在本地执行一些DockerClient命令 比如Docker run nginx Docker会启用sever端的Images端的镜像

如果Images 内有相应的nginx 就会放到 Container 中执行

如果没有就去 Registry 中 拉取镜像 然后放到 Container 中执行 拉取镜像默认是在 hub.docker.com
Docker容器安装及基本体验

Docker命令基本体验

docker images 查看镜像

查看当前images 内有那些镜像

[root@iZj6ceofz5f9y3zvpwrig1Z bin]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

docker pull 拉取镜像到本地

比如要拉取 nginx镜像: docker pull nginx (默认拉取最新版本)

拉取之前先docker images 看一下 现在是没有的

[root@iZj6ceofz5f9y3zvpwrig1Z bin]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

然后pull 拉取镜像

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b4d181a07f80: Pull complete 
edb81c9bc1f5: Pull complete 
b21fed559b9f: Pull complete 
03e6a2452751: Pull complete 
b82f7f888feb: Pull complete 
5430e98eba64: Pull complete 
Digest: sha256:47ae43cdfc7064d28800bc42e79a429540c7c80168e8c8952778c0d5af1c09db
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

然后在docker images 看一下

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    4f380adfc10f   11 days ago    133MB
hello-world   latest    d1165f221234   4 months ago   13.3kB

docker run 根据某个镜像创建容器

参数:

  • ​ d 让容器在后台运行,其实就是一个进程
  • ​ --name 给Container 容器指定一个名字
  • ​ -p 将容器的端口映射到宿主机的端口

后台启动nginx ,指定Container 名字为my_nginx 将nginx默认的80端口指定成当前机器的80端口

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker run -d --name my_nginx -p 80:80 nginx  
085e8e8df9ccf14d8bffd742250cb3254ba9e0f5a3be81e92d6d8a12eebc208c

启动后返回Container id

在浏览器中 输入ip:port 进行访问

docker exec 进入 Container

参数:

  • -d : 分离模式: 在后台运行
  • -i : 即使没有附加也保持STDIN 打开
  • -t : 分配一个伪终端

进入Container 之后 可以ls 查看一下目录结构

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker exec -it my_nginx /bin/bash
root@7ceadf2fa7f6:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

目录结构其实和在宿主机上的基本一样

也可以使用 run 后返回的 id

docker exec -it 085e8e8df9cc /bin/bash

docker ps

查看docker正在运行的Container

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                            NAMES
085e8e8df9cc   nginx     "/docker-entrypoint.…"   2 hours ago   Up 2 hours   80/tcp, 0.0.0.0:9090->80/tcp   my_nginx

加上-a 查看未运行的

[root@iZ2zednq312875fg7oxr3bZ ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS                NAMES
24f04186d5ef   python:3.8.8   "python3"                4 minutes ago   Exited (2) 3 minutes ago                        optimistic_napier
b36394f55dab   python:3.8.8   "python3"                4 minutes ago   Created                                         blissful_chebyshev
2347a4ec1933   python:3.8.8   "python3"                5 minutes ago   Exited (0) 4 minutes ago                        fervent_merkle
75dbf77a96be   python:3.8.8   "python3"                8 minutes ago   Exited (0) 8 minutes ago                        my_python
2241ddd2cb6b   nginx          "/docker-entrypoint.…"   3 hours ago     Up 3 hours                 0.0.0.0:80->80/tcp   my_nginx

stop 终止容器

终止后容器会处于未运行状态 可以使用ps -a 查看

先ps 看一下 当前状态

[root@iZ2zednq312875fg7oxr3bZ ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
f0edc5be6346   nginx     "/docker-entrypoint.…"   7 minutes ago   Up 5 seconds   0.0.0.0:80->80/tcp   my_nginx

然后终止容器 然后ps -a 查看下 运行的容器

[root@iZ2zednq312875fg7oxr3bZ ~]# docker stop f0edc5be6346
f0edc5be6346
[root@iZ2zednq312875fg7oxr3bZ ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
f0edc5be6346   nginx     "/docker-entrypoint.…"   8 minutes ago   Exited (0) 8 seconds ago             my_nginx

docker start来重新启动

使用stop 终止了nginx容器 那么现在 可以使用 start 重新启动

[root@iZ2zednq312875fg7oxr3bZ ~]# docker start f0edc5be6346
f0edc5be6346
[root@iZ2zednq312875fg7oxr3bZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
f0edc5be6346   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 12 seconds   0.0.0.0:80->80/tcp   my_nginx

删除images 镜像

先看一下images

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    4f380adfc10f   12 days ago    133MB
hello-world   latest    d1165f221234   4 months ago   13.3kB

然后删除hello-word

docker rmi -f hello-word

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd

删除后再看一下images

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    4f380adfc10f   12 days ago   133MB

删除Container

docker rn -f 通过container id 删除container

先用ps 看一下运行在container内的id

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                            NAMES
085e8e8df9cc   nginx     "/docker-entrypoint.…"   2 hours ago   Up 2 hours   80/tcp, 0.0.0.0:9090->80/tcp   my_nginx

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker rm -f 085e8e8df9cc
085e8e8df9cc

删除后在看一下 container

[root@iZj6ceofz5f9y3zvpwrig1Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

删除全部通过container

docker rm -f $(docker container ls -qa)
上一篇:C语言指针


下一篇:802.11协议精读10:节能模式(PSM)