"Creating a service that edits the usage history of Mobile Suica so that it can be easily used for expense settlement" First of all, we will develop in Python, so create an environment where Python can be executed
windows PC (Core i7-7600 / 16GB) Edit with VS code It is convenient to include Python and Docker extensions
Since it is developed on a Windows PC, it is troublesome to do it directly. Python execution environment sets up a container with Docker
Install ** Docker for Windows ** It is necessary to enable Hyper-V. Also install [** docker-compose **] * [Docker] Install Docker and Docker Compose on Windows 10 Pro 64bit
I will install various things, so build the image Save the following file as Dockerfile in a suitable directory somewhere
Dockerfile
FROM centos/python-36-centos7:latest
USER root
RUN yum -y install java-1.8.0-openjdk
RUN pip install flask pandas tabula-py uwsgi
WORKDIR /src
ENV PYTHONUNBUFFERED=1
Since you edit the Python file on Windows and run it on Docker, you need to be able to see the Python file on Windows from the container. This can be specified with "-v" when launching the container, but since Windows is troublesome and I do not want to type the command every time, docker-compose makes one command
docker-compose.yml
version: '3.7'
services:
testenv:
build:
context: ./app
dockerfile: Dockerfile
container_name: testenv
tty: true
command: /bin/sh -c "while :; do sleep 10; done"
volumes:
- ./app/src:/src
volumes:
temp:
If there is no command: part, the container will drop in an instant When I google, most people solve it with tty: true, but for some reason it is useless in my environment
Place the Dockerfile and docker-compose.yml from earlier like this test.py is a Python file to run
│ docker-compose.yml
│
└─app
│ Dockerfile
│
└─src
test.py
$ docker-compose.exe up --build -d
Creating network "test_default" with the default driver
Building testenv
Step 1/6 : FROM centos/python-36-centos7:latest
---> 90c6a4022ee5
Step 2/6 : USER root
---> Using cache
---> 03e9f59a6dbc
Step 3/6 : RUN yum -y install java-1.8.0-openjdk
---> Using cache
---> 463337c7b050
Step 4/6 : RUN pip install flask pandas tabula-py PyPDF2 uwsgi crontab
---> Using cache
---> 26c28d2b838c
Step 5/6 : WORKDIR /src
---> Running in f5ab2a9b405e
Removing intermediate container f5ab2a9b405e
---> b998a42b8180
Step 6/6 : ENV PYTHONUNBUFFERED=1
---> Running in fd8d783f9e2f
Removing intermediate container fd8d783f9e2f
---> e1e0833bc33a
Successfully built e1e0833bc33a
Successfully tagged test_testenv:latest
See if you got up
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b7e97ef1fd5 test_testenv "container-entrypoin…" 4 seconds ago Up 2 seconds 8080/tcp testenv
Try to enter the container
$ winpty docker exec -it testenv bash
(app-root) bash-4.2# pwd
/src
(app-root) bash-4.2# ls
test.py
(app-root) bash-4.2# cat test.py
print('Hello, World')
(app-root) bash-4.2# python3 test.py
Hello, World
Now you can finally start writing the code.
Recommended Posts