My docker-compose.yml
version: '3.1'
services:
sqldata:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
- SA_PASSWORD=YourStrong#Passw0rd
- ACCEPT_EULA=Y
ports:
- "1433:1433"
volumes:
- ./docker/mssql:/var/opt/mssql
Each time when I did docker-compose down && docker-compose up -d all databases that I created are deleted.
How to prevent this deleting after each docker restart?
Use docker stop and docker start instead of docker-compose down.
Related
I am working on a MERN Stack Project and I am trying to use Docker for both Development and Production version of the Project. I have created docker-compose for both modes (Dev , Prod) which has 3 services (Frontend, Backend, Database) Now Everything is Connecting correctly and working just fine but For publishing changes in Development mode I am using volumes in it and Now that I am a Windows user, The node_modules in my Project folder and the node_module in Container ( Which are Linux builds for same packages ) are generating Error. I am providing my Docker-Compose File as well.
Error
docker-compose.yml
services:
devengers:
container_name: devengers-root
build:
context: .
dockerfile: Dockerfile.development
image: devengers
backend:
container_name: devengers-backend
image: devengers
ports:
- 3000:3000
environment:
- MONGODB_URL=mongodb://database:27017
networks:
- local_net
depends_on:
- devengers
- database
command: npm run start:dev
volumes:
- ".:/Devengers"
frontend:
container_name: devengers-frontend
image: devengers
ports:
- 8080:8080
environment:
- API=http://backend:3000
networks:
- local_net
depends_on:
- backend
- database
command: npm run dev
volumes:
- ".:/Devengers"
database:
container_name: devengers-database
image: mongo:4.0-xenial
ports:
- 27017:27017
networks:
- local_net
volumes:
- mongodb_data:/data/db
networks:
local_net:
volumes:
mongodb_data:
Why my react build folder doesn't serve after run python manage.py collectstatic command in Dockerfile? I have tried for a long time to dockerized my whole project but I did fail to collect static files. Where I miss please have a look into it.
***This is my backend/Dockerfile inside django project ***
FROM python:3.9.5-slim
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN apt-get update && apt-get install build-essential python-dev -y
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt && pip3 install uwsgi
WORKDIR /django
COPY . .
CMD ['RUN', 'python', 'manage.py', 'collectstatic', '--noinput']
EXPOSE 8000
CMD ["uwsgi", "--http", ":9090", "--wsgi-file", "IMS/wsgi.py", "--master", "--processes", "4", "--threads", "2"]
And this is my frontend/Dockerfile inside my react folder
FROM node:13.12.0-alpine
WORKDIR /react
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
And finally, this is my docker-compose.yml file where I setup 3 services
version: "3.3"
services:
backend:
build:
context: ./backend/
command: gunicorn IMS.wsgi --bind 0.0.0.0:8000
container_name: ims-backend
restart: always
ports:
- "8000:8000"
networks:
- ims-network
ims-postgres:
image: "postgres:14beta2-alpine3.14"
restart: always
container_name: ims-postgres
hostname: emrdsaws.czlz2b677ytu.ap-south-1.rds.amazonaws.com
environment:
- POSTGRES_PASSWORD=zkf!%uW_?&UG%4
- POSTGRES_DB=imsrds
- POSTGRES_USER=dbrdsubuntume12
ports:
- 5432
frontend:
build:
context: ./frontend/
volumes:
- react_build:/react/build
expose:
- 3000
stdin_open: true
nginx:
image: nginx:latest
ports:
- "8080:8080"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
- react_build:/var/www/react
depends_on:
- backend
- frontend
- ims-postgres
volumes:
react_build:
networks:
ims-network:
driver: bridge
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 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/