Docker - run image from docker-compose - docker

I have set up a docker-compose.yml file that runs a web service along with postgres.
It works nicely when I run it with docker-compose up.
docker-compose.yml:
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Dockerfile:
FROM python:3
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
CMD ["python", "manage.py", "runserver"]
Is there any way to construct an image out of the services?
I tried it with docker-compose build, but running the created image simply freezes the terminal.
Thanks!

docker-compose is a container orchestration tool, albeit a simple one , and not a bundler of multiple images and preferences into one. In fact, such a thing does not even exists.
What happens when you run docker-compose up is that it effectively runs docker-compose build for those images that need to be built, web in your example, and then effectively replaces the build: . with image: web and executes the configuration as defined by the compose file.
So if you were to run docker-compose build manually and wanted to run the same configuration you have in the compose file manually, you would need to something along the lines of (in order)
run docker-compose build or docker build -t web . to build the web image
run docker run --name db postgres
run docker run --name web -v .:/code -p 8000:8000 web python manage.py runserver 0.0.0.0:8000

Related

Running docker-compose container from docker hub

I have created 2 containers locally and pushed them into docker hub to use on a VM, one is an Angular App and the other is a Django REST API with a db:
Angular App
Dockerfile
FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install npm#8.11.0 --legacy-peer-deps
RUN npm run build --prod
FROM nginx:alpine
COPY --from=node /app/dist/bom-e-barato /usr/share/nginx/html
I created the image by doing docker build -t andreclerigo/bom_e_barato:latest . then pushed it with docker push andreclerigo/bom_e_barato:latest and then I can run it on my VM by doing docker run -d -p 80:80 andreclerigo/bom_e_barato:latest
Django REST API
Dockerfile
FROM python:3.8.10
ENV PYTHONUNBUFFERED 1
RUN mkdir /rest-api-scraper
WORKDIR /rest-api-scraper
ADD . /rest-api-scraper/
RUN pip install -r requirements.txt
docker-compose.yml
version: '3'
services:
web:
build: .
command: bash -c "python backend/manage.py makemigrations && python backend/manage.py migrate && python backend/manage.py runserver 0.0.0.0:8000"
container_name: rest-api-scraper
volumes:
- .:/rest-api-scraper
ports:
- "8000:8000"
image: andreclerigo/rest-api-scraper:latest
I created the image by doing docker-compose build, then I pushed it to docker hub by doing docker-compose push to run locally I can do docker-compose up
Question
What steps do I need to take to pull this image and run the the image (docker-compose up) on my VM?

docker-compose up command not responding

I am using Ubuntu and tried to run Django with Docker.
When I give docker-compose up command it give below output
Starting aug6_web_1 ... done
Attaching to aug6_web_1
web_1 | Watching for file changes with StatReloader
and does not print anything else ... for almost 20 min now.
Here is my docker-compose.yml:
version: '3'
services:
web:
build:
context: ./
command: python manage.py runserver 127.0.0.0:8000
volumes:
- .:/AUG6
ports:
- '8000:8000'
And my Dockerfile:
FROM python:3
ENV PYTHONBUFFERED 1
RUN mkdir /AUG6
WORKDIR /AUG6
COPY /requirements.txt /AUG6/
RUN pip install -r requirements.txt
COPY . /AUG6/
Very first day of learning Docker, excuse if any mistakes
docker-compose up will not terminate as long as the containers are running.
If you want it to start the containers and then terminate, use docker-compose up -d.
Check the docker-compose manual.
EDIT 1:
I just spotted a mistake in your runserver command:
You have python manage.py runserver 127.0.0.0:8000 but it should be python manage.py runserver 127.0.0.1:8000.
You can remove the IP:PORT completely, as you use the defaults. Check the docs.

How to add docker run param to docker compose file?

