1. What is Docker?

What are containers?

Containers are completely isolated environments. They can have their own separate processes, network, mounts except they all share the OS kernel.

A container is alive only as long as the process inside it is running. As soon as the process is complete, container exits.

Docker Image

A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform.

It provides a convenient way to package up applications and pre-configured server environments, which you can use for your own private use or share publicly with other Docker users.

Docker Run - start a container

docker run command is used to run a container from an image. For example, docker run nginx will download the ngnix application if not found on the host system and run it. The docker image is pulled down only for the first time and the same image is reused when executing the subsequent command.

Docker ps - list containers

  • docker ps command will list all running containers and some basic information about them.

  • docker ps -a command lists all running containers as well as previously stopped or exited containers.

List all the containers

Docker stop - stop a container

To stop a container you either need to provide a container name or container ID.

Stop a container

Docker rm - remove a container

If you don't want to remove a container because you think it is consuming space: docker rm container_name

Removing a container

Docker images - list of images

docker images shows the list of all images.

List of all the images

docker rmi image_id removes an image that you no longer need.

Removing an image

Note: You must ensure no container is running before attempting to delete an image.

Docker pull - download an image

docker run nginx pulls the nginx image and runs it.

docker pull nginx only pulls the nginx image and stores it on the host but doesn't run it.

Run - attach and detach

docker run kodekloud/simple-webapp runs the container in the attached mode. So you can't essentially do anything until you stop the application.

docker run -d kodekloud/simple-webapp runs the container in the detached mode. So you can do other things while this application is running in the background. You can check the docker ps to see the running containers.

Let's rename our containers:

cloud_user@b9a529768e1c:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fd049cdd7c3 alpine "/bin/sh" 12 hours ago Up 2 hours a-container
cloud_user@b9a529768e1c:~$ docker rename a-container web01
cloud_user@b9a529768e1c:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fd049cdd7c3 alpine "/bin/sh" 12 hours ago Up 2 hours web01
cloud_user@b9a529768e1c:~$

Docker Stats:

If you don't have a docker monitoring solution in place, you can use docker stats or docker stats <container_name>.

Docker Inspect

Provides with lots of output. You can grep out only the parts required.

Docker image running in a detached mode with a port

sudo docker run -dp 4000:80 tutum/hello-world

Exercise:

You have to write a Dockerfile with Ubuntu 16.04 as a base image. The image should have following packages present:

  • telnet

  • curl

  • ffmpeg

Finally, when running a container from the docker image, it should launch with bash.

Deliverables:

  • A Dockerfile

  • A README file with instructions on how to build and run the image

Solution: Writing Dockerfile

FROM ubuntu:16.04
RUN apt-get update && \
apt-get install -y telnet curl ffmpeg && \
apt-get clean
## Instructions to build the docker file
mdkir image1
cd image1
docker build .
# Run the docker file in interactive mode
docker run -it --name <custom-container-name> <image-id> bash

Learning Resources: