반응형
스마트팜, 스마트팩토리에 관한 강의를 들었는데 여기서 파이썬 2.6, 텐서플로우 1.x 로 만들어진 파이썬 소스로 만든 프로그램이 2024년에 실행하려고 하니 실행이 안된다. (2021.9월 정도 강의 제작한것으로 보임)
이것 저것 테스트 해본 결과 결론은 도커에서 올리면 정상적으로 실행 되었다.
도커환경
- ubuntu: 18.04
- python: 2.6.9
- tensorflow: 1.14
파이썬 2.6, 텐서플로우 1.x 를 2024년에 사용하기
Dockerfile 파일
#https://codepal.ai/dockerfile-writer.py/query/2yZiifQq/dockerfile-installing-python-3-9-ubuntu-22-04
#https://luis-sena.medium.com/creating-the-perfect-python-dockerfile-51bdec41f1c8
# ubuntu: 18.04
# python: 2.6.9
# tensorflow: 1.14
#
# 도커이미지 만들기 : docker build -t ubuntu1804-py26-tf114 .
# 도커 실행 : docker run --rm -it -v ./ch16SmartFactory:/code -p 8888:8888 ubuntu1804-py26-tf114 /bin/bash
#
# Python 버전 선택
# 필요한 버전을 https://hub.docker.com/_/python 여기서 확인할 수 있습니다.
# FROM python:3.9.6-slim-buster
# Ubuntu 18.04를 사용해야 tensorflow 1.14 버젼을 사용할수 있다.
# Use the Ubuntu 22.04 as the base image - 순수 ubuntu:22.04 size = 77.8MB
FROM ubuntu:18.04
#아래 쉘스크립트 에러 방지 - .bashrc 관련에서 에러
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
# Set environment variables for configuration
#ENV PYTHON_VERSION=2.6
# Set maintainer label
LABEL maintainer="Your Name <your.email@example.com>"
# DEBIAN_FRONTEND=noninteractive : tzdata 설치 시 사용자가 직접 timezone 설정을 할 수 있도록 입력할 수 있는 부분이 나오는데, 도커 이미지를 생성할 때는 입력을 할 수 없으므로 사용자의 입력 없이 넘어가기 위해 설정한다.
# TZ=Asia/Seoul : tzdata 는 시스템 환경변수 TZ 의 값으로 timezone을 설정하기 때문에 해당 환경 변수를 우리가 원하는 지역으로 설정한다.
# ARG 와 ENV : ARG 는 docker build 시에만 적용되는 변수 이며, ENV는 docker container 내부의 환경변수로 설정된다.
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Seoul
# Update the package lists and install necessary packages
# 여기서 tensorflow ..등등의 패키지를 설치하고 이후에 pip install -r requirements.txt 해야 에러가 안나고 정상적으로 만들어 진다. 뭔가 버젼 문제 같은데 해결안됨. 이렇게해서 해결됨(강사님 순서대로 넣은것)
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y tzdata && \
apt-get install -y zip unzip && \
apt install -y python3-pip && \
apt install -y python-pip && \
apt install -y python python-pip python-setuptools python-dev && \
pip3 install --upgrade pip && \
pip3 install --upgrade setuptools && \
apt install -y python-opencv && \
pip3 install flask==2.0.1 && \
pip3 install Flask-SQLAlchemy==2.5.1 && \
pip install --upgrade tensorflow requests && \
pip3 install --user --upgrade tensorflow==1.14 && \
pip3 install efficientnet==1.1.1 && \
pip3 install h5py==2.10.0 && \
pip3 install sklearn && \
pip3 install --upgrade pip && \
pip3 install 'h5py==2.10.0' --force-reinstall && \
apt-get clean
# Set the default working directory
#WORKDIR /app
#RUN echo 'alias python=python3' >> /home/.bashrc
# alias 적용
RUN \
echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc && \
source /root/.bashrc
# python library list 복사
COPY requirements.txt requirements.txt
# python library 설치
RUN pip install -r requirements.txt
# #https://dev-navill.tistory.com/28
# # Dockerfile에 아래 내용 추가
# # 한글 출력을 위한 패키지
# RUN apt-get install locales
# RUN apt-get install -y \
# language-pack-ko && \
# dpkg-reconfigure locales && \
# locale-gen ko_KR.UTF-8 && \
# /usr/sbin/update-locale LANG=ko_KR.UTF-8
#
# # 한글을 출력하기 위해 환경변수 등록
# ENV LANG=ko_KR.UTF-8
# ENV LANGUAGE=ko_KR.UTF-8
# ENV LC_ALL=ko_KR.UTF-8
# 파이썬에서 한글을 사용할 수 있도록 환경변수 등록 : 파이썬이 unicode여서 nicodeEncodeError: 'ascii' codec can't encode character 같은 에러 발생하는것을 방지
ENV PYTHONIOENCODING=UTF-8
# Copy your application code to the container
#배포때만 필요한 - 개발때는 docker -v 옵션으로 실시간 연결로 사용해야함
#COPY . /app
# make sure all messages always reach console
ENV PYTHONUNBUFFERED=1
docker-compose.yml
Dockerfile를 직접 실행 해도 되는데 설정을 편하게 할수 있을거 같아서. 만들어봄, 굳이 이것으로 할 필요는 없음
# Dockerfile을 바로 실행해도 되고 설정 파일만 여기서 바꿀수 있어서 이것도 편한것 같다.
version: '3'
services:
ubuntu1804-py26-tf114:
hostname: ubuntu1804-py26-tf114
container_name: ubuntu1804-py26-tf114
build:
context: .
dockerfile: ./Dockerfile
#stdin_open: true
tty: true
ports:
- 8888:8888
volumes:
- .:/app
- ./ch16SmartFactory:/code
# command:
# - /bin/bash
# - -c
# - |
# export PYTHONIOENCODING=utf-8
# entrypoint: [ "/bin/bash", "-c", "python /code/main.py"]
도커 빌드
docker build -t ubuntu1804-py26-tf114 .
도커 실행
docker run --rm -it -v ./ch16SmartFactory:/code -p 8888:8888 ubuntu1804-py26-tf114 /bin/bash
ls
cd code
ls
python -V
cat /etc/issue
python ./main.py
결과 화면
반응형
'ML' 카테고리의 다른 글
HDF5 파일 로딩 에러 (ImportError: `load_weights` requires h5py package when loading weights from HDF5. Try installing h5py.) (1) | 2024.03.21 |
---|---|
윈도우 Tensorflow2 GPU 셋팅 (f. miniconda) (1) | 2024.03.20 |
Windows11 Tensorflow2, NVIDIA CUDA 사용하기 (feat. wsl) (0) | 2024.03.19 |
딥러닝에서 정밀도, 재현율이란? (0) | 2024.03.13 |