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
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
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
After running you will see.
root@b57dce611822:/#
b57dce611822
is the container ID. And root
is the user of container.command: exit
root@b57dce611822:/# exit
then you will see again
atul@atul-Lenovo-G570:~$
atul@atul-Lenovo-G570:~$ docker volume ls
command: $ docker rm volume <volume name>
atul@atul-Lenovo-G570:~$ docker rm volume greenbook_postgresdata
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