Docker Command Line Reference

Docker Commands

Get Information about Docker Environment
docker info
Show Docker Images built and downloaded
docker images
Download Built Image from Repository
docker pull ubuntu
List Running Containers
docker ps
List All Containers
docker ps -a
Start Container from a Built Image
docker run ubuntu
Start Container with Interactive Shell
docker run -i -t ubuntu /bin/bash Detach from container but keep it running: ctrl+p ctrl+q
Start a Stopped Container
docker start <JOB>
Stop a Running Container
docker stop <JOB>
Restart Container
docker restart <JOB>
Kill a Running Container
docker kill <JOB>
Commit a Container Container
docker commit <container> <some_name>

Docker Run

Run a docker image

Run a docker image, interactively with a tty
docker run -i -t ubuntu /bin/bash -i interactive -t tty

Detach with ctrl+p ctrl+q

Bind docker to listen on address/socket
Get Container Reference
JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)


Starting a MySQL Docker Container with a name and pre-set password docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Remotely link to the MySQL Container docker run --name some-app --link some-mysql:mysql -d application-that-uses-mysql

Connect to MySQL via MySQL Client: docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

Access Container docker exec -it some-mysql bash

The MySQL Server log is available through Docker’s container log: docker logs some-mysql

Customising MySQL

Customise through the MySQL Command Line:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
Use a custom MySQL.cnf
docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Container Initilisation When the container is started any .sh and .sql files in /docker-entrypoint-initdb.d are executed automtically.

Data Storage

Mount a local disk docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Docker Flow

Create a Dockerfile
vi Dockerfile
Build the Image, from the current directory
docker build . docker build –label tex –tag tex .
Run the image

docker run –name tex -it

docker exec -it POSTest bash

Docker Compose

Docker compose is build collections of Docker containers.

Docker compose mult be installed (it comes bundles in the docker toolbox on OSX)

From within the Project folder, containing a docker-compose.yml file, run: docker-compose up -d

docker-compose up -d Start container collection, and detach
docker-compose up Start containers, and view logs
docker-compose down Shutdown containers, destroy data
View attached volumes
 $ docker volume ls
 DRIVER              VOLUME NAME
 local               mypos_app_log
 local               mypos_db_data
 local               mypos_db_init
 local               mypos_dblog
 local               mypos_mypos
 local               mypos_oslog

Inspect an attached volume for details:

Inspect attached volume
docker volume inspect mypos_db_data

        "Name": "mypos_db_data",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/mypos_db_data/_data",
        "Labels": null,
        "Scope": "local"

The location of files on OSX isn’t clear, use absolute paths and not mount points.