Working with Docker containers

Introduction to Docker

Tim Sangster

Software Engineer @ DataCamp

Listing containers

repl@host:/# docker ps
CONTAINER ID   IMAGE    .. CREATED         STATUS         ...  NAMES
3b87ec116cb6   postgres    2 seconds ago   Up 1 second    ...  adoring_germain
8a7830bbc787   postgres    3 seconds ago   Up 2 seconds   ...  exciting_heisenberg
fefdf1687b39   postgres    3 seconds ago   Up 2 seconds   ...  vigilant_swanson
b70d549d4611   postgres    4 seconds ago   Up 3 seconds   ...  nostalgic_matsumoto
a66c71c54b92   postgres    4 seconds ago   Up 4 seconds   ...  lucid_matsumoto
8d4f412adc3f   postgres    6 seconds ago   Up 5 seconds   ...  fervent_ramanujan
fd0b3b2a843e   postgres    7 seconds ago   Up 6 seconds   ...  cool_dijkstra
0d1951db81c4   postgres    8 seconds ago   Up 7 seconds   ...  happy_sammet
...
Introduction to Docker

Named containers

docker run --name <container-name> <image-name>
repl@host:/# docker run --name db_pipeline_v1 postgres
repl@host:/# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED  
43aa37614330   postgres   "docker-entrypoint.s…"   About a minute ago
STATUS              PORTS      NAMES
Up About a minute   5432/tcp   db_pipeline_v1
docker stop <container-name>
repl@host:/# docker stop db_pipeline_v1
Introduction to Docker

Filtering running containers

docker ps -f "name=<container-name>"
repl@host:/# docker ps -f "name=db_pipeline_v1"
CONTAINER ID   IMAGE      COMMAND                  CREATED  
43aa37614330   postgres   "docker-entrypoint.s…"   About a minute ago
STATUS              PORTS      NAMES
Up About a minute   5432/tcp   db_pipeline_v1
Introduction to Docker

Container logs

docker logs <container-id>
repl@host:/# docker logs 43aa37614330
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".

PostgreSQL init process complete; ready for start up.

2022-10-24 12:10:40.318 UTC [1] LOG:  database system is ready to accept connect..
Introduction to Docker

Live logs

docker logs -f <container-id>
repl@host:/# docker logs -f 43aa37614330
PostgreSQL init process complete; ready for start up.

2022-10-24 12:10:40.309 UTC [1] LOG:  starting PostgreSQL 14.5 (Debian 14.5-1.pg..
2022-10-24 12:10:40.309 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port ..
2022-10-24 12:10:40.309 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-10-24 12:10:40.311 UTC [1] LOG:  listening on Unix socket "/var/run/postgre..
2022-10-24 12:10:40.315 UTC [62] LOG:  database system was shut down at 2022-10-..
2022-10-24 12:10:40.318 UTC [1] LOG:  database system is ready to accept connect..
Introduction to Docker

Cleaning up

docker container rm <container-id>
repl@host:/# docker stop 43aa37614330
43aa37614330
repl@host:/# docker container rm 43aa37614330
43aa37614330
Introduction to Docker

Summary of new commands

Usage Command
Start container with a name docker run --name <container-name> <image-name>
Filter running container on name docker ps -f "name=<container-name>"
See existing logs for container docker logs <container-id>
See live logs for container docker logs -f <container-id>
Exit live log view of container CTRL+C
Remove stopped container docker container rm <container-id>
Introduction to Docker

Let's practice!

Introduction to Docker

Preparing Video For Download...