I am able to run my application with the following command:
docker run --rm -p 4000:4000 myapp:latest python3.8 -m pipenv run flask run -h 0.0.0.0
I am trying to write a docker-compose file so that I can bringup the app using
docker-compose up. This is not working. How do "add" the docker run params to the docker-compose file?
version: '3'
services:
web:
build: .
ports:
- "4000:4000"
volumes:
- .:/code
You need to use command to specify this.
version: '3'
services:
web:
build: .
ports:
- '4000: 4000'
image: myapp:latest
command: 'python3.8 -m pipenv run flask run -h 0.0.0.0'
volumes:
- .:/code
You should use CMD in your Dockerfile to specify this. Since you'll want to specify this every time you run a container based on the image, there's no reason to want to specify it manually when you run the image.
CMD python3.8 -m pipenv run flask run -h 0.0.0.0
Within the context of a Docker container, it's typical to install packages into the "system" Python: it's already isolated from the host Python by virtue of being in a Docker container, and the setup to use a virtual environment is a little bit tricky. That gets rid of the need to run pipenv run.
FROM python:3.8
WORKDIR /code
COPY Pipfile Pipfile.lock .
RUN pipenv install --deploy --system
COPY . .
CMD flask run -h 0.0.0.0
Since the /code directory is already in your image, you can actually make your docker-compose.yml shorter by removing the unnecessary bind mount
version: '3'
services:
web:
build: .
ports:
- "4000:4000"
# no volumes:

Docker does not build and run first service when there are two services in docker-compose

docker-compose up -d works fine when I have only the postgres service in the docker-compose.yml code below. But once I add the python service, the postgres container is never run even though its image is built. docker container ls -a shows that it does not exist.
version: '3'
services:
postgres:
build:
context: .
dockerfile: Dockerfile.postgres
restart: always
container_name: test_postgres
ports:
- "5431:5432"
# Once this python service is added, the postgres does not run.
python:
depends_on:
- postgres
build:
context: .
dockerfile: Dockerfile.python
restart: on-failure:10
container_name: test_python
ports:
- "8001:8000"
I haven't been able to find clear information on why this should be. Some solutions mention that version 3 doesn't use depends_on anymore. I thought this might be a possible issue so I removed it and added restart: on-failure:10 but it made no difference.
If I run docker-compose up -d with just the postgres service in it first, then add the python service into the same docker-compose.yml file and run it again, both images are built and containers run properly.
Not sure if necessary but here are the Dockerfiles for the services:
Dockerfile.postgres:
FROM postgres
WORKDIR /docker-entrypoint-initdb.d
ENV POSTGRES_DB test_postgres
ENV POSTGRES_PASSWORD 1234
COPY init.sql /docker-entrypoint-initdb.d
EXPOSE 5432
Dockerfile.python:
FROM python:latest
RUN mkdir /code
WORKDIR /code
COPY ./backend/ /code
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN python manage.py migrate
RUN python manage.py loaddata customers
EXPOSE 8000
CMD python manage.py runserver 0.0.0.0:8000
What am I doing wrong?

docker-compose run returns /bin/ls cannot execute binary file

I have just started learning Docker, and run into this issue which don't know how to go abound.
My Dockerfile looks like this:
FROM node:7.0.0
WORKDIR /app
COPY app /app
COPY hermes-entry /usr/local/bin
RUN chmod +x /usr/local/bin/hermes-entry
COPY entry.d /entry.d
RUN npm install
RUN npm install -g gulp
RUN npm install gulp
RUN gulp
My docker-compose.yml looks like this:
version: '2'
services:
hermes:
build: .
container_name: hermes
volumes:
- ./app:/app
ports:
- "4000:4000"
entrypoint: /bin/bash
links:
- postgres
depends_on:
- postgres
tty: true
postgres:
image: postgres
container_name: postgres
volumes:
- ~/.docker-volumes/hermes/postgresql/data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
ports:
- "2345:5432"
After starting the containers up with:
docker-compose up -d
I tried running a simple bash cmd:
docker-compose run hermes ls
And I got this error:
/bin/ls cannot execute binary file
Any idea on what I am doing wrong?
The entrypoint to your container is bash. By default bash expects a shell script as its first argument, but /bin/ls is a binary, as the error says. If you want to run /bin/ls you need to use -c /bin/ls as your command. -c tells bash that the rest of the arguments are a command line rather than the path of a script, and the command line happens to be a request to run /bin/ls.
You can't run Gulp and Node at the same time in one container. Containers should always have one process each.
If you just want node to serve files, remove your entrypoint from the hermes service.
You can add another service to run gulp, if you are having it run tests, you'd have to map the same volume and add a command: ["gulp"]
And you'd need to remove RUN gulp from your dockerfile (unless you are using it to build your node files)
then run docker-compose up

Resources