Try Docker

Try Docker

Hello all guys~

This time is for programming contents after short period of psychology in those couple of episodes.

I have been involving coding in various languages such as Bash, JAVA, Python, and NodeJS. Therefore, I need one tool to accommodate me for them.


Docker is one of elementary tools of all developers, I guess. We can manage to build an environment with less resources than Virtual Machine and need more command line skill to deal with.

What is this for?

For example, we need to develop a program with tens dependencies. Only that operation can spend whole day. And the target OS is Unix but our laptops are Windows. At last, there are 3 Unix machines for production, means whole day for installing those dependencies again.

Today, we have this, Docker.

Docker is as a ready-to-use box. It contains dependencies and OS as we desire for our development.

What is in the Docker box?

Docker is a program. We need image of Docker as a definition of works. An image consists of necessary components. Once we have the image, we can build a container based on that image and open the container to work on it.

Install Docker

Firstly, we have to visit and register. Then download the installer and install it.

We can find this icon at the Taskbar as the left-handed figure when we finished installing Docker.

Try this command on Terminal (cmd in case of Windows).

docker -v

If it shows a version number like above, we completely installed Docker. Yeah.

Try an image

Let’s try “Hello world” image.

1. Download the image

At Docker Hub, we can find the command to download this (link) and its description.

The command to download is:

docker pull hello-world

2. Verify the image

The command to list all images is:

docker images
# or
docker image ls

3. Build a container from the image

Run this

docker run hello-world

Each image will generate different output when build a container. This example just shows a welcome message.

Try Other image: Jupyter

These days, I often code in Python using Jupyter. This is how I use docker for Jupyter.

1. Build a container

# download image
docker pull jupyter/minimal-notebook
# build container + open port 1880 + named "jupyter"
docker run -it -p 8888:8888 – name jupyter jupyter/minimal-notebook

After run, we can put options as below:

  • -it from -i (interactive) to enable typing commands in Terminal and -t (pseudo-TTY) means display result of the commands.
  • -p from --port means connect ports of container to ones of the computer. As you see, command connects port 8888:8888 that is port 8888 of Jupyter container to port 8888 of this computer. In real case we should check the ports from image documents.
  • --name to give a name for this container to comfort us in next uses. If we don’t give its name, a random name will be created.

All options can be found here.

2. Work on the container

The link to Jupyter browser will be created after run the container.

3. Restart the container

Let say, we need to pause working this time. Use this command:

docker stop jupyter

Then we have time and start working again.

docker start jupyter && docker attach jupyter

Frequently used commands

  • Check if the port 8888 is unused
# cmd on Windows
netstat -na | find "8888"
# terminal on Unix
netstat -na | grep "8888"
  • List all images
docker images
docker image ls
  • Remove an image
# remove one image
docker image rm {image_id}
# remove unused images
docker images prune
# remove all images
docker image rm $(docker images -q)
  • Run a container from the image
docker run [-i=interaction] [-t=pseudo-tty] [-d=background] [-p=port {container_port}:{host_port}] [--name {name}] [-v=mount_volume {host_path}:{container_path}] [--link=connect_containers {container_name_or_id}:{link_alias} image [entry_point] 
  • start/stop a container
docker start {container_name}
docker stop {container_name}
docker restart {container_name}
  • Run a command to a started container
# make sure the container is started
docker restart {container_name}
# execute command
docker exec [-i=interaction] [-t=pseudo-tty] [-u=user {username}] container_name {entry_point}
  • Show console of a started container
docker attach {container_name}
  • Remove a container
# remove one container
docker rm {container_name}
# remove all containers
docker rm $(docker ps -a -q)

All command is documented this link.

Real world usages

Based on my own projects, I used docker containers on both development servers and production. Also use Git to transfer source code.

Hope soon I can have a chance to talk about it.


Show Comments