Getting Started With Docker
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 | curl -fsSL https://test.docker.com -o test-docker.sh |
Using official apt
source.
1 | sudo add-apt-repository \ |
Using mirror apt
source.
1 | sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - |
Installation.
1 | sudo apt-get update |
Sample Dockerfile
1 | FROM python:3.9 # indicate build base |
Build Docker Image
1 | sudo docker build -t IMAGE . # simple |
Show All Docker Images
1 | sudo docker images |
Delete Docker Image
1 | sudo docker image rm [-f] IMAGE:TAG [IMAGE...] |
Run Docker Container
1 | sudo docker run -it --rm -d --name demo -p 5000:5050 IMAGE:TAG # remove on stop |
Delete Docker Container
1 | sudo docker rm [-f] CONTAINER [CONTAINER...] |
Show Containers
1 | sudo docker ps [--all] |
Stop & Start
1 | sudo docker stop 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 | $ curl -fsSL https://test.docker.com -o test-docker.sh |
By doing this, Docker will be downloaded automatically, and you can check its version then.
1 | $ docker --version |
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 | Project |
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 | FROM python:3.9 # indicate build base |
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 | docker build -t demo_image . # simple |
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 | docker image rm [-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 | docker run -it --rm -d --name demo -p 5000:5050 demo_image:v1 |
-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 use0.0.0.0
instead of127.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 | $ docker ps |
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 | docker rm [-f] demo |
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 | docker stop demo |
You can get container’s name and ID via
docker ps
.
Then, you can use start
command to run a stopped container.
1 | docker start demo |
And, you can also restart a container.
1 | docker restart demo |
Emm… Actually Docker can do push and pull, which is similar to Git. But I’m not getting it yet. So… Just be it. 😵💫