sudo apt install docker.io
sudo groupadd docker sudo usermod -aG docker ${USER}
su -s ${USER}
docker run hello-world
docker pull python
As docker shares OS kernels, it will be linux as well.
docker images
docker image rm
docker run --name myPython -it python:latest /bin/bash
- -it: interactive container with a terminal
docker run --name daemon -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
run a container on background
docker start myPython
run an existing container by name
docker stop myPython
docker rm myPython
docker ps -a
exit
docker attach myPython
A directory to be mounted to a container
docker volume create --name test_volume
docker volume ls
docker volume inspect test_volume
docker volume rm test_volume
A script of "docker run" for several containers which collabrate with each other
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
- build docker image with Dockerfile
- start service with docker-compose
services: 即我们要开始定义服务,每个docker容器为一个服务。
app: 这里我们定义了第一个服务,app 为其名字
build: build an image with Dockerfile in the provided path
image: use directly an image
expose: Expose ports WITHOUT publishing them to the host machine
ports: Expose ports WITH publishing them to the host machine
CONTEXT: Either a path to a directory containing a Dockerfile, or a url to a git repository. DOCKERFILE: Alternate Dockerfile. ARGS: Define environment variables
docker-compose build: build image with predefined scripts docker-compose up -d: Create and start containers, leave them background running docker-compose up -d --build: execute the above two commands
docker-compose exec containerName COMMAND
docker-compose down -v
- Prepare a webpage, such as index.html:
- Change this line and have a look.
-
run -d --name "test-nginx" -p 8080:80 -v $(pwd):/usr/share/nginx/html:ro nginx:latest -p: map host 8080 port to docker 80 port
-v: mount current directory (which contains the index.html) to /usr/share/nginx/html inside docker -
visit http://127.0.0.1:8080/ from host machine
https://testdriven.io/blog/dockerizing-flask-with-postgres-gunicorn-and-nginx/