Skip to content

[Linux] Docker Introduction And Creation Step Note

Introduction

Docker is a lightweight virtualization technology that allows us to build an environment suitable for our program development, and also package the entire configuration environment.

So that the program can be transferred to other device, the same Docker environment can still be started to execute the program normally.

It’s a wonderful technology that make us do not worry about different platform.

Now that Docker is mentioned, let’s compare the difference between traditional Virtual Machine (VM) by the way.

Many people must be familiar with VM. For example, the common VirtualBox software allows us to fully simulate another operating system on the device.

However, in Docker’s Container, the Host OS (the operating system on the local side) is directly shared, so it is not as heavy as the load on the memory like VM.

But Docker cannot be without any OS, after all, it still needs the file management system provided by the OS.

Back to topic.

If you want to build a Docker Container, you can follow the steps:

  • Create a Image: You can write Dockerfile by yourself or get from DockerHub
  • Use Image to build a Container
  • Ready to development

The Image file is like the configuration file of our environment, and the Container is the virtual environment that we build with the Image file.

The following will introduce step by step how to install Docker, create a Dockerfile, build the Container and how to operate the Container.

(Note: we can not use GPU environment by install docker, we need nvidia-docker)


Installation of Docker

Step 1. Update

sudo apt-get update


Step 2. Remove the old version

sudo apt-get remove docker docker-engine docker.io


Step 3. Install Docker

sudo apt install docker.io


Step 4. Set up Docker to execute automatically in the system

sudo systemctl start docker
sudo systemctl enable docker

Output:

Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.

If you can see the above information, the Docker should be installed


Write Your Dockerfile

You can create your own Dockerfile under the working directory.

And write down:

# Initial
FROM tensorflow/tensorflow:2.2.1-gpu-py3

# Work dir
WORKDIR /workspace

# Command
RUN pip3 install -U numpy torch torchvision transformers
RUN apt-get update
FROM "IMAGE_NAME"

IMAGE_NAME you can search it on DockerHub (for example:
https://hub.docker.com/r/tensorflow/tensorflow/tags)

WORKDIR is the work path of virtual environment.

RUN is the command to execute in command line when we build the Container.

After writing it, execute it in the same directory (clay/test is my custom name, you can also change it to yours):

docker build -t clay/test .

Output:

You should see such a download and installation screen. After the configuration is complete, it is time to start the container.

docker run \
-e NVIDIA_VISBLE_DEVICES=0 \
--rm \
-it \
-p "8899" \
--name clay_test \
-v /home/ccs96307/workspace/:/workspace/Projects clay/test

The most important thing is the -v parameter. The next step is to map the real folder in the system to the folder in the virtual environment. The clay/test at the end is the custom name just created. (If you have changed, it should be changed to yours)

After the execution, you should see the entry into the virtual environment.


Basic Operation of Docker

1. Temporarily quit

If you just want to exit temporarily, and hope that the Container is still running in the background, you don’t need to close it, just use:

  • Ctrl + q
  • Ctrl + p
  • Close terminal directly


2. Reconnect

Now that we are quit, how do we connect back to the container and restore the current work content?

docker attach clay_test


3. Close the Container completely

There is nothing simpler than this:

  • Ctrl + D
  • exit

References


Read More

Leave a Reply