Commands

Create Docker Image, Run inside Container


/* 
build: Build a Docker image from a Dockerfile, stores in a local registry on your system
-t:     name and tag of docker file
--build-arg="BASE_IMAGE=arm64v8/ubuntu:18.04" Ubuntu 18.04 image for the ARM64 architecture.
*/
$ docker build . -f Dockerfile -t test_image

/* View docker image stored in a local registry on your system */
$ docker image ls

/* Save docker image in current working directory */
$ docker save -o docker-image newsreader-cli-test

$ docker image rmi docker-image

/* 
Run Image Inside Container
    --volume :[:options]
            Mount a directory from the host system into the container. 
            For example 
                On host there are some files inside /home/amit, which i want to access inside container
                I can mount using /home/amit:/test
                All files in /home/amit would be present inside /test in container.
    --user 1001. user ID inside the container.
    test-docker-image. Image name from which container is created
    cmd. Command executed inside container when its created
*/    
$ docker run --volume /home/amit:/test --user <uid of user> test-docker-image cmd

// Run commands present inside test.sh inside docker container
$ docker run --platform linux/amd64 -i newsreader-cli-test < ~/test.sh
            

Run Interactive session inside Container

            
/* 
Run session inside Container
    -i interactive. Keep STDIN open even if not attached. allows you to interact with the container.
    -t Allocate a pseudo-TTY. Helps to run an interactive shell session in the container.
    --volume: explained above
    --entrypoint: 
        Overrides the default entry point of the Docker image
        /bin/bash, container should start with the /bin/bash shell instead of the default entry point defined in the image.
    brunneis/python:3.9.0-ubuntu: specific Docker image
        brunneis: This is the username or organization on Docker Hub that owns the repository.
        python: This is the name of the repository.
        3.9.0-ubuntu: Tag for the image, indicating it contains Python 3.9.0 based on an Ubuntu operating system.
    -c CPU shares (relative weight)

Overall
    Run Docker image(brunneis/python:3.9.0-ubuntu) inside container at /bin/bash
    Mounts the /home/amit directory from the host to /test
    Run command -c 'ls -ltr'
*/
$ docker run -it --volume /home/amit:/test --entrypoint /bin/bash brunneis/python:3.9.0-ubuntu -c 'ls -ltr'
        

Running arm binaries on x86

1. Enable multiple platform builds on docker. Create arm build on docker

/*
Run arm compiled binary on x86
    --privileged:
        flag gives the container extended privileges
        When run with --privileged flag, container gains access to all the host's devices and can 
        perform tasks that are otherwise restricted for security reasons. Eg:
            Change kernel parameters
            Access and manipulate system devices
            Perform other operations typically limited to root access on the host
    --rm:
        Automatically removes the container once it exits, to keep system clean.
    tonistiigi/binfmt:  Docker image available on Docker Hub
        tonistiigi: This is the username or organization on Docker Hub that owns the repository.
        binfmt: This is the name of the repository. Register new binary formats in the kernel,
                allowing the host system to execute binaries of different architectures (e.g., ARM on x86_64)
    --install all: tells binfmt to install support for all available binary formats

// docker run: This command runs a new container.
// --privileged: gives the container extended privileges
// --rm: container is automatically removed after it exits.
// tonistiigi/binfmt: is a pre-built docker image that contains the necessary tools to install binary format handlers
// install support for all available binary formats.
*/
$ docker run --privileged --rm tonistiigi/binfmt --install all            
        
2. docker build. Create docker image from docker file

/*
BASE_IMAGE: placeholder used in the Dockerfile to specify the base image for the build process
    = arm64v8/ubuntu:18.04  //dynamically change the base image without modifying the Dockerfile.
*/
$ docker build --build-arg="BASE_IMAGE=arm64v8/ubuntu:18.04" . -t tag1
        
3. Run make command for ARM env inside docker container

$ docker run --volume test:/checker --user $(shell id -u) tag2 make BUILD_DIR=build-docker-arm TARGET_ARM=1 -C /checker/cpu clean all
        

Other Commands


//Login
$ docker login repository

//Download docker image locally on system
$ docker image pull artifactory/path:image-tag

Loading Docker Image

        Loading means untar the docker image and load inside docker engine, still image is not running. To run the 
        image still we need to do "docker run"
    

        // docker desktop should be running

        cmd> docker load -i fragmuffin-pygcode_oci.tar.gz
        Loaded image: fragmuffin-pygcode:latest

        // List
        cmd> docker images
        REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
        fragmuffin-pygcode    latest    d0da2aa0fd41   13 hours ago   87.3MB
        newsreader-cli-test   latest    59c21a173ad7   21 hours ago   579MB

        // Inspect OS of docker image
        > docker inspect --format='{{.Os}}/{{.Architecture}}' 59c21a173ad7
        'linux/amd64'

        // Unload
        cmd> docker image rmi fragmuffin-pygcode
        Untagged: fragmuffin-pygcode:latest
        Deleted: sha256:d0da2aa0fd41ee2604c74a93748e85452b3fa1d665df61f4dfc62f077f9131a6