Page tree
Skip to end of metadata
Go to start of metadata

What is Docker? Docker is a tool to make it easier to create, deploy and run application by using containers. Containers allow developers to package up an application with all of the dependencies such as libraries and tools, and deploy it as one package. The application will run on most OS machines (Mac/Windows/Linux) regardless of any customized settings. Overall, this page covers how you can running on the development environments using Docker containers.

Docker installation

Docker supports various OS (Mac/Windows/Linux) and, installation of Docker and running on your OS is very easy.

For installation, click here: Mac/Windows/Linux.

Note!

  • If you are a Windows user, please make sure you have shared your drive.
  • You have to log into Docker to use images from Docker repository.

Testing Docker installation

When you installed Docker, test your Docker installation by running the following command:

$ docker --version
Docker version 19.03.8, build afacb8b

When you run docker command without --version, you would see whole options available with docker. Alternatively, you can test your installation by running the following (you have to log into Docker to use this test):

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

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

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Running Docker containers from prebuilt images

Now, you setup everything, and it is time to use Docker seriously. You will run a container from Alpine Linux image on your system and will learn run docker command. However, you first would like to know what containers and images are , and the difference between containers and an images.

Images: The file system and configuration of applications which are created and distributed by developers. Of course, you can create and distribute images.

Containers: Running instances of Docker images. You can have many containers for a same image. 

Now you know what containers and images are, and let's run docker run alpine ls -l command in your terminal.

$ docker run alpine ls -l
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
cbdbe7a5bc2a: Pull complete
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
total 56
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 bin
drwxr-xr-x    5 root     root           340 May 26 17:11 dev
drwxr-xr-x    1 root     root          4096 May 26 17:11 etc
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 home
drwxr-xr-x    5 root     root          4096 Apr 23 06:25 lib
drwxr-xr-x    5 root     root          4096 Apr 23 06:25 media
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 mnt
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 opt
dr-xr-xr-x  187 root     root             0 May 26 17:11 proc
drwx------    2 root     root          4096 Apr 23 06:25 root
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 run
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 sbin
drwxr-xr-x    2 root     root          4096 Apr 23 06:25 srv
dr-xr-xr-x   12 root     root             0 May 26 17:11 sys
drwxrwxrwt    2 root     root          4096 Apr 23 06:25 tmp
drwxr-xr-x    7 root     root          4096 Apr 23 06:25 usr
drwxr-xr-x   12 root     root          4096 Apr 23 06:25 var
(base) Hyperion:~ yongjunchoi$

When you run docker run alpine ls -l command, it searchs "alpine:latest" image from your system first. If your system has it (i.e. if you downloaded it previously), Docker uses that image. If your system does not have that image, then Docker fetches the "alpine:latest" image from the Docker repository first, and save it in your system, and then runs a container from the saved image. 

docker run alpine starts a container, and ls -l is a command which is fed to the container, so Docker starts the given command and results show up.

To see a list of all images on your system, you can use docker images command.

$ docker images
alpine                     latest              f70734b6a266        4 weeks ago         5.61MB
hello-world                latest              bf756fb1ae65        4 months ago        13.3kB

Next, let's try another command.

$ docker run alpine echo "Hello world"
Hello world

In this case, Docker ran the echo command in your "alpine" container, and then exited it. Exit means the container is terminated after running the command.

Let's try another command.

$ docker run alpine sh

It seems nothing happened. In fact, docker ran sh command in you alpine container, and exited it. If you want to be inside the container shell, you need to use docker run -it alpine sh. -it mean interactive and allocating a pseudo-TTY. You can find more help on run command with docker run --help.

Let's run docker run -it alpine sh.

$ docker run -it alpine sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # uname -a
Linux c1552c9b6cf0 4.19.76-linuxkit #1 SMP Fri Apr 3 15:53:26 UTC 2020 x86_64 Linux
/ # exit

