====== Docker quick ====== ===== - Volumes ===== * show https://stackoverflow.com/questions/30133664/how-do-you-list-volumes-in-docker-containers ===== - Run container ===== ==== - Host network ==== Create and start the container as a detached process. The --rm option means to remove the container once it exits/stops. The -d flag means to start the container detached (in the background). Host network --network host, bind container port to host port. docker run --rm -d --network host --name my_nginx nginx ==== - Run docker with custom entrypoint ==== Example docker file: https://github.com/yibo-cai/compass-db/raw/arm64/Dockerfile We want to override the default entrypoint which starts mysql as CMD. First, start the image: docker run --entrypoint /sbin/entrypoint.sh -it opnfv/compass-db ==== - Attach to the running image ==== Not using attach b/c it allows only one tty, so: docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 002af509471f opnfv/compass-db "/sbin/entrypoint.sh" 2 minutes ago Up 2 minutes 3306/tcp condescending_bartik docker exec -it -u root 002af509471f /bin/bash ==== - See concole log ==== sudo docker logs e537fb5d99f7 **Note:** * entrypoint arguments come after image name: docker run --entrypoint "/bin/ls" debian -al /root * Entrypoint vs CMD: https://medium.freecodecamp.org/docker-entrypoint-cmd-dockerfile-best-practices-abc591c30e21 * Docker start script: https://forums.docker.com/t/best-practice-to-run-a-bash-setup-script-after-creation-of-container/28988 * mysql entrypoint: https://stackoverflow.com/questions/25920029/setting-up-mysql-and-importing-dump-within-dockerfile ===== - Remove unuse containers ===== * https://linuxize.com/post/how-to-remove-docker-images-containers-volumes-and-networks/ Remove everything docker system prune docker container ls -a docker container rm cc3f2ff51cab cd20b396a061 docker rm -v $(docker ps -a -q -f status=exited) docker rmi $(docker images -f "dangling=true" -q) docker volume rm $(docker volume ls -qf dangling=true) docker-compose -f docker-compose.yml rm ===== - Build docker ===== Build (.) is context folder, can also be git url: docker build -f path/to/Dockerfile . Build and tag docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 . ====== - Docker-compose ====== cd dir/to/docker-compose.yml docker-compose up docker-compose down ===== - Environment variables ===== **.env and environment are two things** * .env. The docker-compose command will look for a file named .env in the directory you run the command. The docker-compose.yml can use variable defined in the .env file e.g., ${KEY}. The file must be named .env. * Passing environment variables to **docker container** is possible with the top-level 'environment:', 'env-file:' in service definition or --env-file in command line. By passing a file as an argument, you can store it anywhere and name it appropriately using the --env-file option: docker-compose --env-file ./config/.env.dev up .env file format: # MongoDB variables MONGO_DB_PORT=27017 MONGO_DB_HOST_PORT=30017 MONGO_DB_VERSION=3.6 # Mosquitto MOSQUITTO_USERNAME=mqtt --> OK MOSQUITTO_PASSWORD='mqtt' --> '' included. everything after = --> IGNORED # Kura variables ARTEMIS_PORT=1883 KURA_PORT=8080 # blah --> ERROR ====== - Cheat sheet ====== * cheatsheet: https://gist.github.com/tedivm/a4ec30066890e43982d888eab1ff8632 ## Build docker image ``` $ cd /path/to/Dockerfile $ sudo docker build . ``` View running processes ``` $ sudo docker ps ``` View all processes ``` $ sudo docker ps -a ``` Run an image in a new container daemonized ``` $ sudo docker run -d ``` Run an image in interactive mode with the command `/bin/bash` ``` $ sudo docker run -i -t /bin/bash ``` Run an image in interactive mode with the command `/bin/bash` and link the ports. ``` $ sudo docker run -i -t --link : /bin/bash ``` Run an image with an ENTRYPOINT command in interactive mode with the command `/bin/bash` ``` $ sudo docker run --entrypoint /bin/bash -i -t ``` Run an image in interactive mode with the command `/bin/bash` mounting the host director `/var/app/current` to the container directory `/usr/src/app` ``` $ sudo docker run -i -t -v /var/app/current:/usr/src/app/ /bin/bash ``` Run an image in interactive mode with the command `/bin/bash` setting the environments variables `FOO` and `BAR` ``` $ sudo docker run -i -t -e FOO=foo -e BAR=bar /bin/bash ```