Running Docker containers

Introduction to Docker

Tim Sangster

Software Engineer @ DataCamp

Prerequisite

Command Usage
nano <file-name> Opens <file-name> in the nano text editor
touch <file-name> Creates an empty file with the specified name
echo "<text>" Prints <text> to the console
<command> >> <file> Pushes the output of <command> to the end of <file>
<command> -y Automatically respond yes to all prompts from <command>
Introduction to Docker

The Docker CLI

  • Docker command line interface will send instructions to the Docker daemon.
  • Every command starts with docker.

The image called Ubuntu with a Full Ubuntu OS installation can be started to create a running Ubuntu OS that we can interact with using a shell.

Introduction to Docker

Docker container output

docker run <image-name>
docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon created a new container from the hello-world image which runs
 the executable that produces the output you are currently reading.
3. The Docker daemon streamed that output to the Docker client, which sent it to 
 your terminal.
Introduction to Docker

Choosing Docker container output

docker run <image-name>
docker run ubuntu
repl@host:/# docker run ubuntu

repl@host:/#
Introduction to Docker

An interactive Docker container

Adding -it to docker run will give us an interactive shell in the started container.

docker run -it <image-name>
docker run -it ubuntu
docker run -it ubuntu
repl@container:/#
repl@container:/# exit
exit
repl@host:/#
Introduction to Docker

Running a container detached

Adding -d to docker run will run the container in the background, giving us back control of the shell.

docker run -d <image-name>
docker run -d postgres
repl@host:/# docker run -d postgres
4957362b5fb7019b56470a99f52218e698b85775af31da01958bab198a32b072
repl@host:/#
Introduction to Docker

Listing and stopping running containers

docker ps
repl@host:/# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED  
4957362b5fb7   postgres   "docker-entrypoint.s…"   About a minute ago
STATUS              PORTS      NAMES
Up About a minute   5432/tcp   awesome_curie
docker stop <container-id>
repl@host:/# docker stop cf91547fd657
cf91547fd657
Introduction to Docker

Summary of new commands

Usage Command
Start a container docker run <image-name>
Start an interactive container docker run -it <image-name>
Start a detached container docker run -d <image-name>
List running containers docker ps
Stop a container docker stop <container-id>
Introduction to Docker

Let's practice!

Introduction to Docker

Preparing Video For Download...