7.1 django項目-新聞博客系統之新聞搜索1

Docker

一、docker安裝

1、docker官方安裝

ubuntu下安裝

lsb_release -a   # 查看系統信息
uname -a        # 查看位數

如果是第一次安裝,你需要先添加docker的源然后再安裝

1、更新包

$ sudo apt-get update

2、安裝證書

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

3、添加docker的官方GPGkey

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4、添加docker源

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

5、再次跟新包索引

$ sudo apt-get update

6、安裝docker

$ sudo apt-get install docker-ce

7、檢測

檢測安裝是否成功

$ sudo 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之后,測試hello-world鏡像,終端卡在Unable to find image 'hello-world:latest' locally位置

參考:https://blog.csdn.net/wireless911/article/details/88989620

2、國內源安裝docker

參考:http://www.lxweimin.com/p/9b1dd79b5708?tdsourcetag=s_pcqq_aiomsg

為了方便使用,不用sudo就可以運行docker命令,安裝好docker后再命令行輸入如下命令:

  $ sudo usermod -aG docker $USER

運行正常后,重新連接即可。

二、docker簡單使用

1、docker基本使用

啟動docker 查看docker版本號

$ systemctl start docker

設置開機啟動docker

$ systemctl enable docker

停止docker

$ systemctl stop docker

查看幫助

$ docker --help
[root@localhost ~]# docker --help

Usage:  docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host list          Daemon socket(s) to connect to (default [])
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  container   Manage containers
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  volume      Manage volumes

Commands:
  attach      Attach 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 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

Run 'docker COMMAND --help' for more information on a command.

2、Docker鏡像操作

操作 命令 說明
檢索 docker search 關鍵字egdocker search mysql 我們經常去docker hub上檢索鏡像的詳細信息,如鏡像的TAG
拉取 docker pull 鏡像名:tag :tag是可選的,tag表示標簽,多為軟件的版本,默認是latest(最新的)
列表 docker images 查看所有本地鏡像
刪除 docker rmi image-id 刪除指定的本地鏡像

1、檢索鏡像

[root@localhost ~]# docker search centos

默認會去 https://hub.docker.com/ 搜索,搜過結果如下

[root@localhost ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4709                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              118                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   99                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   63                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              45                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      43                                      
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   39                                      
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   34                                      [OK]
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   33                                      
centos/python-35-centos7           Platform for building and running Python 3.5…   30                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   29                                      
kinogmt/centos-ssh                 CentOS with SSH                                 22                                      [OK]
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   15                                      
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   7                                       
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   5                                       
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   4                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
pivotaldata/centos                 Base centos, freshened up a little with a Do…   2                                       
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   0                                       
pivotaldata/centos7-test           CentosOS 7 image for GPDB testing               0                                       
pivotaldata/centos7-build          CentosOS 7 image for GPDB compilation           0                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]
  • NAME:倉庫名稱
  • DESCRIPTION:鏡像描述
  • STARS:用戶評價,反應一個鏡像的受歡迎程度
  • OFFICIAL:是否官方
  • AUTOMATED:自動構建,表示該鏡像由Docker Hub自動構建流程創建的

2、拉取鏡像

拉取鏡像默認是從docker hub拉取,這是docker默認的公用倉庫,不過缺點是國內下載會比較慢。這里設置從ustc****拉取鏡像****(建議使用)。

在宿主機器編輯文件:vi /etc/docker/daemon.json,在該配置文件中加入(沒有該文件的話,請先建一個):

{
 "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
img

最后,需要重啟docker服務 systemctl restart docker.service 執行拉取鏡像命令

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete 
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
img

3、列表鏡像

[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat          latest              41a54fe1f79d        3 days ago          463 MB
docker.io/centos          latest              5182e96772bf        5 weeks ago         200 MB
  • REPOSITORY:鏡像所在的倉庫名稱
  • TAG:鏡像標簽
  • lMAGE ID:鏡像ID
  • CREATED:鏡像的創建日期(不是獲取該鏡像的日期)
  • SIZE:鏡像大小

這些鏡像都是存儲在Docker宿主機的/var/lib/docker目錄下

img

4、刪除鏡像

[root@localhost ~]# docker rmi 41a54fe1f79d

3、Docker容器操作

操作 命令 說明
運行 docker run --name container-name -d image-nameeg:docker run –name myredis –d redis --name:自定義容器名-d:后臺運行image-name:指定鏡像模板
列表 docker ps(查看正在運行中的容器); docker ps -a:查看歷史運行過的容器docker ps -l:查看最近運行過的容器
停止 docker stop container-name/container-id 停止當前你運行的容器
啟動 docker start container-name/container-id 啟動容器
刪除 docker rm container-id 刪除指定容器
端口映射 -p 6379:6379eg:docker run -d -p 6379:6379 --name myredis docker.io/redis -p: 主機端口(映射到)容器內部的端口
容器日志 docker logs container-name/container-id
更多命令 https://docs.docker.com/engine/reference/commandline/docker/

1、啟動容器

1)以交互方式啟動容器:docker run -it --name 容器名稱 鏡像 /bin/bash;

  • -i:表示以“交互模式”運行容器
  • -t:表示容器啟動后會進入其命令行。加入這兩個參數后,容器創建就能登錄進去。即分配一個偽終端。
  • --name :為創建的容器命名。
  • -v:表示目錄映射關系(前者是宿主機目錄,后者是映射到宿主機上的目錄),可以使用多個-v做多個目錄或文件映射。注意:最好做目錄映射,在宿主機上做修改,然后共享到容器上。
  • -d:在run后面加上-d參數,則會創建一個守護式容器在后臺運行(這樣創建容器后不會自動登錄容器,如果只加-i -t兩個參數,創建后就會自動進去容器)。
  • -p:表示端口映射,前者是宿主機端口,后者是容器內的映射端口。可以使用多個-p做多個端口映射

創建一個交互式容器并取名為mycentos

[root@localhost docker]# docker run -i -t --name=mycentos centos /bin/bash
img

2)以守護進程后臺方式啟動容器:docker run -d --name 容器名稱 鏡像

[root@localhost ~]# docker search tomcat                                       #搜索tomcat
[root@localhost ~]# docker pull tomcat                                         #拉取tomcat鏡像
[root@localhost ~]# docker run -d --name mytomcat -p 8888:8080 tomcat:latest   #啟動tomcat容器,并做端口映射
[root@localhost ~]# docker  ps                                                 #查看容器運行列表
img
img

2、停止容器

docker stop 容器名稱或者容器ID

[root@localhost ~]# docker stop 62a021b1c0fd 
img

3、重啟容器

docker start 容器名稱或者容器ID

[root@localhost ~]# docker start  62a021b1c0fd 

4、刪除容器

刪除容器必須是已經停止的容器,刪除指定容器:docker rm 容器名稱或者容器ID;刪除所有容器:docker rm ‘docker ps -a -q’

[root@localhost ~]# docker rm 62a021b1c0fd
img

以上參考:https://www.cnblogs.com/frankdeng/p/9275092.html

5、容器自啟

  1. 在創建容器的時候加參數

    docker run --restart=always
    
  2. 那已經創建好了怎么辦呢?

    也不用擔心,運行下面的命令

    docker update --restart=always 容器名[容器id]
    

    就可以實現容器的自動啟動

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。