Millet Porridge

English version of https://corvo.myseu.cn

0%

Docker Series 1: Basic

Preface

There are more than ten blogs in this serices. I won’t go into code of docker itself, but I will share some tips and tricks in my daily work. When I wrote these blogs, I usedDebian10, docker-ce=5:19.03.2~3-0~debian-buster

Install Docker

Please refer to offical document. If you are in China, maybe you need USTC’s source

1
2
3
4
5
6
7
# A simple way to list available versions
$ apt-cache policy docker-ce

5:19.03.2~3-0~debian-buster 500
500 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian buster/stable amd64 Packages
100 /var/lib/dpkg/status
$ apt-get install docker-ce=5:19.03.2~3-0~debian-buster

Download and build images

1
2
3
4
5
6
7
# download images
$ docker pull python:3.7.4-stretch

# list the images in the host
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.7.4-stretch b9d77e48a75c 3 weeks ago 940MB

You can use docker build to create a new image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# make an empty directory
$ mkdir tmp && cd tmp

# create Dockerfile
$ cat > Dockerfile <<EOF
FROM python:3.7.4-stretch
RUN echo 1 > /tmp/file.txt
EOF

# start build
$ docker build . -t my_image

# now, you can get the image just built
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.7.4-stretch b9d77e48a75c 3 weeks ago 940MB
my_image latest 860d8f0fd1c4 25 seconds ago 940MB

Start and stop container

When we have images, we could use docker run to create a container.

1
2
3
4
5
6
7
$ docker run -ti my_image /bin/bash
root@9b497582be91:/#

# `docker ps` will display the container currently running
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b497582be91 my_image "/bin/bash" 44 seconds ago Up 43 seconds magical_beaver
1
2
3
4
5
6
7
8
9
10
# `docker stop` to stop a container

$ docker stop magical_beaver
magical_beaver


# `docker ps -a` will display all the containers, even we closed it.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b497582be91 my_image "/bin/bash" 2 minutes ago Exited (0) 4 seconds ago magical_beaver

How to execute a command in a running container

I use docker exec to attach a docker container.

1
docker exec -ti <container_name> /bin/bash