《Django开发教程》1.1 安装django Docker

以下是在MAC上操作,其他系统类似,需要替换对应命令
参考:https://hub.docker.com/_/django

安装Docker

# brew install --cask --appdir=/Applications docker

下面表示安装成功:

==> Downloading https://desktop.docker.com/mac/main/amd64/78998/Docker.dmg
Already downloaded: /Users/ksuser/Library/Caches/Homebrew/downloads/e5ae7e77584e89d32fa9b0b96db83f3378f9b8196ef004fe0914a32d75504ce4--Docker.dmg
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'
==> Linking Binary 'docker-compose.bash-completion' to '/usr/local/etc/bash_completion.d/docker-compose'
==> Linking Binary 'docker.zsh-completion' to '/usr/local/share/zsh/site-functions/_docker'
==> Linking Binary 'docker.fish-completion' to '/usr/local/share/fish/vendor_completions.d/docker.fish'
==> Linking Binary 'docker-compose.fish-completion' to '/usr/local/share/fish/vendor_completions.d/docker-compose.fish'
==> Linking Binary 'docker-compose.zsh-completion' to '/usr/local/share/zsh/site-functions/_docker_compose'
==> Linking Binary 'docker.bash-completion' to '/usr/local/etc/bash_completion.d/docker'
  docker was successfully installed!

在Applications找到并打开:

image.png

桌面上面任务栏找到这个:


image.png

打开官网:https://hub.docker.com/

搜索django


image.png
$ docker pull django
Using default tag: latest
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

这个报错是docker服务未启动。

方法 1 : 通过 launchctl 查看 docker server, 记住docker server 名:

$ launchctl list | grep docker
-   0   com.docker.helper
86716   0   com.docker.docker.4180

然后关闭和启动它。

$ launchctl stop com.docker.docker.4180 && launchctl start com.docker.docker.4180

这种方法有可能起不来。

方法 2 : 通过docker.app启动

$ open /Applications/Docker.app

继续安装django

# docker pull django

安装完成

Using default tag: latest
latest: Pulling from library/django
75a822cd7888: Pull complete 
e4665cede9d1: Pull complete 
202a45aa091c: Pull complete 
7799136eb561: Pull complete 
7a7f9ca3fd40: Pull complete 
412f2d081014: Pull complete 
Digest: sha256:5bfd3f442952463f5bc97188b7f43cfcd6c2f631a017ee2a6fca3cb8992501e8
Status: Downloaded newer image for django:latest
docker.io/library/django:latest

参考:https://hub.docker.com/_/django

自己build镜像

准备requirements.txt文件

pymongo==3.7.1
django==1.11.23
django-mongoengine==0.3.3
PyYAML==3.13
requests==2.8.1

准备Dockerfile文件:

FROM python:3.6

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    postgresql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY . /user/src/app
RUN pip install -r requirements.txt

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

COPY . /user/src/app
这句的意思把 'docker build -t daozy-django-app .' 命令执行的当前目录内容copy到容器里的/usr/src/app目录下。

build image

如果出现问题,可能是你文件位置不对。

% docker build -t daozy-django-app .
[+] Building 256.3s (11/11) FINISHED                                                                                                                                                         
 => [internal] load build definition from Dockerfile                                                                                                                                    0.0s
 => => transferring dockerfile: 342B                                                                                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/python:3.6                                                                                                                           4.6s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                                                           0.0s
 => [1/5] FROM docker.io/library/python:3.6@sha256:f8652afaf88c25f0d22354d547d892591067aa4026a7fa9a6819df9f300af6fc                                                                     0.0s
 => [internal] load build context                                                                                                                                                       5.3s
 => => transferring context: 88.66MB                                                                                                                                                    5.3s
 => CACHED [2/5] RUN apt-get update     && apt-get install -y --no-install-recommends     postgresql-client     && rm -rf /var/lib/apt/lists/*                                          0.0s
 => CACHED [3/5] WORKDIR /usr/src/app                                                                                                                                                   0.0s
 => [4/5] COPY . /usr/src/app                                                                                                                                                           0.9s
 => [5/5] RUN pip install -r requirements.txt                                                                                                                                         242.6s
 => exporting to image                                                                                                                                                                  2.7s 
 => => exporting layers                                                                                                                                                                 2.6s 
 => => writing image sha256:2f7c53725c1affd5e06c92098949fd40b66a614985e8def2b55b4dd53711372f                                                                                            0.0s 
 => => naming to docker.io/library/daozy-django-app                                                                                                                                     0.0s 
                                                                                                                                                                                             
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them         

启动docker

$ docker run --name daozy -d daozy-django-app

下一课 1.2 在ubuntu上安装Django
Django开发教程-目录大纲

你可能感兴趣的:(《Django开发教程》1.1 安装django Docker)