Docker #3 - Get started(2) build, run, stop

2021. 4. 22. 13:42

1. Docker Image Build

##Image Build Command
##docker build [OPTIONS] PATH | URL | -

##repository에 들어가는 docker image 이름은 반드시 소문자로 작성해야한다.
docker build -t hellodocker C:\\Users\\ISET-DA\\Workplace\\Docker\\docker-get-started

 

 

 

2. Docker Container run, stop, remove

## Container Run Command
##docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

## -p 옵션이 아닌 -d 옵션을 주면 Background에서 동작한다
docker run -p 4000:80 hellodocker

 

## Container Info Command
##docker container ls [OPTIONS]

docker container ls

 

## Container Stop Command
##docker container stop [OPTIONS] CONTAINER [CONTAINER...]

## 컨테이너를 종료시킬 수 있는 여러가지 방법이 있지만, 대표적으로 stop 뒤에 컨테이너 아이디를 적어주면 된다
docker container stop 7ed0f4e368af

 

 

## Container Remove Command
##docker container rm [OPTIONS] CONTAINER [CONTAINER...]

## 사용이 끝난 컨테이너를 삭제하여 리소스를 정리한다
docker container rm 7ed0f4e368af -f


 

Summary

##Image Build Command
docker build [OPTIONS] PATH | URL | -

## Container Run Command
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

## Container Info Command
docker container ls [OPTIONS]

## Container Stop Command
docker container stop [OPTIONS] CONTAINER [CONTAINER...]

## Container Remove Command
docker container rm [OPTIONS] CONTAINER [CONTAINER...]

  Dockerfile을 이용하여 Image를 만든다.

  Image를 이용하여 Container를 만든다.

 

 


Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
# 현재 디렉토리의 내용을 /app 하위에 카피한다.
ADD . /app

# Install any needed packages specified in requirements.txt
# requirements.txt에 명시되어있는 필요로하는 패키지를 인스톨 한다
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python","app.py"]

 

 

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello() :
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disable</i>"
    
    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80)

 

 

requirements.txt

Flask
Redis

 

 

Reference

 - docs.docker.com/get-started/

 

Orientation and setup

 

docs.docker.com

 

 

'cloud computing > docker' 카테고리의 다른 글

Docker #5 - Ubuntu에 Docker Engine 설치하기  (0) 2021.06.08
vmmem  (0) 2021.05.20
Docker #4 - Get started(3) tag, push  (0) 2021.04.22
Docker #2 - Get started(1)  (0) 2021.04.22
Docker #1 - Docker overview  (0) 2021.04.22

BELATED ARTICLES

more