GFFG
GFFG
@ yasirrehman1
Docker – A
Comprehensive Guide
TECH: DOCKER LEVEL: BEGINNERS TO ADVANCED
Docker is a powerful platform that simplifies the
creation, deployment, and management of
applications within lightweight, portable
containers.
Dockerfile directives:
FROM: Specifies the base image to use for the
Docker image being built. It defines the starting
point for the image and can be any valid image
available on Docker Hub or a private registry.
ENV: Sets environment variables within the image.
These variables are accessible during the build
process and when the container is running.
COPY or ADD: Copies files and directories from the
build context (the directory where the Dockerfile is
located) into the image. COPY is generally preferred
for simple file copying, while ADD supports
additional features such as unpacking archives.
RUN: Executes commands during the build process.
You can use RUN to install dependencies, run
scripts, or perform any other necessary tasks.
EXPOSE: Informs Docker that the container will listen
on the specified network ports at runtime. It does
not publish the ports to the host machine or make
the container accessible from outside.
CMD or ENTRYPOINT: Specifies the command to run
when a container is started from the image. CMD
provides default arguments that can be overridden,
while ENTRYPOINT specifies a command that
cannot be overridden.
WORKDIR: Sets the working directory for any
subsequent RUN, CMD, ENTRYPOINT, COPY, or ADD
instructions.
STOPSIGNAL: Sets a custom signal that will be used to
stop the container process.
HEALTHCHECK: Sets a command that will be used by
the Docker daemon to check whether the container
is healthy.
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /app/publish .
Delete an image:
docker rmi <IMAGE>
docker image rm <IMAGE>
docker image rm -f <IMAGE>
Filesystem storage:
▪ Data is stored in the form of regular files on the
host disk
▪ Efficient use of memory
▪ Inefficient with write-heavy workloads
▪ Used by overlay2
Block storage:
▪ Stores data in blocks using special block storage
devices
▪ Efficient with write-heavy workloads
▪ Used by btrfs and zfs
Object storage:
▪ Stores data in an external object-based store
▪ Applications must be designed to use object-
based storage
▪ Flexible and scalable.
2.6 Docker Volumes
There are two different types of data mounts on
Docker:
--mount syntax
Bind mount:
docker run --mount source=/opt/data,destination=/tmp nginx
Named volume:
docker run --mount source=my-vol,destination=/tmp nginx
We can mount the same volume to multiple
containers, allowing them to share data.
We can also create and manage volumes by
ourselves without running a container.
`openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem -subj
"/C=US/ST=Texas/L=Keller/O=Linux Academy/OU=Content/CN=$HOSTNAME" openssl genrsa -out server-
key.pem 4096 `
`openssl req -subj "/CN=$HOSTNAME" -sha256 -new -key server-key.pem -out server.csr \ echo subjectAltName =
DNS:$HOSTNAME,IP:,IP:127.0.0.1 >> extfile.cnf `
`openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-
cert.pem -extfile extfile.cnf`
Generate client certificates:
`openssl genrsa -out key.pem 4096
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem \
-CAcreateserial -out cert.pem -extfile extfile-client.cnf
{
"tlsverify": true,
"tlscacert": "/home/user/ca.pem",
"tlscert": "/home/user/server-cert.pem",
"tlskey": "/home/user/server-key.pem"
}
Edit the Docker service file, look for the line that
begins with ExecStart and change the -H.
sudo vi /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H=0.0.0.0:2376 --
containerd=/run/containerd/containerd.sock