0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerコマンド

Last updated at Posted at 2025-05-18

基本コマンド

# イメージ取得
docker pull nginx:1.21.4-alpine
docker pull php:8.1.0-fpm-alpine3.15
docker pull mysql:5.7

# 初回コンテナ立ち上げ
docker run -d --name nginx -p 80:80 nginx:1.21.4-alpine
docker run -d --name php-fpm php:8.1.0-fpm-alpine
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7

# コンテナ状態確認
docker ps -a

# コンテナに入る
docker exec -it nginx sh

# コンテナ停止
docker stop nginx

# コンテナ起動
docker start nginx

# コンテナ削除
docker rm nginx

イメージ作成〜Pushまで

今回はPython&node環境を作成

# ①ベースにしたいイメージからコンテナ起動
docker run --rm -it ubuntu:24.04 sh

# ②コンテナ内で必要なモジュールを確認&インストール
sh# apt update
sh# apt install -y curl git

# ③ある程度見通しが立ったらそれを元にDockerfile作成
vi Dockerfile

# Dockerfileをビルドしてイメージ作成。エラーになったらベースイメージから確認して①〜③の繰り返し
docker build --no-cache -t npy:v1 -f Dockerfile .
docker run --rm -it npy:v1 sh
# ローカルのファイルや環境変数を使用する場合
docker run --rm -it -v ./requirements.txt:/app/requirements.txt -v ./scripts:/app/scripts --env-file=.env npy:v1 sh

# 今回はGitHub Container registryにイメージをPush
# https://docs.github.com/ja/packages/working-with-a-github-packages-registry/working-with-the-container-registry#personal-access-token-classic
export CR_PAT=YOUR_TOKEN
echo $CR_PAT | docker login ghcr.io -u xxxxx --password-stdin

# イメージをPush
docker build -t ghcr.io/NAMESPACE/npy:v1 -f Dockerfile .
docker push ghcr.io/NAMESPACE/npy:v1

# 手元のイメージ削除
docker rmi npy:v1
docker rmi ghcr.io/NAMESPACE/npy:v1

# PushしたイメージをPullして動作確認
docker run --rm -it ghcr.io/NAMESPACE/npy:v1 sh

今回作ったDockerfile

FROM ubuntu:24.04

ARG PYTHON_VERSION=3.12.10
ARG NODE_VERSION=22.2.0

# https://github.com/astral-sh/uv/pkgs/container/uv
COPY --from=ghcr.io/astral-sh/uv:0.7.5 /uv /uvx /bin/

WORKDIR /app

RUN apt update
RUN apt install -y curl git make

# pythonバージョン管理 uv
# https://docs.astral.sh/uv/
RUN uv venv --python $PYTHON_VERSION
ENV PATH="/app/.venv/bin:$PATH"

# nodeバージョン管理 n
# https://github.com/tj/n
RUN curl -L https://bit.ly/n-install | bash -s -- -y $NODE_VERSION
ENV PATH="/root/n/bin:$PATH"

サクッと動作確認したかったのでメモ

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?