5. Run

5.1. Basic usage

  • Check hostname

  • Check PS1 (bash prompt)

  • Will pull automatically

  • Will close immediately after executing command

$ docker run bash
$ docker run bash echo 'hello'
hello

5.2. Interactive mode

  • -t, --tty - Allocate a pseudo-TTY

  • -i, --interactive - Keep STDIN open even if not attached

  • -it - implies both -i and -t

  • ctrl + p + q - quit container without stopping it

  • ctrl + d - exits and stops the container

$ docker run -it bash
$ docker run -it alpine sh

5.3. Detach and attach

5.3.1. Detach

  • -d, --detach - Run container in background and print container ID

$ docker run -d -it alpine sh
b7583714a497ac10fcfa2f514025dc271a9e0d4540684f26f115d5a98b2f87b7

$ docker run --detach -it alpine sh
09f99d54cba4162ebea238766d366fe09ad831ca9cc844c1b54f3151dd8aec3b

5.3.2. Attach

  • Attach to local standard input, output, and error streams of main process

  • ctrl + p + q - quit container without stopping it

$ docker attach CONTAINER_NAME_OR_ID

5.4. Show containers

5.4.1. Show running

$ docker ps

5.4.2. Show all containers

  • even not running

$ docker ps -a

5.5. Name

  • --name - Assign a name to the container

$ docker run -d --name sleeper alpine sleep 50
b9e2e75cb7727cc43c6daff677b69d2fcae9077717c069190ab7bb3329339c4a

$ docker ps
CONTAINER ID        IMAGE     COMMAND      CREATED              STATUS              PORTS   NAMES
b9e2e75cb772        alpine    "sleep 50"   5 seconds ago        Up 4 seconds                sleeper
09f99d54cba4        alpine    "sh"         About a minute ago   Up About a minute           serene_kare
b7583714a497        alpine    "sh"         About a minute ago   Up About a minute           cocky_curie

5.6. Environmental variables

5.6.1. Env

  • -e, --env - Set environment variables

$ docker run -e MY_NAME='Jan Twardowski' alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=f5f93be44865
MY_NAME=Jan Twardowski
HOME=/root

5.6.2. Env-file

  • --env-file - Read in a file of environment variables

  • .env name convention

  • Add .env to .gitignore

  • .env-sample in your repository

Listing 5.1. Contents of .env file
DATABASE_ENGINE=sqlite3
DATABASE_HOST=localhost
DATABASE_PORT=1337
DATABASE_NAME=/tmp/db.sqlite3
DATABASE_USER=root
DATABASE_PASSWORD=mypassword
$ docker run --env-file=.env alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=bb04daae4875
HOME=/root
DATABASE_ENGINE=sqlite3
DATABASE_HOST=localhost
DATABASE_PORT=1337
DATABASE_NAME=/tmp/db.sqlite3
DATABASE_USER=root
DATABASE_PASSWORD=mypassword

5.7. Expose ports

  • -p, --publish - Publish a container’s port(s) to the host

  • -P, --publish-all - Publish all exposed ports to random ports

$ docker run -p 5432:5432 postgres
$ docker run -p 192.168.56.101:5432:5432 postgres

5.9. Assignments

5.9.1. Ehlo World

  1. Zainstaluj Docker

  2. Czym różni się Docker od Vagrant?

  3. Wyświetl Ehlo World! z wnętrza kontenera Docker

  4. Wyświetl listę działających kontenerów Docker