atul@atul-Lenovo-G570:~/softbook_docker$ docker --version
Docker version 28.0.1, build 068a01e
atul@atul-Lenovo-G570:~/softbook_docker$ docker version
atul@atul-Lenovo-G570:~/softbook_docker$ touch Dockerfile
softbook_docker/Dockerfile
docker file in project root directory.# Use the official Python image
FROM python:3.12.8
# Set the working directory
WORKDIR /softbook_docker
# This command put requirements.txt in container in specific directory
COPY ./requirements.txt /softbook_docker/requirements.txt
# This command will install dependancies in docker container
RUN pip install --no-cache-dir --upgrade -r /softbook_docker/requirements.txt
# copy source code files and directory in docker container
COPY ./app /softbook_docker/app
# copy the nginx configuration file
COPY ./nginx.conf /etc/nginx/nginx.conf
# this is default command. It will run after container start
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
FROM python:3.12.8
FROM
statement defines which image to download. It is specify that which base image will be use by container. It must be the first command in your Dockerfile
. A Dockerfile can have multiple FROM
statements which means the Dockerfile produces more than one image.WORKDIR /softbook_docker
COPY ./requirements.txt /softbook_docker/requirements.txt
requirements.txt
file from current working directory of host machine.requirements.txt
file in softbook_docker directory in container of docker. If softbook_docker
directory is not exist then docker will create this directory automatically.RUN pip install --no-cache-dir --upgrade -r /softbook_docker/requirements.txt
COPY ./app /softbook_docker/app
./app
will be copy./softbook_docker/app
this directory not available in container then docker will create automatially.CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
app
is directory and main
is main.py
file that is created in app
directory and :app
is a opject that is written in app/main.py
filesoftbook_docker/Dockerfile
file created in project root directory then you will create image of dockeratul@atul-Lenovo-G570:~$ cd softbook_docker
sofbookdockerimage
is docker image nameatul@atul-Lenovo-G570:~/softbook_docker$ docker build -t sofbookdockerimage .
docker inspect
command.atul@atul-Lenovo-G570:~/softbook_docker$ docker inspect sofbookdockerimage
atul@atul-Lenovo-G570:~/softbook_docker$ docker images
softbook_docker/Dockerfile
file created in project root directory then you will create image of docker.atul@atul-Lenovo-G570:~/softbook_docker$ docker build -t softbookdockerimage:1.0 .
docker inspect
command.atul@atul-Lenovo-G570:~/softbook_docker$ docker inspect softbookdockerimage:1.0
atul@atul-Lenovo-G570:~/softbook_docker$ docker images
softbookdockercontainer
is container nameatul@atul-Lenovo-G570:~/softbook_docker$ docker run -d -p 8000:8000 --name softbookdockercontainer sofbookdockerimage
atul@atul-Lenovo-G570:~/softbook_docker$ docker ps -a
:~/softbook_docker$ docker create -p <host_port>:<container_port> --name dev_container softbookdockerimage:1.0
atul@atul-Lenovo-G570:~/softbook_docker$ docker create -p 8000:8000 --name dev_container softbookdockerimage:1.0
CREATED
atul@atul-Lenovo-G570:~/softbook_docker$ docker ps -a --filter ancestor=b410fcd67bc1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9839f284f9b1 softbookdockerimage:1.0 "uvicorn app.main:ap…" 3 minutes ago Created dev_container
atul@atul-Lenovo-G570:~/softbook_docker$ docker ps -a
command: :~/softbook_docker$ docker start
atul@atul-Lenovo-G570:~/softbook_docker$ docker start 9839f284f9b1
Up 9 seconds
.atul@atul-Lenovo-G570:~/softbook_docker$ docker ps -a --filter ancestor=b410fcd67bc1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9839f284f9b1 softbookdockerimage:1.0 "uvicorn app.main:ap…" 5 minutes ago Up 9 seconds dev_container
atul@atul-Lenovo-G570:~/softbook_docker$ docker stop softbookdockercontainer
Exited (0) 9 seconds ago
atul@atul-Lenovo-G570:~/softbook_docker$ docker ps -a --filter ancestor=b410fcd67bc1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9839f284f9b1 softbookdockerimage:1.0 "uvicorn app.main:ap…" 9 minutes ago Exited (0) 9 seconds ago dev_container
atul@atul-Lenovo-G570:~/softbook_docker$ docker restart softbookdockercontainer
atul@atul-Lenovo-G570:~/softbook_docker$ uvicorn app.main:app --reload
atul@atul-Lenovo-G570:~$ docker images
atul@atul-Lenovo-G570:~$ docker ps -a
atul@atul-Lenovo-G570:~$ docker ps -a --filter ancestor=315b6543b1b0
atul@atul-Lenovo-G570:~$ docker rm 7d9dc2fb0e81
atul@atul-Lenovo-G570:~$ docker rmi 315b6543b1b0
docker logs <container name or id>
atul@atul-Lenovo-G570:~$ docker logs fastapiappcontainer
docker compose logs
commanddocker compose logs
used to check all services are running that is defined in docker-compose.yml
fileyml
file available
atul@atul-Lenovo-G570:~/greenbook$ docker compose logs
atul@atul-Lenovo-G570:~$ docker pull nginx:stable
atul@atul-Lenovo-G570:~$ docker run -d -p 80:80 --name softbookdocker_nginx -v $(pwd)/softbook_docker/nginx.conf:/etc/nginx/nginx.conf nginx:stable
softbook_docker_network
is network nameatul@atul-Lenovo-G570:~/softbook_docker$ docker network create softbook_docker_network
atul@atul-Lenovo-G570:~/softbook_docker$ docker network ls
$ docker network connect <network name> <container name>
atul@atul-Lenovo-G570:~/softbook_docker$ docker network connect softbook_docker_network dev_container
atul@atul-Lenovo-G570:~$ docker network inspect bridge
atul@atul-Lenovo-G570:~$ docker network inspect softbook_docker_network
atul@atul-Lenovo-G570:~$ docker login -u atulkrishnathakur
'/home/username/.docker/config.json'.
testfastapi
atul@atul-Lenovo-G570:~$ docker tag softbookdockerimage:1.0 atulkrishnathakur/testfastapi:1.0
$ docker push <docker user name>/<docker repository>:<tag>
atul@atul-Lenovo-G570:~$ docker push atulkrishnathakur/testfastapi:1.0
docker images
command then you got softbookdockerimage:1.0
and atulkrishnathakur/testfastapi:1.0
with same image id. It means atulkrishnathakur/testfastapi:1.0
not a new image. It is a simply new reference(tag) of the same image.$ docker pull <docker user name>/<repository name>:<tag>
atul@atul-Lenovo-G570:~$ docker pull atulkrishnathakur/testfastapi:1.0
1 command : $ docker exec -it <container name or container ID> /bin/bash
atul@atul-Lenovo-G570:~$ docker exec -it nginx-container /bin/bash
# or
atul@atul-Lenovo-G570:~$ docker exec -it nginx-container bash
After running you will see.
root@b57dce611822:/#
b57dce611822
is the container ID. And root
is the user of container.-u 0
option specifies the root user (user ID 0)docker exec -u 0 -it <container_name_or_id> bash
atul@atul-Lenovo-G570:~$ docker exec -u 0 -it pgadmin4container bash
docker exec -u <username_or_id> -it <container_name_or_id> bash
atul@atul-Lenovo-G570:~$ docker exec -u pgadmin -it pgadmin4container bash
command: exit
root@b57dce611822:/# exit
then you will see again
atul@atul-Lenovo-G570:~$
atul@atul-Lenovo-G570:~$ docker volume ls
/var/lib/docker/volumes/<volume_name>/
path in linuxcommand: $ docker rm volume <volume name>
atul@atul-Lenovo-G570:~$ docker rm volume greenbook_postgresdata
command: $ docker export -o <filename>.tar <container_id_or_name>
atul@atul-Lenovo-G570:~$ docker export -o /home/atul/docker_container_backup/greenbook_fastapiappcontainer.tar 78ccc7f6da33
None
.$ docker save -o <file_name>.tar <image_name>:<tag>
atul@atul-Lenovo-G570:~$ docker save -o /home/atul/docker_container_backup/mydocker_image2.tar fastapiappimage:9.0
atul@atul-Lenovo-G570:~$ docker import <filename>.tar <New Image Name>
import
command does not import container in existing imageatul@atul-Lenovo-G570:~$ docker import /home/atul/docker_container_backup/greenbook_fastapiappcontainer.tar fastapiappimported
command: $ docker load -i <imagefilename>.tar
atul@atul-Lenovo-G570:~$ docker load -i /home/atul/docker_container_backup/greenbook_image.tar
.yml
file by docker compose
commandIf user not login in docker then login docker first.
If you created docker-compose.yml
. It is the default file. you can run this file using below command
atul@atul-Lenovo-G570:~/microhub/microhub-gateway$ docker compose up -d --build
If you want to run custom docker compose file then use -f
flag in command as like below command
atul@atul-Lenovo-G570:~/microhub/microhub-gateway$ docker compose -f docker-compose.dev.yml up -d --build
atul@atul-Lenovo-G570:~/greenbook$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fastapiappimage 8.0 6cdf9ce8856e 31 minutes ago 1.4GB
<none> <none> cef8117d5e3f 50 minutes ago 1.4GB
<none> <none> 407ea7696834 About an hour ago 1.4GB
<none> <none> 3be28394f137 About an hour ago 1.4GB
<none> <none> 8cfe99548974 2 hours ago 1.4GB
<none> <none> 9a1d2ed0ced7 2 hours ago 1.4GB
<none> <none> 02f198c45d11 2 hours ago 1.4GB
<none> <none> 7592a7c416b7 3 hours ago 1.4GB
<none> <none> 986f3743b5de 3 hours ago 1.4GB
<none> <none> 5a757a60e254 3 hours ago 1.4GB
<none> <none> a87369cab88c 3 hours ago 1.4GB
<none> <none> 9e320be8f65a 3 hours ago 1.4GB
<none> <none> ede03278bb80 3 hours ago 1.4GB
<none> <none> a7777d702011 4 hours ago 1.4GB
<none> <none> 4fe53649bbe1 4 hours ago 1.4GB
<none> <none> cf350a71dc10 4 hours ago 1.4GB
<none> <none> 6e73228758aa 4 hours ago 1.4GB
<none> <none> 2216797f4296 4 hours ago 1.4GB
<none> <none> b37bfc1c64bb 4 hours ago 1.4GB
<none> <none> 63c88fc18355 4 hours ago 1.4GB
<none> <none> 684a69d4a17a 4 hours ago 1.4GB
<none> <none> c34083d0b903 5 hours ago 1.4GB
atul@atul-Lenovo-G570:~$ docker image prune -f
atul@atul-Lenovo-G570:~$ docker system prune -a --volumes
docker-compose.yml
fileport mapping syntax
ports:
- "<hostPort>:<containerPort>"
A simple dockerfile
# Use the official Python image
FROM python:3.12
# Set the working directory. It means project root directory
WORKDIR /websys
# This command put requirements.txt in container in specific directory. Requirements library will be install in docker container
COPY ../requirements.txt /websys/requirements.txt
# This command will install dependancies in docker container
RUN pip install --no-cache-dir --upgrade -r /websys/requirements.txt
# copy source code files and directory in docker container
COPY ./app /websys/app
# this is default command. It will run after container start
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
nginx.config file
events {}
http {
upstream websys_upstream {
server websyscontainer:8000; # for development on local machine
}
server {
listen 90;
server_name localhost;
location / {
proxy_pass http://websys_upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
docker-compose.yml file
services:
websys:
build:
context: . # directory where docker file created. Here dot(.) used for root directory
dockerfile: dockerfiles/Dockerfile.dev
image: websys:latest
container_name: websyscontainer
ports:
- "8081:8000" # Maps host machine port 8081 with container port 8000. Here port map as <hostport>:<containerport>
env_file:
- .env # Load all environment variables from the .env file
environment:
- ENV=$ENV # Explicitly defines the ENV variable from the .env file
volumes:
- .:/websys # Bind-mounted local directory for live updates. but it will be not use in production
networks:
- mywebsysnetwork # Connects to your custom network
restart: unless-stopped # better for development. better for debuging. If you want to stop container manually then it will not again start automatically. It will start automatically when system reboot
nginx: # Service for the Nginx web server
image: nginx:stable # Uses the stable version of the official Nginx image
container_name: nginxcontainer # Custom name for the container
ports:
- "8082:90" # Maps host machine port 8082 with container port 90. Here port map as <hostport>:<containerport>
volumes:
- ./nginx-dev.conf:/etc/nginx/nginx.conf:ro # Mounts the custom Nginx configuration file in read-only mode. It is bind mount. It is not persistent valume
depends_on:
- websys # Ensures websys service starts before Nginx
networks:
- mywebsysnetwork # Connects to your custom network
restart: always # Automatically restarts the container if it stops or after a host machine reboot
networks:
mywebsysnetwork: # Ensure this name matches all other references
driver: bridge
name: websysnetwork # Explicitly name defined of network
nginx.config
file will be run in nginxcontainer
container. In nginx.config file we are using port 90. You can see listen 90;
localhost:8082
from browser then request will go on 8082 port in host machine. Now, docker will forward this request on 90 port in nginxcontainer
container because in docker-compose.yml file port mapping is 8082:90
. In nginxcontainer
container nginx server is running that has listening port is 90. Now, nginx server is sending request by proxy on 8000
port in websyscontainer
container because in nginx.conf
file we are using proxy_pass http://websys_upstream;
and upstream websys_upstream { server websyscontainer:8000; }
. Our uvicorn server running on 8000 port in websyscontainer
container because we are using CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
in Dockerfile. Now, our API will get request and send response to browser because uvicorn server running on 8000 port in websyscontainer
container. The nginx server used for production because here you will use your domain name like exampledomain.com
instead of localhost.localhost:8081
from browser then request will to on 8081 port in host machine. Now, docker will forward this request directly on 8000 port in websyscontainer
because in docker-compose.yml file port mapping is 8081:8000
. Here 8081 port direct map with 8000 port of websyscontainer
container. Now, our API will get request and send response to browser because uvicorn server running on 8000 port in websyscontainer
container. The 8081:8000
port worked with localhost and it is used for development and debuging. The localhost
used for 127.0.0.1
IP by OS. Therefore localhost
directly send request and get response by browser because uvicorn server running on 8000 port in websyscontainer
container.localhost:8000
for development then you should map 8000 for hostPort like 8000:8000
in port in websyscontainer
container in docker-compose.yml
file.localhost:80
means you want to use localhost
means you want to use exampledomain.com
production then you should map 80 for hostPort like 80:90
in port in nginxcontainer
container in docker-compose.yml
filenginxcontainer
container then you should map 80 for containerPort like 80:80
in nginxcontainer
in docker-compose.yml
file. And, you will use listen 80;
in nginx.config file.Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.
Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.
comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.
comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.
copyright © 2023