My Wiki!

Docker quick

1. Volumes

2. Run container

2.1 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

2.2 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

2.3 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

2.4 See concole log

  sudo docker logs e537fb5d99f7
  

Note:

3. Remove unuse containers

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

4. 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
  

1. 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

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 <image_name>

Run an image in interactive mode with the command /bin/bash

$ sudo docker run -i -t <image_name> /bin/bash

Run an image in interactive mode with the command /bin/bash and link the ports.

$ sudo docker run -i -t --link <docker_container_name>:<docker_container_alias> <image_name> /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 <image_name>

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/ <image_name> /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 <image_name> /bin/bash

Navigation