I've installed Docker on Windows Server 2019 according to this manual but when I try to build my image it fails with errors:
Step 5/13 : RUN python3 -m venv /opt/venv
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
---> Running in 6b034a5cbe59
The command '/bin/sh -c python3 -m venv /opt/venv' returned a non-zero code: 4294967295: failed to shutdown container: container 6b034a5cbe59b5aae61936320a216306c1722c2cccf63a93069ed5897940c290 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container 6b034a5cbe59b5aae61936320a216306c1722c2cccf63a93069ed5897940c290 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)
ERROR: Service 'server' failed to build : Build failed
Here is my Dockerfile:
FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements/ requirements/
RUN python3 -m venv /opt/venv
RUN . ./opt/venv
RUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir --upgrade setuptools
RUN pip install --no-cache-dir -r requirements/common.txt
RUN pip install --no-cache-dir psycopg2-binary
COPY . .
RUN ["chmod", "+x", "/app/entrypoint.sh"]
ENTRYPOINT ["/app/entrypoint.sh"]
docker-compose.yml:
version: '3.9'
services:
server:
build:
context: .
dockerfile: Dockerfile
environment:
- "SQLALCHEMY_DATABASE_URI=<DB_CONNECTION_STR>"
- "RAW_DATA_DIR=/app/mydrive"
restart: "always"
volumes:
- ${CONFIG_PATH}\companyLogo.png:/app/files/branding/companyLogo.png
- mydrive:/app/mydrive
networks:
- internal
ports:
- '80:80'
command: "server"
volumes:
mydrive:
driver: local
driver_opts:
type: cifs
o: 'username=<USERNAME>,password=<PASSWORD>,domain=<DOMAIN>,uid=5000,gid=6000'
device: '<LINK_TO_MY_SERVER>'
networks:
internal:
As it shown above there is a simple Docker image for Flask, and if I will run a pure python:3.8-slim-buster image and run all these commands manually then all works. However, docker build . and docker-compose build fail. I tried to google the error and error code but there is a nothing useful.
Why does it happen? How to fix it?
I believe that you will need to use a Python image for Windows Server, as suggested here.
The official Docker images with Python over Windows are listed in the Docker Hub, marked with windowsservercore.
Related
What I'm trying to do
I'm working on a flask API that is supposed to talk to a remote maria db.
The problem
My local, non-containerized flask can connect to the remote mariadb no problem, as does the local Flask container, but I run into issues deploying it in a docker container on an remote ubuntu host. The container is building and responsive, but it can't connect to the mariadb.
Response when asking API to POST
<title>
sqlalchemy.exc.OperationalError: (mariadb.OperationalError) Can't connect to MySQL server on <mariadb_ip>; (115)
(Background on this error at: https://sqlalche.me/e/14/e3q8) // Werkzeug Debugger
</title>
All that changes from local Flask to remote Flask container is the remote Ubuntu host environment.
Here is my docker compose and dockerfile:
Dockerfile:
FROM python:3.8-slim-buster
WORKDIR /app
RUN apt-get update && apt-get install -y libmariadb-dev
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV FLASK_APP=runner.py
ENV FLASK_ENV=production
ENV FLASK_DEBUG=0
ENV PROPAGATE_EXCEPTIONS=1
EXPOSE 5010
#COPY requirements.txt ./
#RUN pip install -r requirements.txt
RUN pip install gunicorn
COPY . .
CMD ["gunicorn", "--reload", "--bind", "0.0.0.0:5010", "runner:runner"]
docker-compose
version: "3.8"
services:
api:
build: .
restart: always
ports:
- "5010:5000"
What I tried
A lot, but mostly figuring out if I set up Flask incorrectly and it doesn't seem the case. I then tried to focus on the dockerfile and if I overlooked something, but the the Flask API is building and responsive. I just can't figure out why it's working fine locally, but not remotely. I'm worried that it has something to do with the remote Ubuntu host, but I have no clue what that could be and how to resolve it.
How can I install a private repo inside a python image docker? I tried many alternatives but all were unsuccesful. Seems I cant get to set ssh credentials inside a python based image.
My Docker image:
FROM python:3.8
ENV PATH="/scripts:${PATH}"
# Django files
COPY ./requirements.txt /requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
the requirements file has:
git+ssh://git#github.com/my_repo_name.git#dev
And build is triggered from aocker compose file:
....
django_service:
build:
context: ..
dockerfile: Dockerfile
volumes:
- static_data:/vol/web
environment:
- SECRET_KEY=${SECRET_KEY}
depends_on:
....
Perhaps you could use https instead of ssh:
git clone https://${GH_TOKEN}#github.com/username/my_repo_name.git#dev
To set the token inside the Dockerfile use: ARG GH_TOKEN
To keep the token outside the Dockerfile you can build your docker
image with passing the arg like this --build-arg GH_TOKEN=MY_TOKEN
The question is self explanatory but just wanted to add some details. I am running an ubuntu container containing some python flask code:
FROM ubuntu:latest
ADD app/ /app
WORKDIR /app
RUN apt-get update -y && \
apt-get install -y python3-pip python-dev build-essential
RUN pip3 install -r requirements.txt
RUN pip3 install flask
EXPOSE 50000
ENTRYPOINT ["python3"]
CMD ["app.py"]
The docker compose file looks something like this:
version: "2"
services:
app:
container_name: flask-app
restart: always
build:
context: ./
dockerfile: app/Dockerfile
volumes:
- "./app:/app"
ports:
- "5000:5000"
stdin_open: true
tty: true
How do I attach to the container and run an interactive bash shell? Currently the attach command just hangs without returning.
Docker attach:
Attach local standard input, output, and error streams to a running container
...
Note: The attach command will display the output of the ENTRYPOINT/CMD process. This can appear as if the attach command is hung when in fact the process may simply not be interacting with the terminal at that time.
Docker exec:
The docker exec command runs a new command in a running container.
TL;DR: You want docker exec -it [docker-instance-id] /bin/sh to get to a terminal. docker attach will just show you stdout from your flask app from that point on (which might be nothing, which is why it appears to hang).
I am new to docker, currently following book to learn Django.
Is it necessary to be in virtual environment when running the below
command?
I have gone through docker basic videos which says it saves each apps as images. But where these images are saved?.
Does this line make the current pc root directory or dockers Image '
WORKDIR /usr/src/app'
ADD is placed before RUN in the Dockerfile.
$ sudo docker-compose build
But I got these errors.
ERROR: Service 'app' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder912263941/config/requirements.txt: no such file or directory
Dockerfile
FROM python:3
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
mysql-client default-libmysqlclient-dev
WORKDIR /usr/src/app
ADD config/requirements.txt ./
RUN pip3 install --upgrade pip; \
pip3 install -r requirements.txt
RUN django-admin startproject myproject .;\
mv ./myproject ./origproject
docker-compose.yml
version: '2'
services:
db:
image: 'mysql:5.7'
app:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- './project:/usr/src/app/myproject'
- './media:/usr/src/app/media'
- './static:/usr/src/app/static'
- './templates:/usr/src/app/templates'
- './apps/external:/usr/src/app/external'
- './apps/myapp1:/usr/src/app/myapp1'
- './apps/myapp2:/usr/src/app/myapp2'
ports:
- '8000:8000'
links:
- db
requirements.txt
Pillow~=5.2.0
mysqlclient~=1.3.0
Django~=2.1.0
Is it necessary to be in virtual environment when running the below
command?
No, the docker build environment is isolated from the host. Any virtualenv on the host is ignored on the build context and the resulting image.
I have gone through docker basic videos which says it saves each apps
as images. But where these images are saved?.
The images are stored somewhere in /var/lib/docker but isn't meant to be browsed manually. You can send the images somewhere with docker push <image:tag> or save them with docker save <image:tag> -o <image>.tar
Does this line make the current pc root directory or dockers Image ' WORKDIR > /usr/src/app'
That line change the current workdir on the image.
ERROR: Service 'app' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder912263941/config/requirements.txt: no such file or directory
This error means that you do not have config/requirements.txt in your current directory where build is run. Adjust your path on the Dockerfile properly.
$ docker-compose up -d
This will download the necessary Docker images and create a container for the web service.
I am trying to learn Docker. I have a Hello World Django server application. When I try to run my server using a Dockerfile, my server is unreachable. But when I use docker-compose, I am able to access it.
My question is why, especially when they are quite similar.
My Dockerfile:
FROM python:3
# Set the working directory to /app
WORKDIR /bryne
# Copy the current directory contents into the container at /app
ADD . /bryne
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# done!
Commands used when running server using Dockerfile:
docker build -t swyne-latest
docker run swyne-latest
Result: Cannot access server at 127.0.0.1:8000
My docker-compose.yml:
version: '3'
services:
web:
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
container_name: swyne
volumes:
- .:/bryne
ports:
- "8000:8000"
Commands used when running server using docker-compose:
docker-compose up
Result: Able to access my server at 127.0.0.1:8000
Thanks
Edit: Output from Dockerfile build:
$ docker build -t swyne-latest .
Sending build context to Docker daemon 60.15MB
Step 1/6 : FROM python:3
3: Pulling from library/python
05d1a5232b46: Already exists
5cee356eda6b: Already exists
89d3385f0fd3: Already exists
80ae6b477848: Already exists
28bdf9e584cc: Already exists
523b203f62bd: Pull complete
e423ae9d5ac7: Pull complete
adc78e8180f7: Pull complete
60c9f1f1e6c6: Pull complete
Digest: sha256:5caeb1a2119661f053e9d9931c1e745d9b738e2f585ba16d88bc3ffcf4ad727b
Status: Downloaded newer image for python:3
---> 7a35f2e8feff
Step 2/6 : WORKDIR /bryne
---> Running in 9ee8283c6cc6
Removing intermediate container 9ee8283c6cc6
---> 5bbd14170c84
Step 3/6 : ADD . /bryne
---> 0128101457f5
Step 4/6 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
---> Running in 55ab661b1b55
Collecting Django>=2.1 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/32/ab/22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/Django-2.1.2-py3-none-any.whl (7.3MB)
Collecting pytz (from Django>=2.1->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
Installing collected packages: pytz, Django
Successfully installed Django-2.1.2 pytz-2018.5
Removing intermediate container 55ab661b1b55
---> dce5400552b2
Step 5/6 : EXPOSE 8000
---> Running in c74603a76b54
Removing intermediate container c74603a76b54
---> ee5ef2bf2999
Step 6/6 : CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
---> Running in 4f5ea428f801
Removing intermediate container 4f5ea428f801
---> 368f73366b69
Successfully built 368f73366b69
Successfully tagged swyne-latest:latest
$ docker run swyne-latest
(no output)
I guess it's normal that unlike docker-compose up, docker run swyne-latest does not allow you to access the web application at 127.0.0.1:8000.
Because the docker-compose.yml file (which is read by docker-compose but not by docker itself) specifies many parameters, in particular the port mapping, which should otherwise be passed as CLI parameters of docker run.
Could you try running docker run -p 8000:8000 instead?
Also, I guess that the line command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000" should probably put inside the Dockerfile itself with a CMD or ENTRYPOINT directive, not in the docker-compose.yml file.
Actually, I've just taken a look at the output of your docker build command and there is an orthogonal issue:
the command
CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
should be replaced with
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
(See this SO answer for more feedback on this issue, albeit in another language, Java instead of Python.)
As an aside, the complete command to compile the Dockerfile is not docker build -t swyne-latest but docker build -t swyne-latest . (with the final dot corresponding to the folder of the Docker build context).