Created
January 18, 2022 06:33
-
-
Save spapas/4df88ef50b2d4279eab2e3116c958a68 to your computer and use it in GitHub Desktop.
docker.notes.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Docker-notes | |
# Test docker installation | |
docker ps | |
# and run a test container | |
docker run hello-world | |
# and stop it with | |
# docker stop hello-world | |
# See docker images | |
docker images | |
# See running docker procs | |
docker ps | |
# See all docker procs | |
docker ps -a | |
# Delete exited containers | |
docker rm $(docker ps -a -q -f status=exited) | |
# or | |
docker container prune | |
# Run command on a docker container | |
docker run spapas/foodrec ls | |
# Add -it for interactive and pseudo tty: | |
docker run -it spapas/foodrec bash | |
# Docker run server | |
docker run -d -P --name static-site prakhar1989/static-site | |
# --name: Give a name to the container | |
# -d: Detach from terminal | |
# -P: Export ports randomly. Use docker port static-site | |
# Dockerfile | |
# We start with specifying our base image. Use the FROM keyword to do that - | |
FROM python:3 | |
# The next step usually is to write the commands of copying the files and installing the dependencies. | |
# First, we set a working directory and then copy all the files for our app. | |
# set a directory for the app | |
WORKDIR /usr/src/app | |
# copy all the files to the container | |
COPY . . | |
# Now, that we have the files, we can install the dependencies. | |
# install dependencies | |
RUN pip install --no-cache-dir -r requirements.txt | |
# set env vars | |
ENV DJANGO_SETTINGS_MODULE=foodrec.settings.prod | |
# The next thing we need to specify is the port number that needs to be exposed. | |
# Since our flask app is running on port 5000, that's what we'll indicate. | |
EXPOSE 5000 | |
# The last step is to write the command for running the application, | |
# which is simply - python ./app.py. We use the CMD command to do that - | |
CMD ["python", "./app.py"] | |
# The primary purpose of CMD is to tell the container which command it should run when it is started. | |
# Build an image | |
# Use | |
docker build -t spapas/foodrec . | |
# and then | |
docker run -p 8888:8000 spapas/foodrec | |
# (expose container port 5000 to local 8888) | |
# Volumes and bind mounts | |
# Volumes | |
# Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. | |
# While bind mounts are dependent on the directory structure and OS of the host machine, volumes are | |
# completely managed by Docker: https://docs.docker.com/storage/volumes/ | |
C:\progr\py3\foodrec\foodrec>docker run -it --mount source=testvol,target=/bar spapas/foodrec ls -l /bar | |
total 0 | |
C:\progr\py3\foodrec\foodrec>docker run -it --mount source=testvol,target=/bar spapas/foodrec bash | |
root@3f6fa9a1d3f6:/app# ls -l /bar | |
total 0 | |
root@3f6fa9a1d3f6:/app# cd /bar | |
root@3f6fa9a1d3f6:/bar# ls -l | |
total 0 | |
root@3f6fa9a1d3f6:/bar# echo x > y | |
root@3f6fa9a1d3f6:/bar# ls -l | |
total 4 | |
-rw-r--r-- 1 root root 2 Nov 15 10:09 y | |
root@3f6fa9a1d3f6:/bar# exit | |
C:\progr\py3\foodrec\foodrec>docker run -it --mount source=testvol,target=/bar spapas/foodrec ls -l /bar | |
total 4 | |
-rw-r--r-- 1 root root 2 Nov 15 10:09 y | |
# File is saved. If you don't use a volume on the other hand: | |
(venv) C:\progr\py3\foodrec\foodrec>docker run -it --mount source=testvol,target=/bar spapas/foodrec bash | |
root@26120bfba417:/app# cd / | |
root@26120bfba417:/# mkdir foo | |
root@26120bfba417:/# cd foo | |
root@26120bfba417:/foo# ls | |
root@26120bfba417:/foo# echo z > b | |
root@26120bfba417:/foo# exit | |
(venv) C:\progr\py3\foodrec\foodrec>docker run -it --mount source=testvol,target=/bar spapas/foodrec ls -l /foo | |
ls: cannot access '/foo': No such file or directory | |
# the file is lost ! | |
# Inspect a volume: | |
# docker volume inspect testvol | |
[ | |
{ | |
"CreatedAt": "2021-11-15T10:09:25Z", | |
"Driver": "local", | |
"Labels": null, | |
"Mountpoint": "/var/lib/docker/volumes/testvol/_data", | |
"Name": "testvol", | |
"Options": null, | |
"Scope": "local" | |
} | |
] | |
# And the actual volume data is saved at: \\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes (on windows) | |
# Bind mounts | |
# https://docs.docker.com/storage/bind-mounts/ | |
# Mount a local folder to docker: | |
C:\progr\py3\foodrec\foodrec>dir foo | |
Volume in drive C is Windows | |
Volume Serial Number is 10D2-719C | |
Directory of C:\progr\py3\foodrec\foodrec\foo | |
15/11/2021 12:03 <DIR> . | |
15/11/2021 12:03 <DIR> .. | |
15/11/2021 12:03 0 zz.txt | |
1 File(s) 0 bytes | |
# docker run -it --mount type=bind,source=c:/progr/py3/foodrec/foodrec/foo,target=/bar spapas/foodrec ls -l /bar | |
total 0 | |
-rwxrwxrwx 1 root root 0 Nov 15 10:03 zz.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment