2 minute read

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:

  1. can be run on local machine, virtual machines or deployed on the cloud.
  2. is isolated from other containers and runs its own software, binaries and configurations
  3. 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:

  1. have a machine acting as server
  2. have a OS on that machine e.g. Linux or Windows etc.
  3. 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

  1. get a running Linux OS

  2. install some basic utils yum install -y yum-utils device-mapper-persistent-data lvm2

  3. install docker

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum install -r docker-ce

  1. start docker service

service docker start

  1. verify installation

docker version

  1. 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

  1. docker pull tomcat

  2. docker images

  3. 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

  4. netstat -tulpn

    could find follow item:

    tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      64506/docker-proxy  
    
  5. docker ps list all running container
  6. docker stop <container_id>
  7. docker rm <container_id> to stop a container
  8. docker rmi to remove an image
  9. /var/lib/docker contains all downloaded images and containers

What’s the internal structure of Container?

  1. docker run -d -p 8000:8080 tomcat

  2. docker ps to get container id

  3. docker exec -it <container_id> /bin/bash can make you execute command inside container

    prefix of CLI:root@81092110cc39:/usr/local/tomcat#

  4. cat /proc/version

  5. java version

  6. exit to back to local machine