Docker

Docker File for Python:

For creating FirstAPI Dockerfile:

FROM python:3.9
WORKDIR /code
COPY ./requirement.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app
CMD ["uvicorn", "app.main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"]

FROM: to declare which package you want to import ex: FROM package: version
WORKDIR: to declare which location your working.
COPY: copy necessary directory to workspace you created directory

 

File Structure:


app :  it contains all the python project related things and executables.

.dockerignore: it contains all the ignore files while building the docker.

requirement.txt: it contains all the modules to be installed from python through pip.

Docker build commands:

Go to the app directory for build image and run:

è docker build -t getting-started .

“. “ => . shows the Dockerfile directory present in

è docker run -dp 3000:3000 getting-started

You use the -d flag to run the new container in “detached” mode (in the background). You also use the -p flag to create a mapping between the host’s port 3000 to the container’s port 3000. Without the port mapping, you wouldn’t be able to access the application.

è docker exec -it a27b7bf86813 /bin/sh

for executing command on code

run bash command

$ docker build -t image-name .

$ docker run -it --rm --name my-running-app image-name

 

Run single Python script:

$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python your
-daemon-or-script.py

 

 

Docker file for React:

For Creating React Dockerfile:

Create React app

è         npx create-react-app gdpu_api

Create Docker file

è         Dockerfile.dev file create inside npm file


Dev Docker


 

è 


Prod Docker 


File Structure

è Docker React repo files structure


è Docker Comand files:

docker build -f Dockerfile.dev -t react_gpdu .

docker run -d -it --rm -p 3000:3000 --name gpdu react_gpdu

docker ps

docker exec -it containerId bin/sh

docker stop container_id

 

Host on nginx:

 

Docker file for Production:

# Fetching the latest node image on apline linux
FROM node:alpine AS builder
# Declaring env
ENV NODE_ENV production
# Setting up the work directory
WORKDIR /app
# Installing dependencies
COPY . .
RUN npm install
# Copying all the files in our project
COPY . .
# Building our application
RUN npm run build
# Fetching the latest nginx image
FROM nginx
WORKDIR /usr/share/nginx/html
# Remove default nginx static assets
RUN rm -rf ./*
# Copying built assets from builder
COPY --from=builder /app/build .
# Copy static assets from builder stage
COPY --from=builder /app/build .
# Copying our nginx.conf
# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]
docker build -t react-nginx .
docker run --rm -it -p 8080:80 react-nginx

 

docker build -f Dockerfile -t react_gpdu .
docker run -d -it --rm -p 8080:80 --name gpdu react_gpdu
docker ps
docker exec -it 52c7368caa17 /bin/sh

 




Comments

Popular posts from this blog

Domain Check Tool

Unlocking the Secrets of Natural Language Processing: How AI Is Changing Communication