How to put an existing container in docker-compose? - docker

I already have a vue application containerized and running, but how do I put it in docker compose?
Dockerfile:
FROM node:14
WORKDIR /app
RUN npm install #babel/core #babel/node #babel/preset-env nodemon express axios cors mongodb
COPY . .
EXPOSE 4200
CMD ["npm", "run", "serve"]
So when I try to put port "4200" it says that I have already the same port running, so how do I put that container inside whole app which will store multiple containers?
This is my docker-compose try:
version: '3.8'
services:
posts:
build: ./posts
ports:
- "4200:4200"
So this is the visualisation of something that I want to do:

Change host port in compose
For example:
ports:
- "4201:4200"

Related

Docker: connect ECONNREFUSED

I have docker running. There are no errors in the log file.
But when I try to execute a simple request via Postman
POST -> http://ip:3200/api/login
I get an error:
Error: connect ECONNREFUSED ip:3200
What did I do wrong?
docker ps:
CONTAINER_ID 84b2968aa424
IMAGE top-api:latest
COMMAND "docker-entrypoint.s"
PORTS 0.0.0.0:3200->3200/tcp
NAMES top-api
Dockerfile:
FROM node:12-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm add sharp
COPY . .
COPY ./dist ./dist
CMD ["npm", "run", "start:dev"]
docker-compose.yml:
version: '3'
services:
top.api:
image: top-api:latest
container_name: top-api
restart: always
ports:
- 3200:3200
volumes:
- ./.env.dev:/app/.env.dev
From your host you have to connect to: localhost:3200. So when using postman, target localhost:3200.
You're probably listening on 127.0.0.1, which is not publicly exposed (see https://pythonspeed.com/articles/docker-connection-refused/ for diagrams).
Try configuring your webserver to listen on 0.0.0.0.
You can try to expose the port 3200 by adding the following line to your Dockerfile:
EXPOSE 3200

How to deploy docker react app on clever-cloud

I am new to both clever cloud and docker. I want to create an application running on docker with nginx and react to clever cloud. But, everytime I push to clever cloud, deployment failed
ERROR MESSAGE:
Nothing listening on 0.0.0.0:8080 yet. If the deployment fails after this message, please update your configuration and redeploy.
My docker-compose file:
version: '3.7'
services:
front:
container_name: front
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- 8080:80
labels:
NAME: "App Front"
networks:
- app-network
environment:
- CHOKIDAR_USEPOLLING=true
expose:
- 8080
networks:
app-network:
driver: bridge
Content of the Dockerfile
FROM node:alpine as builder
WORKDIR /app
COPY . ./
RUN yarn install
RUN yarn run build
FROM nginx:alpine
COPY docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
create-react-app works fine on Clever Cloud.
Your app should listen on 0.0.0.0:8080, you can set this with environment variables (HOST and PORT).
If you are deploying a Docker application that is not listening on 8080, you can use the CC_DOCKER_EXPOSED_HTTP_PORT environment variable to define the correct port.
NOTE: There are some issues with the latest version (3.4.1)
https://github.com/facebook/create-react-app/issues/8688
Several solutions are proposed to fix this :
Add stdin_open: true on your docker-compose command
Add an CI=true as an environment variable
Downgrade your react-scripts to 3.4.0

docker compose and multi-stage dockerfile with nginx

I've run into a problem where my Docker file is running fine outside of Docker compose, but when used in Docker Compose, my Nginx target isn't installed properly in /usr/share/nginx/html.
docker-compose.yml
version: "3.7"
services:
web:
build:
context: ./web
dockerfile: Dockerfile-development
volumes:
- ./web:/web
- ./web/node_modules/
env_file:
- .env-web
nginx:
build:
context: ./web
dockerfile: Dockerfile-development
target: nginx
volumes:
- ./web/nginx/default.conf:/etc/nginx/conf.d
- ./web/build:/usr/share/nginx/html
ports:
- "80:80"
depends_on:
- web
dockerfile-development
FROM node:9.8.0 as web
ARG APP_ENV=development
WORKDIR /web
COPY package.json /web/package.json
RUN npm install
COPY . /web
RUN npm run build-development
FROM nginx:1.14 as nginx
COPY --from=web /web/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
So if I build and run Dockerfile-development outside of Docker compose everything works and Nginx starts, but if I use docker-compose up the images are built and run without errors, but nginx is no where to be found in the container.
EDIT
I've figured out that the problem lies in build-development which runs webpack --watch. It results nginx not running at all. Any way I can have both webpack with the watch flag run in the background, and still have docker move on to building and running the nginx container?

how to access node api which is running as docker container

I have built a docker-compose file for my node js application that has been dockerized, But I don't know how to make the API call to that node js app which is running as a docker container, Please help me with this concern.
My DockerFile:
FROM node:10.15-slim
ENV NODE_ENV=production
WORKDIR /app
COPY package.json package-lock*.json ./
RUN npm install && npm cache clean --force
COPY . .
CMD ["node", "./bin/www"]
My Docker-compose file:
version: '2.4'
services:
express:
build:
context: .
dockerfile: Dockerfile
command: /app/node_modules/.bin/nodemon ./bin/www
ports:
- 3000:3000
volumes:
- .:/app
environment:
- DEBUG=sample-express:*
- NODE_ENV=development
You'll need to expose the port from docker on which your application is running.
Let's say your application is running on port 8080 inside docker, here's how you can expose that specific port:
EXPOSE 8080
Then you'll need to map the port exposed by docker tthato your local port. Here's how you can do it in docker:
docker run -p 49160:8080 -d docker_image
And if you're working with docker-compose, you'll do it like this:
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
ports:
- "8080:8080"
UPDATE
Let's say you want to send /api requests to back-end server. This is how you'll do it in nginx conf:
server {
listen 80
location /api {
proxy_pass http://backend:8080/;
}
}
I hope it helps.

How do I configure docker compose to expose ports correctly?

I'm using docker and docker compose to run a clojure and a node app, alongside postgres.
The project is contained in the following folder structure.
project/
-- app/
-- -- Dockerfile
-- frontend/
-- -- /Dockerfile
-- docker-compose.yml
The app/Dockerfile looks like so...
FROM clojure:latest
COPY . /usr/src/app
WORKDIR /usr/src/app
EXPOSE 9000
CMD ["lein", "run", "migrate", "&&","lein", "run"]
The frontend/Dockerfile looks like so ...
FROM node:5
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm install
EXPOSE 8080
CMD ["npm", "start"]
And lastly the docker-compose.yml looks like...
frontend:
image: bradcypert/node
volumes:
- ./frontend:/usr/src/frontend
ports:
- "8080:8080"
backend:
image: bradcypert/clojure
volumes:
- ./app:/usr/src/backend
ports:
- "9000:9000"
links:
- postgres
postgres:
image: postgres
ports:
- "5432:5432"
backend is failing for a separate reason, but the frontend seems to be running successfully, that being said, I'm unable to hit localhost:8080 and see the app. What do I need to do make this happen?
Thanks in advance.
Just to clarify, the command being run is docker-compose up
With boot2docker (on Mac or Windows), to access any port from localhost, you have to configure your VirtualBox VM in order to port-forward that port from the VM into the host.
Your port mappings are correct, but you still need to make visible to your host (Mac) the one port you want to access from localhost (your Mac).
See for instance "Using boot2docker to run Docker on a Mac or Windows" from Andrew Odewahn:
That way, you don't have to find out what the IP of your machine is.
(Which you can see with docker-machine ls followed by docker-machine ip <name>)

Resources