Prologue

I’m a new to Docker, and I have not understand it yet. So in this article, I’ll only show the basic use of it and won’t go much further.

Since I don’t want to put more pressure on my local C volume, I use remote Ubuntu server as an example.

Again, here is a quick reference. You can expand it optionally.

Docker Quick Reference

Download Docker

Using one-click script.

1
2
3
curl -fsSL https://test.docker.com -o test-docker.sh
chmod u+x test-docker.sh
./test-docker.sh

Using official apt source.

1
2
3
4
5
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Using mirror apt source.

1
2
3
4
5
sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"

Installation.

1
2
3
4
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo systemctl enable docker
sudo systemctl start docker

Sample Dockerfile

1
2
3
4
5
FROM python:3.9                      # indicate build base
COPY ./RESTful /RESTful # copy working directory to Docker Image
WORKDIR /RESTful # set working directory
RUN pip install -r requirements.txt # install python requirements
CMD python app.py # run python file

Build Docker Image

1
2
sudo docker build -t IMAGE .                    # simple
sudo docker build -t IMAGE:TAG -f Dockerfile . # with tag 'v1', and indicate build file

Show All Docker Images

1
sudo docker images

Delete Docker Image

1
2
sudo docker image rm [-f] IMAGE:TAG [IMAGE...]
sudo docker rmi [-f] IMAGE:TAG [IMAGE...]

Run Docker Container

1
2
sudo docker run -it --rm -d --name demo -p 5000:5050 IMAGE:TAG # remove on stop
sudo docker run -d --name demo -p 5000:5050 IMAGE:TAG

Delete Docker Container

1
sudo docker rm [-f] CONTAINER [CONTAINER...]

Show Containers

1
sudo docker ps [--all]

Stop & Start

1
2
3
sudo docker stop CONTAINER
sudo docker start CONTAINER
sudo docker restart CONTAINER

What is Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.”


Basic Use of Docker

Reference: https://docs.docker.com/reference/

Download Docker

For Linux, I recommend auto download. It is way much more convenient than do it manually. Just download the official bash downloader, and run the file.

1
2
3
$ curl -fsSL https://test.docker.com -o test-docker.sh
$ chmod u+x test-docker.sh
$ ./test-docker.sh

By doing this, Docker will be downloaded automatically, and you can check its version then.

1
2
$ docker --version
Docker version 24.0.0-rc.1, build eabb927

Build Docker Image

Here, I’d like to use a simple Python Flask + RESTful project as a demonstration. Below is the structure of the project.

1
2
3
4
5
Project
├── Dockerfile # Docker build file
└── RESTful
├── requirements.txt # python requirements
└── app.py # Flask main file

Really simple, huh?

You can create Python requirements by run the following command.

1
pip freeze > requirements

Then, it is time to write Dockerfile. Here is an example. The path here should be relative to Dockerfile.

1
2
3
4
5
6
7
8
FROM python:3.9                      # indicate build base

COPY ./RESTful /RESTful # copy working directory to Docker Image
WORKDIR /RESTful # set working directory

RUN pip install -r requirements.txt # install python requirements

CMD python app.py # run python file

After Dockerfile is finished, you can build Docker Image now by the following command. Make sure you are in the same directory as Dockerfile, here is Project. Be careful about the . at last, it indicates Docker to work at current directory.

1
2
docker build -t demo_image .                   # simple
docker build -t demo_image:v1 -f Dockerfile . # with tag 'v1', and indicate build file

You may need root permission to execute docker commands by adding sudo before commands.

If no error occurred, you will get a Docker image. You can check it by listing all images.

1
docker images

And if you are not satisfied with this image anymore, you can delete it. The tag is optional if you didn’t specify it on creation. There are two ways to delete an image, and -f/--force means force delete.

1
2
docker image rm [-f] demo_image:v1
docker rmi [-f] demo_image:v1

Run Docker Container

When you run a Docker image, it will become Docker container. You can run it rather easily, but it takes many arguments. run command is used only the first time to start container. Later start should use start command instead.

1
2
docker run -it --rm -d --name demo -p 5000:5050 demo_image:v1
docker run -d --name demo -p 5000:5050 demo_image:v1 # fewer arguments

-i: Keep STDIN open even if not attached.

-t: Allocate a pseudo-TTY

--rm: Automatically remove the container when it exits. If this is added, container will be remove on stop.

-d: Run container in background and print container ID.

--name: Assign container name. If omitted, a random name will be chosen.

-p 5000:5050: This indicates the port you want docker to run at. On the left is the host port, which will be wired to host machine. On the right is Docker’s internal port. Emm… Like a virtual machine. For this, we can access our Flask service via localhost:5000, but in Flask, we should set our port to 5050 as it is actually running in Docker.

For example, in our app.py, we should write like this. Here, we should use 0.0.0.0 instead of 127.0.0.1 for some reason that… that I don’t know. 🤐

1
app.run("0.0.0.0", 5050, debug=True)

Once a container is created, it will start to run automatically. To see which containers are currently running, you can use docker ps command. It will list running container info like this. Since there are too many columns, I only list the important ones. docker ps --all will also show stopped containers.

1
2
3
4
5
6
7
$ docker ps
CONTAINER ID IMAGE CREATED STATUS NAMES
ec6b0ae83f02 demo_image:v1 2 minutes ago Up 2 minutes demo
$ docker ps --all
CONTAINER ID IMAGE CREATED STATUS NAMES
ec6b0ae83f02 demo_image:v1 2 minutes ago Up 2 minutes demo
979a3f5e104c another_image:v1 About a minute ago Exited (137) 13 seconds ago daemon

We can see the container ID, and the image it comes from, and their names.

If you want to delete a container, just use rm command. Again, -f/--force is for force removal. You can use both container name or their id.

1
2
docker rm [-f] demo
docker rm [-f] ec6b0ae83f02

Start & Stop Docker

Well, when we do not want a Docker image run, we can stop it by the following command. You can stop a container by its name or id. If you didn’t assigned --rm on run, they will be kept, just not running anymore.

1
2
docker stop demo
docker stop ec6b0ae83f02

You can get container’s name and ID via docker ps.

Then, you can use start command to run a stopped container.

1
2
docker start demo
docker start ec6b0ae83f02

And, you can also restart a container.

1
2
docker restart demo
docker restart ec6b0ae83f02

Emm… Actually Docker can do push and pull, which is similar to Git. But I’m not getting it yet. So… Just be it. 😵‍💫