Thursday, 14 October 2021

Docker

run docker image

docker image ls
REPOSITORY      TAG            IMAGE ID       CREATED          SIZE
java            8-jdk-alpine   3fd9dd82815c   4 years ago      145MB

//start a container using image
docker run -i -t 3fd9dd82815c /bin/sh

//find if www-data group exists in alpine:3.14
docker run --rm -it alpine:3.14 cat /etc/group | grep www-data

//find php version of this linux box
docker run --rm -it docker-php7-alpine:1.3.3 php -v

//link another container
docker run --link docker_mysql_1:sql-master.local.buzz.io

//add entry in host file of docker container
docker run --add-host sql-master.sandbox.buzz.io:250.48.4.218


about -p (or --publish) flag

docker run -p [host port]:[container port]

The -p flag in docker run is used to publish a container's port(s) to the host. It allows external access to the container's network ports. For example, if a container is running a web server on port 80 and you want to access it from your host machine on port 8080, you would use the command docker run -p 8080:80 [image name]. This would map port 80 of the container to port 8080 on the host machine.

delete all stopped container

docker container prune

Start a stopped container

//tweb is container name
docker container start tweb

delete docker image

 docker image rm b9032ca8381a

That number is image id. get by run docker image ls

go into docker container

docker exec -it container_name bash | ash 

-i -t flag: "interactively" type commands to the pseudo-tty/terminal created by the -t switch.

Some times need to replace /bin/sh with /bin/bash depending on which program container has

see volumes

docker volume ls
//see docker volumes a contained mounted
docker inspect -f '{{ .Mounts }}' container_id

//sample output
[{bind  /home/leo2019/jenkins_home /var/jenkins_home   true rprivate}]
//$PWD is a built in env variable. It is current directory
volumes:
    - $PWD:/var/src:delegated

see log

docker logs container_name

build image

//build image called web:latest. do not forget dot
//it tells docker to use current directory as build context
docker image build -t web:latest .

//--no-cache will not use cached layers
docker-compose build  --no-cache web

see image layer

docker image inspect my_image_name

//the above will show image maintanter. Therefore, by changing
//maintanter and inspect image
//we can be sure it is using the new docker file

push image to docker hub

Assume there is a image in local called web:latest. Want to push to leo_image in docker hub

//re-tag
docker image tag web:latest leo_image/web:latest

//docker login
docker login

//push to docker hub
docker image push leo_image/web:latest

Docker copy

It is very useful. One use case is that we can pre-built some binary instead of building it directly when build image. Then copy the binary to the right destination.

COPY extra_php_extension/grpc.so /usr/lib/php7/modules/.
COPY extra_php_extension/protobuf.so /usr/lib/php7/modules/.

//from alpine base image. The following will build grpc.so and protobuf.so and it will
//take 20 minutes. Instead we can build once and grab grpc.so and protobuf.so. Then use
//copy command to do the copy. It will save lots of build time.
RUN apk --update add php-pear php7-dev g++ make linux-headers php-phar

RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
       && pecl install grpc \
       && pecl install protobuf \
       && apk del .phpize-deps

Trouble shooting

build always failed. After change Dockerfile to dockerfile, it works. what?

docker-compose build web get error: [output clipped, log limit 1MiB reached]

To solve:

COMPOSE_DOCKER_CLI_BUILD=0 DOCKER_BUILDKIT=0 docker-compose build web 

COMPOSE_DOCKER_CLI_BUILD

Configure whether to use the Compose python client for building images or the native docker cli. By default, Compose uses the docker CLI to perform builds, which allows you to use BuildKit to perform builds. Set COMPOSE_DOCKER_CLI_BUILD=0 to disable native builds, and to use the built-in python client.

docker network

My Post for Docker Network

Copy File from container in EC2 to local machine

//go to EC2 which host the container
ssh -i NOC.pem ec2-user@10.123.6.123

//copy file from container to EC2
docker cp ecs-sandbox-container-c8bdbc01:/var/src/composer.lock /tmp/.

//copy file to local
scp -i NOC.pem ec2-user@10.123.6.123:/tmp/composer.lock .

docker info

Also can see how much data space we have. If data space is too low, some tasks can not be done.