You are inside of the container shell and you can try out a few commands like ls and uname -a and others. To quit the container, type exit on the terminal. If you use exit command, the container is terminated. If you want to keep the container active, then you can use keys ctrl p, and then ctrl q (You don't have to press these two keys simultaneously). If you want to go back into the container, you can type docker attach <container_id>, such as docker attach c1552c9b6cf0. You can find container id with docker ps -all. This command will be explained next.

Now, let's learn on the docker ps command which shows you all containers that are currently running.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS

You can not see any container because no containers are running. To see a list of all containers that you ran, use docker ps --all. You can see that STATUS says that all containers exited.  

$ docker ps --all
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
c1552c9b6cf0        alpine              "sh"                     6 minutes ago       Exited (0) 2 minutes ago                        wonderful_cori
5de22ab86f2a        alpine              "echo 'Hello world'"     18 minutes ago      Exited (0) 18 minutes ago                       goofy_visvesvaraya
df35ee7df7e3        alpine              "ls -l"                  31 minutes ago      Exited (0) 31 minutes ago                       fervent_gould
6dbe999044b4        hello-world         "/hello"                 3 hours ago         Exited (0) 3 hours ago

When Docker containers are created, the Docker system automatically assign a universally unique identifier (UUID) number to each container to avoid any naming conflicts. CONTAINER ID is a shortform of the UUID. You can assign names to your Docker containers when you run them, using --name flags. In addition, you can rename your Docker container's name  with rename command. For example, let's rename "wonderful_cori" to "my_container" with docker rename command.

$ docker rename wonderful_cori my_container
$ docker ps --all
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
c1552c9b6cf0        alpine              "sh"                     10 minutes ago      Exited (0) 6 minutes ago                        my_container
5de22ab86f2a        alpine              "echo 'Hello world'"     22 minutes ago      Exited (0) 22 minutes ago                       goofy_visvesvaraya
df35ee7df7e3        alpine              "ls -l"                  35 minutes ago      Exited (0) 35 minutes ago                       fervent_gould

Build Docker images which contain your own code

Now, you are ready to use Docker to create your own applications! First, you will learn more about Docker images. Then you will build your own image and use that image to run an application on your local machine.

Docker images

Docker images are basis of containers. IN the above example, you pulled the alpine image from the repository and ran a container based on that image. To see the list of images that are available on your local machine, run docker images command.

$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
alpine                     latest              f70734b6a266        4 weeks ago         5.61MB
hello-world                latest              bf756fb1ae65        4 months ago        13.3kB
centos/python-36-centos7   latest              070f320fe348        2 weeks ago         698MB
centos                     7                   b5b4d78bc90c        2 weeks ago         203MB
...

The TAG refers to a particular snapshot of the image and the ID is the corresponding UUID of the image. Images can have multiple versions. When you do not assign a specific version number, the client defaults to latest. If you want a specific version of the image, you can use docker pull command as follows:

$ docker pull centos:7

You can search for images from a repository (for example, the following is Docker hub for centos) or directly from command line using docker search.

$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   6014                [OK]
ansible/centos7-ansible            Ansible on Centos7                              129                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   115                                     [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   114                                     [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   76
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      46
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   44
kinogmt/centos-ssh                 CentOS with SSH                                 29                                      [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   12
guyton/centos6                     From official centos6 container with full up…   10                                      [OK]
centos/tools                       Docker image that has systems administration…   6                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   4
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   3
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   3
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
mcnaughton/centos-base             centos base image                               1                                       [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0

Building your first Docker image

In this section, you will build a simple Docker image with writing a Dockerfile, and run it. For this purpose, we will create a Python script, and a Dockerfile.

Creating working directory

Let's create a working directory where you will make following files: hello.py, Dockerfile

$ cd ~
$ mkdir my_first_Docker_image
$ cd my_first_Docker_image
Python script

Create hello.py file with the following content.

print("Hello world!")
print("This is my 1st Docker image!")
Dockerfile

A Dockerfile is a text file which has a list of commands that the Docker calls while creating an image. The Dockerfile is similar to a job batch file, and contains all information that Docker needs to know to to run the application package.

In my_first_Docker_image directory, create a file, called Dockerfile, which has content as below.

# our base image. The latest version will be pulled.
FROM alpine

# install python and pip
RUN apk add --update py3-pip

# copy files required to run
COPY hello.py /usr/src/my_app/

# run the application
CMD python3 /usr/src/my_app/hello.py

Now, let's learn the meaning of the each line.

The first line means that we will use alpine Linux as a base image. No version is specified, so the latest version will be pulled. Use FROM keyword.

FROM alpine

Next, Python pip package is installed. Use RUN keyword.

RUN apk add --update py3-pip

Next, copy the file to the image. /usr/src/my_app will be created while the file is copied. Use COPY keyword.

COPY hello.py /usr/src/my_app/

The last step is run the application with CMD keyword. CMD tells the container what the container should do by default when it is started.

CMD python3 /usr/src/my_app/hello.py
Build the image

Now you are ready to build your first Docker image. docker build command will do most of the work. However, before you run docker build command, check again if you log in to Docker. 

To build the image, use the following command.

$ docker build -t my_first_image .

The client will pull all necessary images and create your image. If everything goes well, your image is ready to be used! Run docker images command to see if your image my_first_image is shown.

Run your image

When you successfully create your Docker image, test it by starting a new container form the image.

$ docker run my_first_image

If everything went well, you would see this message.

Hello world!
This is my 1st Docker image!

Docker and Jupyter notebook

Now, we will build a Docker image which deploy a Jupyter notebook.

First, let's check Jupyter images on Docker Hub. We will use minimal-notebook.

$docker search jupyter
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jupyter/datascience-notebook            Jupyter Notebook Data Science Stack from htt…   666
jupyter/all-spark-notebook              Jupyter Notebook Python, Scala, R, Spark, Me…   301
jupyterhub/jupyterhub                   JupyterHub: multi-user Jupyter notebook serv…   248                                     [OK]
jupyter/scipy-notebook                  Jupyter Notebook Scientific Python Stack fro…   241
jupyter/tensorflow-notebook             Jupyter Notebook Scientific Python Stack w/ …   218
jupyter/pyspark-notebook                Jupyter Notebook Python, Spark, Mesos Stack …   157
jupyter/base-notebook                   Small base image for Jupyter Notebook stacks…   106
jupyter/minimal-notebook                Minimal Jupyter Notebook Stack from https://…   105
...

Let's start by creating a directory my_note_book. Copy hello.py, which we used for the Python image, to my_note_book dir. Then create a Dockerfile under the my_note_book directory and add content to is as the following.

# base image
FROM jupyter/base-notebook

# copy files
COPY hello.py /home/my_note_book/

# the port number the container should expose
EXPOSE 8888

The last part is specifying the port number which needs to be exposed. The default port for Jupyter is 8888, and therefore, we will expose that port.

Now, build the image using the following command:

$ docker build -t mynotebook .
Sending build context to Docker daemon  4.096kB
Step 1/3 : FROM jupyter/base-notebook
 ---> 7ea955290e01
Step 2/3 : COPY model.py /home/my_note_book_dir/
 ---> 9a1dd638a667
Step 3/3 : EXPOSE 8888
 ---> Running in a955eb421dde
Removing intermediate container a955eb421dde
 ---> 8d3f7447955c
Successfully built 8d3f7447955c
Successfully tagged mynotebook:latest

Now, everything is ready. You can run the image using the Docker run command.

$ docker run -p 8888:8888 mynotebook
Executing the command: jupyter notebook
[I 20:26:03.538 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
[I 20:26:04.144 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.7/site-packages/jupyterlab
[I 20:26:04.144 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 20:26:04.146 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 20:26:04.147 NotebookApp] The Jupyter Notebook is running at:
[I 20:26:04.147 NotebookApp] http://c47b22cd65be:8888/?token=a53b2303380fa4a2e92cc50441ad121a08d6e39dea726fd9
[I 20:26:04.147 NotebookApp]  or http://127.0.0.1:8888/?token=a53b2303380fa4a2e92cc50441ad121a08d6e39dea726fd9
[I 20:26:04.147 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 20:26:04.151 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/.local/share/jupyter/runtime/nbserver-6-open.html
    Or copy and paste one of these URLs:
        http://c47b22cd65be:8888/?token=a53b2303380fa4a2e92cc50441ad121a08d6e39dea726fd9
     or http://127.0.0.1:8888/?token=a53b2303380fa4a2e92cc50441ad121a08d6e39dea726fd9
[I 20:26:11.151 NotebookApp] 302 GET /?token=a53b2303380fa4a2e92cc50441ad121a08d6e39dea726fd9 (172.17.0.1) 0.65ms

  • No labels