2024-03-19

docker logs

`docker logs , displays the logs of the container.

docker kill vs docker stop

Container doesn’t stop for a SIGTERM signal sent by a stop command, to terminate the process stop follows the SIGTERM with a SIGKILL after a grace period, instead it is faster to use docker kill which terminates the container immediately.

docker search

To search for images within the registery server i.e here Docker Hub we can use the command docker search <container-name> say for example, docker search debian will list out the images with the repository owner and official status.

docker prune

prune command deletes all the unused and stopped containers,unused build cache,unused images from the host system,below command demonstrates the examples of the docker prune command.

docker build

docker build command builds docker images from a Dockerfile, which is a file that contains all the commands a user could call from the terminal.Docker builds images by reading instructions in Dockerfile.

A docker instruction has two components: INSTRUCTION and ARGUMENT.

Docker Instructions

  • FROM: base image we want to start from
  • RUN: run commands during the image build process
  • ENV: sets Environment variable during the build process and also while the container is created.
  • COPY: command copies a a file or folder from the host to the docker image.
  • EXPOSE: to expose a port on the docker image at runtime.
  • ADD: advanced version of COPY command,to copy files,tarballs to the docker image from the host,we can also define instructions to extract the tarball on the docker image.
  • WORKDIR: set the current working directory in the docker image.
  • VOLUME: mount the volume to the docker container.
  • USER: sets the username and UUID when running the container,for setting the non-root user.
  • LABEL: sets metadata information of Docker image.
  • CMD: executes a command within a running container, only one CMD command is allowed,if multiple are present only the last one is takes effect.
  • ENTRYPOINT: commands that will execute when the Docker container starts.

Defining Start conditions for container

Let us consider a sample project where we run yt-dlp against a youtube link which downloads the video, let us build the Dockerfile by giving instructions.

To construct a container image we should first select a base image,here debian:bookworm as a base image to build yt-dlp image, the first line of the Dockerfile will be

FROM debian:bookworm

To get the binary of yt-dlp, we need curl and to run the yt-dlp we need python3 so these two packages has to be installed before running the binary,so the Dockerfile with additions will be

FROM debian:bookworm
RUN apt update -y && apt install curl python3 -y

Fetching the yt-dlp binary from github and copying it to /usr/local/bin and changing the permissions of the binary to executable of all.

FROM debian:bookworm
RUN apt update -y && apt install curl python3
RUN curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
RUN chmod a+x /usr/local/bin/yt-dlp
CMD ["/usr/local/bin/yt-dlp"]

Upon creating the image and creation of container yt-dlp is to be launched.

$ docker run yt-dlp

  Usage: yt-dlp [OPTIONS] URL [URL...]

  yt-dlp: error: You must provide at least one URL.
  Type yt-dlp --help to see a list of all options.
$ docker run yt-dlp https://www.youtube.com/watch?v=uTZSILGTskA

  docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "https://www.youtube.com/watch?v=uTZSILGTskA": stat https://www.youtube.com/watch?v=uTZSILGTskA: no such file or directory: unknown.
  ERRO[0000] error waiting for container: context canceled

According to the above error the youtube link should be taken as an argument to the binary yt-dlp, but here the yt-dlp is executing as a command,updating the CMD TO ENTRYPOINT

FROM ubuntu:22.04

WORKDIR /mydir

RUN apt-get update && apt-get install -y curl python3
RUN curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
RUN chmod a+x /usr/local/bin/yt-dlp

# Replacing CMD with ENTRYPOINT
ENTRYPOINT ["/usr/local/bin/yt-dlp"]
$ docker run yt-dlp https://www.youtube.com/watch?v=DptFY_MszQs
[youtube] Extracting URL: https://www.youtube.com/watch?v=DptFY_MszQs
[youtube] DptFY_MszQs: Downloading webpage
[youtube] DptFY_MszQs: Downloading ios player API JSON
[youtube] DptFY_MszQs: Downloading android player API JSON
[youtube] DptFY_MszQs: Downloading player 9bb09009
[youtube] DptFY_MszQs: Downloading m3u8 information
[info] DptFY_MszQs: Downloading 1 format(s): 22
[download] Destination: Welcome to Kumpula campus! | University of Helsinki [DptFY_MszQs].mp4
[download] 100% of   29.92MiB in 00:00:04 at 7.10MiB/s

Now when the container is run when passed the youtube link as argument the video is downloaded, but it is stored onto the container,here comes the concept of volumes where data is stored.