docker info
Client:
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.4
    Path:     /Users/leo/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.17.3
    Path:     /Users/leo/.docker/cli-plugins/docker-compose
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.0
    Path:     /Users/leo/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.19
    Path:     /Users/leo/.docker/cli-plugins/docker-extension
....

To free up data space, can delete some unused docker image in the machine.

MySQL container can not start because of no space left on device

One time, I can not start mysql db container because it complained that there was not enough spaces. We can free up spaces by deleted orphaned volumes. Also delete images with <none> to free up spaces. Also consider clear redis cache use flush command below. You may need to restart Docker desktop

//Error
Error response from daemon: mkdir /var/lib/docker/overlay2/d49a2835b41459956e752ceddaaf0671b4e9386fc220e8b9d4697a46ad29a635-init: no space left on device
# see orphaned volumes. These volumes are not 
# referenced by any containers.
docker volume ls -qf dangling=true

#to remove
docker volume rm $(docker volume ls -qf dangling=true)

Can not start redis container

Container started, but exited right away. Before it always worked. But all of sudden, it did not work any more.

root cause is that image is updated. Because we use redis:latest, some how machine downloaded a new version of image and this image does not work for alpine linux. The old image works for alpine linux.

To solve it, we use redis:7-alpine and it works on Alpine linux.

Lesson learned: try not use image with latest tag. If use latest image and suddenly it stops working, it may because of image itself.

Clear a redis cache in Docker

docker exec -it container-name redis-cli FLUSHALL

port is used when start MYSQL container

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306: bind: address already in use

Soulution

//in mac:  sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist 

Download file from AWS Fargate container to local machine

aws ecs execute-command --region us-west-1 --cluster sandbox-ecs-mickdkkdroserv --task  e7b0edkdkdk4ace95052a678b896845    --container sandbox-msdbbdcontanern-1 --command "cat /var/src/utils/test.csv"  --interactive >> ~/leotest.csv

Local MySQL container can not run because of switching between mysl5.7 and mysql8.0 image

  • #stop container
      docker-compose down
  • #find mysql volume
      docker volume ls -qf dangling=true
  • #delete mysql volume. Be careful, all database will be removed.
     #Only do it for local db!!
      docker volume rm docker_mysql
  • #restart container
      docker-compose up -d

Copy file from docker container to host machine

docker cp optimistic_mirzakhani:/var/src/phpunit.xml  ~/Desktop/.

Docker run explained

Usage	docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
//this command is used in Jenkin. Here pwd means to get the current directory
docker run --rm -v `pwd`:/var/src/ -w /var/src php:8.0.26-cli-alpine3.16  vendor/bin/browscap-php browscap:fetch

flag --rm Automatically remove the container and its associated anonymous volumes when it exits

flag -v Bind mount a volume

-w Working directory inside the container

use image php:8.0.26-cli-alpine3.16 to create a container. Mount volume `pwd`:/var/src/. Working directory is /var/src. run commnand: vendor/bin/browscap-php browscap:fetch. After it is done, automatically remove the container and its associated anonymous volumes when it exits

Can not docker-compose up any container in my mac

Look like it is still because of spaces. The build cache is more than 20 GB

# can see build cache is more than 20 GB
docker system df

#clear builder cache
docker builder prune

After delete builder cache, restart Docker Destop. Everything goes normal!

entrypoint in docker-compose.yml

In a docker-compose.yml file, the entrypoint directive specifies the command that will be executed when a container starts. It overrides the default ENTRYPOINT defined in the Docker image's Dockerfile.

version: '3'
services:
    mysite:
        container_name: webpack-cart
        image: node:16.6.1-alpine3.13
        ports:
            - 8092:8092
        hostname: webpack.mysite.com
        volumes:
            - $PWD:/app:delegated
        working_dir: /app
        entrypoint: ['npm', 'run', 'build']
        stdin_open: true
        tty: true
        network_mode: bridge

The command below wll be executed when the container starts

npm run build

This command build is defined in package.json scripts section

 "scripts": {
    "build": "./node_modules/.bin/webpack serve --config webpack.development.config.js --mode development --env APP_VERSION=dev-server --env APPLICATION_ENV=local",
  },

No comments:

Post a Comment