3
4

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の基本コマンド

Posted at

外部からイメージを持ってくる場合

$ docker pull ${imageName}
$ docker run -itd --name ${contenaName} ${imageName or imageID}

-p: 外部からアクセスされるポート番号:コンテナ側のポート番号
-v: ホストの絶対パス:コンテナの絶対パス

$ docker exec -it ${contenaName or contenaID} bash
or
$ docker attach ${contenaName or contenaID}

attachexit時、コンテナが停止する

$ docker start ${contenaName or contenaID}
$ docker stop ${contenaName or contenaID}
$ docker rm ${contenaName or contenaID}
 $ docker rmi ${imageName or imageID}

自分でイメージを作成する場合

dockerfile: buildの手順書

./dockerfile
FROM ubuntu

CMD echo 'Hello docker world!'

FROM: imageName
ARG: 変数定義 -> 代入時: $変数名
RUN: build時に実行
CMD: run時に実行

$ docker build -t ${imageName or imageId} ${dockerfileName} ${absolute path}

コンポーズ(イメージ作成からコンテナ起動まで行ってくれる)

Docker Composeのインストール

$ curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

docker-Composeが使用できるか確認

$ docker-compose --version

Docker Composeを使ってみる

./dockerfile
FROM python:3.6

ARG project_dir=/projects/

ADD src/requirements.txt $project_dir

RUN mkdir /test
VOLUME /test/
ADD src/requiremnts.txt /test/
ARG requirements=/test/requirements.txt

WORKDIR $project_dir

RUN pip install -r $requirements
./docker-compose.yml
version: '3'

services:
  flask:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - "./src:/projects"
    tty: true
    environment:
      TZ: Asia/Tokyo
    command: flask run --host 0.0.0.0 --port 5000
./src/app.py
from flask import Flask, jsonify

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

@app.route('/')
def index():
  return jsonify({
    "message": "Hello flask!"
  })

if __name__ == '__main__':
  app.run()
./src/requirements.txt
flask
jsonify
$ docker-compose up -d

アクセスできるかホストから確認

$ curl http://localhost:5000
  {"message":"Hello flask!"}

参考

Docker | docker build と Dockerfile でイメージをビルドする基本
Python+Flask環境をDockerで構築する
Dockerfileについて
docker-compose.ymlの命令を理解して、よりDockerを有効活用したい!

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?