# Uninstall old version $ sudo apt-get remove docker docker-engine docker.io
# Update the apt package index $ sudo apt-get update
# Install packages to allow apt to use a repository over HTTPS $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common # Add Docker’s official GPG key $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, # by searching for the last 8 characters of the fingerprint. $ sudo apt-key fingerprint 0EBFCD88
# Use the following command to set up the stable repository. $ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
# Update the apt package index. $ sudo apt-get update
#Install the latest version of Docker CE, or go to the next step to install a specific version $ sudo apt-get install docker-ce
列出可用版本,并且下载
1 2 3 4 5 6 7 8
# List the versions available in your repo $ apt-cache madison docker-ce
# Install a specific version by its fully qualified package name, which is package name (docker-ce) “=” version string (2nd column) $ sudo apt-get install docker-ce=<VERSION>
# Verify that Docker CE is installed correctly by running the hello-world image. $ sudo docker run hello-world
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## List Docker CLI commands docker docker container --help
## Display Docker version and info docker --version docker version docker info
## Execute Docker image docker run hello-world
## List Docker images docker image ls
## List Docker containers (running, all, all in quiet mode) docker container ls docker container ls --all docker container ls -aq
REPOSITORY TAG IMAGE ID friendlyhello latest 326387cea398
Run the app
运行应用程序,使用-p将计算机的端口4000映射到容器的已发布端口80
1
docker run -p 4000:80 friendlyhello
使用 curl 测试
1 2 3
$ curl http://localhost:4000
<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
现在让我们以分离模式在后台运行应用程序:
1
docker run -d -p 4000:80 friendlyhello
使用docker container stop来结束进程,使用CONTAINER ID:
1
docker container stop 1fa4ab2cf395
Share your image
Log in with your Docker ID
1
$ docker login
Tag the image
1 2
# 将某一个镜像放在某个用户的repository下面,并且打上标签 docker tag image username/repository:tag
1 2 3 4 5
# For example: # 添加标签 docker tag friendlyhello vallzey/get-started:part2 # 去除标签 docker image rm vallzey/get-started:part2
Publish the image
1
docker push username/repository:tag
1 2
# For example: docker push vallzey/get-started:part2
现在可以在任何机上运行这个image,如果本地没有的话,系统就会从网上下载
1
docker run -p 4000:80 username/repository:tag
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
docker build -t friendlyhello . # Create image using this directory's Dockerfile docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80 docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode docker container ls # List all running containers docker container ls -a # List all containers, even those not running docker container stop <hash> # Gracefully stop the specified container docker container kill <hash> # Force shutdown of the specified container docker container rm <hash> # Remove specified container from this machine docker container rm $(docker container ls -a -q) # Remove all containers docker image ls -a # List all images on this machine docker image rm <image id> # Remove specified image from this machine docker image rm $(docker image ls -a -q) # Remove all images from this machine docker login # Log in this CLI session using your Docker credentials docker tag <image> username/repository:tag # Tag <image> for upload to registry docker push username/repository:tag # Upload tagged image to registry docker run username/repository:tag # Run image from a registry