Basic Use of Docker

Docker is an open source application container that allows developers to package their applications and dependencies into a portable engine, and then publish them to operating system such as Linux or Windows for virtualization.

This blog covers basic introduction of the Docker environment, Dockerfile settings and construction of a multi-container environment using docker-compose (one of the essential tools to build, connect and run multiple containers).

(1) Docker containers

The following commands are used to install docker on virtual machine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo bash
apt update; apt -y upgrade;
# Install the required packages
apt install apt-transport-https ca-certificates curl software-properties-common
# Add the GPG key for the official Docker repository to the system
curl -fsSL https://download.docker.com/ linux/ubuntu/gpg |sudo apt-key add -
# Add the Docker repository to APT sources
add-apt-repository "deb [arch=amd64] https:// download.docker.com/linux/ubuntu bionic stable"
# Update the package database with the Docker packages from the newly added repo
apt-get update
# Install Docker
apt install docker-ce
# Check if docker has started
systemctl status docker

Then create a file name “Dockerfile ” and add the following contents in the file:

1
2
3
4
5
6
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install sl
ENV PATH="${PATH}:/usr/games/"
CMD ["echo", "Data Engineering-I."]

Contextualize a container (create a new image that is contextualized according to the Dockerfile and start a container based on the contextualized image):

1
2
3
4
5
docker build -t mycontainer/first:v1 .
# In batch mode
docker run mycontainer/first:v1
# In interactive mode:
docker run -it mycontainer/first:v1 bash

Now we have a running docker container with some extra packages installed in it.

(2) DockerHub

DockerHub is used to store the Docker images, and developers also can use the images that are stored on DockerHub so the required development-environment can be built quickly. To use DockerHub, we need to create an account on its official website firstly. And the following commands are use to operate DockerHub on virtual machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
# login the DockerHub account on virtual machine
docker login
# then input the username and password

# upload image to DockerHub
# docker tag 'image-name:tag-name or image-ID' 'username/ image-name:tag-name'
docker tag e7083fd898c7 arnieswap/my_repo:testing
docker push arnieswap/my_repo
# check if it's pushed successfully
docker search arnieswap/my_repo

# download image from DockerHub
docker pull arnieswap/my_repo

All articles in this blog adopt the CC BY-SA 4.0 agreement except for special statements. Please indicate the source for reprinting!