The meaning of life is to explore the world

Docker basics

Posted on By Jason Liu

Commands

  1. Official Docker reference documentation
    https://docs.docker.com/reference/

  2. Docker registry commands
     docker pull ubuntu
     docker push jasonmccb/drives_bash_sync
    
  3. Docker image commands
     # Listing
     docker images
    
     # Removing
     docker rmi <image-name>
     docker rmi $(docker images -q -f dangling=true)
    
     # Building using "docker build"
     docker build -t <image name[:image tag]> <dockerfile dir>
     # -f option
     docker build -t image-name -f folder_name/Dockerfile .
     # Building using "docker-compose" ("up" will build/create and run)
     docker-compose up -d
     # To rebuild after changed
     docker-compose build
    
  4. Docker container commands
     # Listing
     docker ps -a
     # Show all details
     docker inspect <container id>
     # Only show bind mounts
     docker inspect -f "{ { .Mounts }}" <container id>
    	
     # Running a new container
     docker run <image name>
     # -d runs from background, -it runs interactively, -v mounts volume, -w for working dir
     docker run -v "D:/programming/Docker container/sharing:/root" -w /root -it my-wsl-sync
     # Running from docker-compose, the "app" service in a shell
     docker-compose exec app sh
    	
     # Managing existing container
     docker start/stop/kill/rm <container id>
    	
     # Checking log
     docker logs <container id>
    

Issues

  1. Cannot find package
    docker run ... -c "yarn install && yarn run env"
    ...
    yarn run v1.22.4
    error Couldn't find a package.json file in "/app"
    

    Solution: cd "xxxx/app"

  2. Executable file not found in $PATH
    docker run -it ubuntu -v "D:/programming/Docker container/sharing:/root"
    docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-v\": executable file not found in $PATH": unknown.
    

    Solution: docker run -v "D:/programming/Docker container/sharing:/root" -it ubuntu

  3. COPY failed
    Dockerfile: COPY ../sharing/myWSLSync.sh /tmp/
    docker build -t my-wsl-sync folder_name
    Step 2/2 : COPY sharing/myWSLSync.sh /tmp/
    COPY failed: stat /var/lib/docker/tmp/docker-builder764373298/sharing/myWSLSync.sh: no such file or directory
    

    Solution: don’t use “..”, and use “-f”
    docker build -t my-wsl-sync -f folder_name/Dockerfile .

  4. Warning on non-Windows host
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    

    Solution: Because folders will become files, we should not copy folders from Windows to Linux image, we should only copy files. And also our program should not rely on the permissions of files and folders.

Example

I’ve written an example of Bash program in Docker:
https://github.com/jasonmccb/drives_bash_sync