I tried to run in one container NodeJS and in other container Python3.
I have two node applications and one python aplication.
Dockerfile
WORKDIR /app
COPY package.json /app
RUN npm install
RUN apt-get update || : && apt-get install python -y
COPY . /app
CMD ["npm", "start"]
Docker-Compose
services:
app1:
build: .
ports:
- 3100:3100
command: node ./app.js
app2:
build: .
ports:
- 3000:3000
command: node ./app1.js
app3:
build: .
command: python ./algo.py
Is it possible to run all of these apps in one image with docker-compose
Related
I'm trying to run a python program that uses MongoDB and I want to deploy it on a server, that's because I write a docker-compose file. My problem is that when I run the python project isolated with the docker build -t PROJET_NAME . and docker run image commands everything works properly, however when executing docker-compose up -d the python container restarts over and over again. What am I doing wrong?
I just tried to log it but nothing shows up
Here is the Dockerfile
FROM python:3.7
WORKDIR /app
# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable
# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
# set display port to avoid crash
ENV DISPLAY=:99
ENV PROD=true
COPY . .
# Installing requirements
RUN pip install -r requirements.txt
RUN export PYTHONPATH=$PATHONPATH:`pwd`
CMD ["python3", "foo.py"]
And the docker-compose.yml
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: app
restart: unless-stopped
environment:
APP_ENV: "prod"
APP_DEBUG: "False"
MONGODB_DATABASE: db
MONGODB_USERNAME: appuser
MONGODB_PASSWORD: mongopassword
MONGODB_HOSTNAME: mongodb
depends_on:
- mongodb
networks:
- internal
mongodb:
image: mongo
container_name: mongodb
restart: unless-stopped
command: mongod --auth
environment:
MONGO_INITDB_ROOT_USERNAME: mongodbuser
MONGO_INITDB_ROOT_PASSWORD: mongodbrootpassword
MONGO_INITDB_DATABASE: db
MONGODB_DATA_DIR: /data/db
MONDODB_LOG_DIR: /dev/null
volumes:
- mongodbdata:/data/db
networks:
- internal
networks:
internal:
driver: bridge
volumes:
mongodbdata:
driver: local
How can I connect my postgreSQL database container with my django application.
How can I create a database in postgreSQL while building the image but the case is I have separate container for postgreSQL and in this case How can I connect my postgreSQL.
Dockerfile
FROM ubuntu
ENV PATH="/scripts:${PATH}"
RUN apt update -y
RUN apt-get install debconf-utils
RUN apt install python3.8 -y
RUN apt install python3-pip -y
RUN echo 'tzdata tzdata/Areas select Asia' | debconf-set-selections
RUN echo 'tzdata tzdata/Zones/Asia select Kolkata' | debconf-set-selections
RUN DEBIAN_FRONTEND="noninteractive" apt install -y tzdata
RUN apt-get install -y gdal-bin
RUN apt-get install -y libgdal-dev
COPY ./requirements.txt /requirements.txt
RUN pip install -r requirements.txt
RUN mkdir /app
COPY ./app /app
WORKDIR /app
COPY ./scripts /scripts
RUN chmod +x /scripts/*
# RUN mkdir -p /vol/web/media
# RUN mkdir -p /vol/web/static
# RUN mkdir -p /vol/web/media
# RUN adduser --disabled-password user
# RUN chown -R user:user /vol
# RUN chmod -R 755 /vol/web
# USER user
CMD ["entrypoint.sh"]
docker-compose.yml
version: '3.8'
services:
app:
build:
context: .
environment:
- SECRET_KEY=changeme
- ALLOWED_HOSTS=127.0.0.1,localhost
depends_on:
- db
db:
image: postgres
restart: always
volumes:
- static_data:/static/db
ports:
- 5432:5432
container_name: ae73234b58e8
proxy:
build:
context: ./proxy
volumes:
- static_data:/vol/static
ports:
- 80:8080
depends_on:
- app
volumes:
static_data:
So, here I need to create a database while I build the Dockerfile image and How can I do that?
You can add environment variables in db.
Set the POSTGRES_DB environment variable with name of database you use in django. It will be created once postgres is built.
I am trying to run the docker container for web which has script start.sh file to start it, but docker-compose up web is giving an error.
ERROR: for web Cannot start service web: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/home/app/start.sh\": stat /home/app/start.sh: no such file or directory": unknown
By the following command it shows the start.sh is present in the docker image
docker run -it web bin/bash
docker-compose.yaml
web:
container_name: "web"
environment:
- LANG=C.UTF-8
env_file:
- .env
build: .
volumes:
- ../app:/home/app
- ../media:/home/media
- ../misc:/home/downloads
command: ["/home/app/start.sh"]
dockerfile
# Fetch the base image
FROM ubuntu:18.04
# Install python3 and pip3
RUN apt-get -y update && apt-get install -y python3 python3-pip git libsasl2-dev python-dev libldap2-dev libssl-dev openjdk-8-jdk libwebkitgtk-1.0-0 curl nano wget unzip
# Install pip3 lib
COPY pip3-requirements.txt /pip3-requirements.txt
RUN pip3 install -r pip3-requirements.txt
# Copy Code
ADD . /home/app/
Details:
docker version:
Docker version 19.03.6, build 369ce74a3c
docker-compose version:
docker-compose version 1.17.1, build unknown
base image of dockerfile:
ubuntu:18.04
I'm attempting this again via example, as I think it really does come down to the project structure, sorry if my prior attempt was confusing.
I have a Dockerfile:
FROM ubuntu:18.04
ADD . /home/app
And a docker-compose.yml file:
web:
container_name: "web"
build: .
volumes:
- ../test:/home/app
command: ["/home/app/start.sh"]
With a directory structure of:
./test
docker-compose.yml
Dockerfile
start.sh
Then I can run:
chmod +x start.sh
docker-compose build
docker-compose up
What can be useful to troubleshoot this is to run:
docker run -it web bash
> ls -al / /home /home/app
I hope that helps, I suspect the script isn't being placed into /home/app as the error you are getting is stating.
So I am following this tutorial and have gotten all the way to the 'media' section and when I run the command:
docker-compose exec web python manage.py startapp upload
it all works fine but when I open the newly created views.py file and edit and try to save I get a permission denied error. I can open the file as root and edit it but now thru my Atom code editor. I don't know where I am going wrong can someone help me? Here's my code:
Dockerfile:
# pull official base image
FROM python:3.8.3-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy entrypoint.sh
COPY ./entrypoint.sh .
# copy project
COPY . .
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
docker-compose.yml:
services:
web:
build: ./app
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app/
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
- POSTGRES_DB=hello_django_dev
volumes:
postgres_data:
entrypoint.sh:
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
# python manage.py flush --no-input
# python manage.py migrate
exec "$#"
try to issue chmod 777 -R in the folder where it is located.
Dockerfile
FROM python:3.7
FROM registry.gitlab.com/datadrivendiscovery/images/primitives:ubuntu-bionic-python36-v2020.1.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /bbml
WORKDIR /bbml
RUN pip install -r requirements.txt
RUN pip install --upgrade pip
ADD . /bbml/
CMD [ "python", "./manage.py runserver 0.0.0.0:8800" ]
docker-compose.yml
version: '3'
services:
web:
build: .
command: "python3 manage.py runserver 0.0.0.0:8800"
container_name: bbml
volumes:
- .:/bbml
ports:
- "8800:8800"
So I managed to get this to run properly by doing 'docker-compsoe run web' and go the standard "Starting development server" message at the bottom, but when I go to localhost:8800 it says "site can't be reached". What's going on?
Dockerfile
FROM python:3
FROM registry.gitlab.com/datadrivendiscovery/images/primitives:ubuntu-bionic-python36-v2020.1.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /bbml
WORKDIR /bbml
COPY requirements.txt /bbml/
RUN pip install -r requirements.txt
COPY . /bbml
docker-compose.yml
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
container_name: bbml
volumes:
- .:/app
ports:
- "8000:8000"
Check your folders Permission and Run :
sudo docker-compose up
I found the issue. Docker will be running on a port you actually need to go to :8800 rather than localhost:8800