2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeでpython環境

Last updated at Posted at 2020-05-28

はじめに

pythonのバージョン指定ができるコンテナが欲しいと思った次第。(主にicrawlerのせい)
コンテナで作ると簡単に捨てられるし、ポートの管理も楽だということで、docker-composeを使ってやってみることにした。

下準備

ファイルやフォルダを作る。

$ mkdir python
$ cd python
$ touch Dockerfile
$ touch docker-compose.yml
$ touch requirements.txt // 入れたいパッケージは書いておく
$ mkdir workspace

もし、コンテナ内からvolumeしたときに権限がrootにならにようにする方法があったりしたら教えていただきたいです。

Dockerfile
FROM python:3.6

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

ARG DOCKER_UID=1000
ARG DOCKER_USER=docker-user
RUN useradd -m --uid ${DOCKER_UID} --groups sudo ${DOCKER_USER}

USER ${DOCKER_USER}

WORKDIR /home/docker-user
docker-compose.yml
version: "3"
services:
  python36:
    build: .
    container_name: python36
    ports:
      - "5000:5000"
    tty: true
    volumes:
      - ./workspace:/home/docker-user

以上で準備は完了。

コンテナを立てる

コンテナを建てていく。そしたら入るだけ。

$ docker-compose build
$ docker-compose up -d

# 入り方
$ docker-compose exec python36 bash

# rootで入る場合
$ docker-compose exec --user=root python36 bash

こだわりとしては、rootで操作しないところ。ただ書き方として良いのかはわからないからなんとも言えないので注意。

参考

Docker hub
Python, pipでrequirements.txtを使ってパッケージ一括インストール

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?