I'm trying to make one Docker Compose file to up an WEB (reactJS), API (.NET Core 2.1) and an SQL Server instance.
When I init the database and run .NET with dotnet cli, it works (using a connection string Server=localhost). However what I've been googling is that localhost does not work on containers. And when using container I can't get my .NET Core to connect with my SQL Server.
Can anyone shed some light what am I doing wrong?
I have this repo:
https://github.com/lucasgozzi/sagetest
And I'm currently using a branch names 'docker'. Here is my Docker files and composer in case you don't want to clone the repo.
Backend dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1
WORKDIR /src
COPY . .
RUN dotnet restore "./Api/Api.csproj"
RUN dotnet build "Api/Api.csproj" -c Release -o /app/build
RUN dotnet publish "Api/Api.csproj" -c Release -o /app/publish
EXPOSE 5000
WORKDIR /app/publish
ENTRYPOINT ["dotnet", "Api.dll"]
Frontend dockerfile:
# base image
FROM node:12.2.0-alpine
# set working directory
WORKDIR /app
EXPOSE 3000
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY . .
RUN npm install --silent
RUN npm install react-scripts -g --silent
# start app
CMD ["npm", "start"]
Docker compose:
version: '3.1'
services:
api:
container_name: "teste-sage-api"
image: 'teste-sage-api'
build:
context: ./backend
dockerfile: Dockerfile
volumes:
- ./backend:/var/www/backend
ports:
- "5000:5000"
depends_on:
- "database"
networks:
- sagetest-network
web:
container_name: "teste-sage-web"
image: 'teste-sage-web'
build:
context: ./frontend_react
dockerfile: Dockerfile
ports:
- "3000:3000"
depends_on:
- "api"
networks:
- sagetest-network
database:
container_name: "sql-server"
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "Teste#123"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
networks:
- sagetest-network
networks:
sagetest-network:
driver: bridge
You can access the database form your containers on the service name, in your case this will be database. But you need to make sure that the database container is up and running before trying to connect to it. depends_on is not enough for this case. you may need to implement a waitfor in your dotnet container. check this for more info https://docs.docker.com/compose/startup-order/
Related
I have some issues with docker. It's my first time and I don't understand everything.
I use it for my front: React and ViteJS, and for my backend express/MySQL. 1 folder for the front and 1 folder for the back.
If I understand correctly, I need 1 docker file for each folder, and in the parent one docker-compose.yml, right?
They are my docker file for front :
FROM node:16 as build
WORKDIR /src
COPY package*.json ./
RUN npm install --silent
COPY . .
RUN npm run build
FROM nginx
COPY --from=build /src/build /usr/share/nginx/html
EXPOSE 80
and the back :
FROM node:16 AS production
WORKDIR /node
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
The docker-compose :
version: "3.9"
services:
frontend:
build:
context: ./FindMeAStreamer
dockerfile: Dockerfile.prod
ports:
- "5173:80"
# env_file:
# - ./FindMeAStreamer/.env
environment:
- NODE_ENV=production
backend:
build:
context: ./FindMeAStreamer_server
dockerfile: Dockerfile.prod
ports:
- "3000:3000"
# env_file:
# - ./FindMeAStreamer_server/.env
environment:
- DB_HOST=db
- DB_USER=root
- DB_PASSWORD=secret-pw
- DB_DATABASE=findmeastreamer
restart: always
depends_on:
- db
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=findmeastreamer
# - MYSQL_USER=root
# - MYSQL_PASSWORD=secret-pw
- MYSQL_ALLOW_EMPTY_PASSWORD='no'
- MYSQL_ROOT_PASSWORD=secret-pw
ports:
- "3306"
# volumes:
# - my-database:/var/lib/mysql
# networks:
# - mynetwork
adminer:
image: adminer
restart: always
ports:
- 8081:8081
When I do on local npm run build, viteJS make a new folder : dist, with minify code. Inside it's :
---dist
-assets (inside my .png and two index like index-e3d58a7f.js, same for CSS )
index.html
favicon.png
I use this command to launch the docker-compose : docker-compose -f docker-compose-prod.yml up -d --build
But when I check the front containers's logs, i see all my files, but index are not correct. For me, my dockerfile don't copy correctly my folder.. and nothing works. Can you help me to understand why docker doesn't work? :(
I try :
Modify my docker file in the frontend, and change workdir to other names.
Try to add a nginx.conf.
I can't get all the routes of React with docker-compose up command.
docker-compose up => that only allows me to access the default route of the react app. Also, I can access them successfully with local npm run command. Am I missing something, may be in containerisation?
Any ideas why is it happening?
Here's my .yml file
version: "3"
services:
client:
build:
context: ./client
dockerfile: Dockerfile
image: fc-client-app
restart: always
ports:
- "80:80"
volumes:
- /client-app/node_modules
- .:/client-app
depends_on:
- "server"
server:
build:
context: ./server
dockerfile: Dockerfile
image: fc-server-app
ports:
- "8080:8080"
volumes:
- /server-app/node_modules
- .:/server-app
The problem is with the client service.
And here's my Docker File of Client service:-
FROM node:lts
WORKDIR /usr/src/client-app
ENV PATH /usr/src/client-app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
RUN npm install react-scripts#3.4.1 -g
COPY . .
EXPOSE 80
CMD ["npm", "start"]
You are exposing port 8080 in your client docker file, but the port specified in the docker-compose is 80 for your client service. And 8080 is for your server service. Please try changing the client port in your docker file.
If I build and run container with just Dockerfile (without docker-compose)
sudo docker-compose up --build -d
and then run it with :
sudo docker run --hostname sqlserver -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=1StrongPwdclear" -p 1433:1433 -d sql
I am able to
sqlcmd -S localhost -U sa -P 1StrongPwdclear
But, when I build and run the container with docker-compose:
sudo docker-compose up --build
I get :
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed
for user 'sa'..
I don't understand why. I mean I am practically using the same Dockerfile in both the cases.
Dockerfile:
FROM mcr.microsoft.com/mssql/server:2017-latest AS build
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=1StrongPwdclear
WORKDIR /tmp
COPY AdventureWorksLT2017.bak .
COPY restore-backup.sql .
FROM mcr.microsoft.com/mssql/server:2017-latest AS release
ENV ACCEPT_EULA=Y
docker-compose.yml
version: "3"
services:
coreapi:
build:
context: ./theapi
dockerfile: Dockerfile
ports:
- "5000:5000"
sqlserver:
build:
context: ./sqlserver
ports:
- "1433:1433"
environment:
- ACCEPT_EULA="Y"
- SA_PASSWORD="1StrongPwdclear"
angular:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "4300:4200"
I have also check if container is running fine:
This leads me to believe that there is something wrong with my docker-compose file. I am not sure what.
EDIT:
Docker-compose.yml:
version: "3"
services:
coreapi:
build:
context: ./theapi
dockerfile: Dockerfile
ports:
- "5000:5000"
sqlserver:
build:
context: ./sqlserver
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=1StrongPwdclear
angular:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "4300:4200"
Folder structure on ubuntu vm:
frontend-- Dockerfile:
FROM node:alpine
WORKDIR '/app'
COPY ./package.json .
EXPOSE 4200
RUN npm i
COPY . .
CMD ["npm","start"]
sqlserver- Dockerfile
FROM mcr.microsoft.com/mssql/server:2017-latest AS build
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=1StrongPwdclear
WORKDIR /tmp
COPY AdventureWorksLT2017.bak .
COPY restore-backup.sql .
FROM mcr.microsoft.com/mssql/server:2017-latest AS release
ENV ACCEPT_EULA=Y
theapi-- dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
EXPOSE 80/tcp
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "theapi.dll", "--urls", "http://*:5000"]
For your issue, you just need to change the environment setting like this:
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=1StrongPwdclear
Then it will work fine. You can take a look at the environment in docker-compose.
Update:
I do not have other things you used, so I just can test the docker-compose for sqlserver. The Dockerfile here:
FROM mcr.microsoft.com/mssql/server:2017-latest AS build
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=1StrongPwdclear
FROM mcr.microsoft.com/mssql/server:2017-latest AS release
ENV ACCEPT_EULA=Y
And the docker-compose file here:
version: '3.3'
services:
sqlserver-1:
build:
context: .
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=1StrongPwdclear
Then I can also connect to the sqlserver just with the command:
sqlcmd -S localhost -U sa -P 1StrongPwdclear
I'm trying to build a React 16.13.0 app, running in a Docker container (alongside a Django app). I would like to mount my local React directory so that my React docker container reads its files from there so that if I change a file on my local file system, it's automatically picked up by my React docker container. I have this docker-compose.yml file ...
version: '3'
services:
...
client:
build:
context: ./client
volumes:
- /app/node_modules
- ./client:/app
ports:
- '3001:3000'
restart: always
container_name: web-app
environment:
- NODE_ENV=development
- REACT_APP_PROXY=http://localhost:9090
#command: npm run start
depends_on:
- web
...
This is the Dockerfile file in my React directory (client/Dockerfile) ...
FROM node:10-alpine AS alpine
# A directory within the virtualized Docker environment
# Becomes more relevant when using Docker Compose later
WORKDIR /usr/src/app
# Copies package.json and package-lock.json to Docker environment
COPY package*.json ./
# Installs all node packages
RUN npm install
# Finally runs the application
CMD [ "npm", "start" ]
Sadly, this doesn't seem to be working. Changes to my local file system are not getting reflected in my running Docker container. What else should I be doing?
Dockerfile seems ok. Here is portion of docker-compose.yml. Note env. variable CHOKIDAR_USEPOLLING=true at the bottom.
version: '3.7'
services:
react:
container_name: react
build:
context: react/
dockerfile: Dockerfile
volumes:
- './react:/app'
- '/app/node_modules'
stdin_open: true
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
Now I am developing a React application. For the deployment I want to use nginx as the web server. I have written a docker-compose file with two services (One for React app and another for nginx webserver). Usually nginx service needs only the 'build' folder from the react project.
Now my question is how can I copy the 'build' folder from the react container to the nginx container directory when the react container is running.
Please take a look on the Dockerfiles and the yaml file.
docker-compose.yaml
version: "3"
services:
nginx-server:
image: nginx_server:dev
container_name: nginx
build:
context: ./nginx
dockerfile: Dockerfile
restart: always
command: >
sh -c "cp -R /build/ /var/www/html/" // I want to do something like that
volumes:
- .:/react_app_server/nginx
ports:
- 80:80
depends_on:
- react-app
networks:
- server_network
react-app:
container_name: my_react_app
build:
context: .
dockerfile: ./Dockerfile
image: my_react_app:dev
tty: true
volumes:
- .:/react_app
ports:
- "1109:1109"
networks:
- frontend_network
command: >
bash -c "npm run-script build"
networks:
frontend_network:
driver: bridge
server_network:
driver: bridge
volumes:
static-volume:
Dockerfile for React app
FROM node:10.16.3
RUN mkdir /app
WORKDIR /app
COPY . /app
ENV PATH /app/node_modules/.bin:$PATH
RUN npm install --silent
RUN npm install react-scripts#3.0.1 -g --silent
RUN npm run-script build
Dockerfile for Nginx
FROM nginx:1.16.1-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY /prod.conf /etc/nginx/conf.d
Project Directory
My_React_App
build
nginx
Dockerfile
prod.conf
node_modules
public
src
.dockerignore
docker-compose.yaml
Dockerfile
package-lock.json
package.json
README.md
You don't actually need to copy the data, rather making use of same volume should work.
You need to share a named volume between the two containers.
Your docker-compose.yaml should be:
version: "3"
services:
nginx-server:
image: nginx_server:dev
container_name: nginx
|
|
volumes:
- .:/react_app_server/nginx
- app-volume:/var/www/html/
|
|
react-app:
container_name: my_react_app
build:
context: .
|
|
volumes:
- .:/react_app
- app-volume:/path/to/build/folder/
|
|
NOTE: Here app-volume is a named volume which we are mounting at directory inside react-app container where the build folder is expected to get created. The same app-volume named volume is also mounted inside nginx container at /var/www/html/ where you want the build folder to get copied.
Also instead of named volume you can also mount same host directory in both the containers and share the data. -v /samepath/on/host:/path/to/build/folder and -v /samepath/on/host:/var/www/html.
Hope this helps.