# Docker Notes
Table of Contents
Hi there!
This is yet another note from me ^^
This is for my notes about Docker. I’ve been dealing with container technologies for years, it’s a good habit to dump all of my notes here.
I hope you find this useful as well.
Build Docker Image
Method 1: Docker build
Using dockerfile is the formal way to build a docker image.
We can define the base image to pull from, copy files inside it, run configuration, specify what process to start with.
You know I like using Django for projects, here comes a dockerfile from Cookiecutter:
# define an alias for the specific python version used in this file.FROM python:3.11.6-slim-bullseye as python
# Python build stageFROM python as python-build-stage
ARG BUILD_ENVIRONMENT=local
# Install apt packagesRUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ # psycopg2 dependencies libpq-dev
# Requirements are installed here to ensure they will be cached.COPY ./requirements .
# Create Python Dependency and Sub-Dependency Wheels.RUN pip wheel --wheel-dir /usr/src/app/wheels \ -r ${BUILD_ENVIRONMENT}.txt
# Python 'run' stageFROM python as python-run-stage
ARG BUILD_ENVIRONMENT=localARG APP_HOME=/app
ENV PYTHONUNBUFFERED 1ENV PYTHONDONTWRITEBYTECODE 1ENV BUILD_ENV ${BUILD_ENVIRONMENT}
WORKDIR ${APP_HOME}
# devcontainer dependencies and utilsRUN apt-get update && apt-get install --no-install-recommends -y \ sudo git bash-completion ssh vimRUN echo "alias ls='ls -G --color=auto" >> ~/.bashrcRUN echo "alias ll='ls -lh --color=auto" >> ~/.bashrc
# Create devcontainer user and add it to sudoersRUN groupadd --gid 1000 dev-user \ && useradd --uid 1000 --gid dev-user --shell /bin/bash --create-home dev-user \ && echo dev-user ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/dev-user \ && chmod 0440 /etc/sudoers.d/dev-user
# Install required system dependenciesRUN apt-get update && apt-get install --no-install-recommends -y \ # psycopg2 dependencies libpq-dev \ # Translations dependencies gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/*
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction# copy python dependency wheels from python-build-stageCOPY --from=python-build-stage /usr/src/app/wheels /wheels/
# use wheels to install python dependenciesRUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ && rm -rf /wheels/
COPY ./compose/production/django/entrypoint /entrypointRUN sed -i 's/\r$//g' /entrypointRUN chmod +x /entrypoint
COPY ./compose/local/django/start /startRUN sed -i 's/\r$//g' /startRUN chmod +x /start
COPY ./compose/local/django/celery/worker/start /start-celeryworkerRUN sed -i 's/\r$//g' /start-celeryworkerRUN chmod +x /start-celeryworker
COPY ./compose/local/django/celery/beat/start /start-celerybeatRUN sed -i 's/\r$//g' /start-celerybeatRUN chmod +x /start-celerybeat
COPY ./compose/local/django/celery/flower/start /start-flowerRUN sed -i 's/\r$//g' /start-flowerRUN chmod +x /start-flower
# copy application code to WORKDIRCOPY . ${APP_HOME}
ENTRYPOINT ["/entrypoint"]Method 2: Docker commit
Another way is to use docker commit <container_id> <new_image_name>, it will create a new image based on your existing image in you docker local storage.
Export/Import
After we have docker images, we usually want to share it with other or transfer to another places, that’s where export/import are used:
docker save <image_name:version> > exported_file.tardocker load < exported_file.tarDocker Registry
Environment: CentOS 7.2
Setup Docker repository
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'[dockerrepo]name=Docker Repositorybaseurl=https://yum.dockerproject.org/repo/main/centos/7/enabled=1gpgcheck=1gpgkey=https://yum.dockerproject.org/gpgEOFInstall and enable docker-registry
yum install docker-registrysystemctl enable docker-registry.serviceservice docker-registry startVerify docker-registry service
-
Using curl to check
curl localhost:5000You should get results:"\"docker-registry server\"" -
systemctl status docker-registry
Configure storage_path
Update local storage path to your specific location in /etc/docker-registry.yml:
local: &local <<: *common storage: local storage_path: _env:STORAGE_PATH:/data/docker/docker-registryThen restart: systemctl restart docker-registry.service
Setup client to use the registry
Update /etc/sysconfig/docker to add --insecure-registry your_ip_or_hostname:5000 as below:
# Modify these options if you want to change the way the docker daemon runsOPTIONS='--insecure-registry your_ip_or_hostname:5000 --selinux-enabled --log-driver=journald'DOCKER_CERT_PATH=/etc/dockerPush to the registry
In order to have some images to push to the registry, let’s pull from docker.io firstly: docker pull centos
Please write down the IMAGE ID for the centos image
If you push it to your own registry now, you will get error as blow:
# docker push your_ip_or_hostname:5000/ciThe push refers to a repository [your_ip_or_hostname:5000/ci]An image does not exist locally with the tag: your_ip_or_hostname:5000/ciSo you need to create a repo on your private registry then try to push again.
To do that, you can tag a repo on your private registry and push:
# docker tag the_centos_image_id_you_wrote_down your_ip_or_hostname:5000/centos[root@geekcoding101 ~]# docker push your_ip_or_hostname:5000/centosThe push refers to a repository [your_ip_or_hostname:5000/centos]97ca462ad9cc: Image successfully pushedPushing tag for rev [the_centos_image_id_you_wrote_down] on {http://your_ip_or_hostname:5000/v1/repositories/centos/tags/latest}[root@geekcoding101 ~]#Docker Storage
Where does docker store images?
Usually is /var/lib/docker/.
But vary depending on the driver Docker is using for storage.
You can manually set the storage driver with the -s or —storage-driver= option to the Docker daemon.
-
/var/lib/docker/{driver-name}will contain the driver specific storage for contents of the images. -
/var/lib/docker/graph/<id>now only contains metadata about the image, in the json and layersize files.
In the case of aufs:
-
/var/lib/docker/aufs/diff/<id>has the file contents of the images. -
/var/lib/docker/repositories-aufsis a JSON file containing local image information. This can be viewed with the command docker images
Cheat Sheet
| Command |
|---|
docker version |
docker info |
docker images |
docker rmi <image name> |
docker run -t -i centos |
docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done" |
docker stop <container name> |
docker inspect <container name> |
docker tag <tag of container> |
docker logs <container name> |
Okay, that’s all from me. Thank you for reading!