Basic Usage of Docker Container
This article will introduce the basic usage of docker, including how to setup, how to initiate a container from image
What is Docker
Docker is a container technology. What is container?
A container is a sandboxed process running on a host machine that isolated from all other processes on same machine.
To summarize, a container is:
- can be run on local machine, virtual machines or deployed on the cloud.
- is isolated from other containers and runs its own software, binaries and configurations
- is a runnable of image
So what’s image?
A running container use an isolated filesystem. This isolated filesystem is provided by image. Image also contains everying needed to run an application - all dependencies, configurations, scripts, binaries etc.
Why use Docker
Let’s take a production environment. You have a running website written by Java, what’s the essense of this website?
Actually it’s a JVM process running on server. To make this JVM runnable on server machine, you need to:
- have a machine acting as server
- have a OS on that machine e.g. Linux or Windows etc.
- have all basic & correct required softwares like JDK, Database, Nginx etc.
When everything is ready, you can see your website. It’s obviously that running a website is so difficult, how about …
We wrap all necessary infrastructure in a blackbox, it can be started by single click. This is why we need Docker!
How to use Docker
-
get a running Linux OS
-
install some basic utils
yum install -y yum-utils device-mapper-persistent-data lvm2
-
install docker
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -r docker-ce
- start docker service
service docker start
- verify installation
docker version
- play with docker!
docker pull hello-world
docker run hello-world
or you can go to hub.docker.com
to find any interesting docker image
Case: Use Docker to run Tomcat
-
docker pull tomcat
-
docker images
-
map the port of local machine to the port of docker container
docker run -d -p 8000:8080 tomcat
-p
: port mapping-d
: run container on background, make the docker not blocking other command -
netstat -tulpn
could find follow item:
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 64506/docker-proxy
docker ps
list all running containerdocker stop <container_id>
docker rm <container_id>
to stop a containerdocker rmi
to remove an image/var/lib/docker
contains all downloaded images and containers
What’s the internal structure of Container?
-
docker run -d -p 8000:8080 tomcat
-
docker ps
to get container id -
docker exec -it <container_id> /bin/bash
can make you execute command inside containerprefix of CLI:root@81092110cc39:/usr/local/tomcat#
-
cat /proc/version
-
java version
-
exit
to back to local machine