Skip to content

Instantly share code, notes, and snippets.

@Tanapruk
Last active February 19, 2019 14:09
Show Gist options
  • Save Tanapruk/ec00b117348ccf8efa00d2f70f953bc0 to your computer and use it in GitHub Desktop.
Save Tanapruk/ec00b117348ccf8efa00d2f70f953bc0 to your computer and use it in GitHub Desktop.
Docker

Docker

Docker's Concepts

  • image - a snapshot of environment that just work. E.g., node10:alpine This is a super small linux (alpine) os with node10 installed.
  • container - a running instance of an image. You can build container into an image and distribute the image.
  • port
    • -p 4000:80 (when using command line.)
        - "4000:80"
    
    (when using docker-compose.yml)
    • locally inside the container it is port 80. Outside the container, the machine(host) that runs it, is port 4000.
    • docker ps will show 0.0.0.0:4000->80/tcp in PORTS column.
    • You can connect to this service through the curl localhost:4000 or through web browser at localhost:4000

docker-machine

control docker machine on your PC *

docker-machine create \
  --driver digitalocean \
  --digitalocean-access-token <GENERATED_TOKEN> \
  --digitalocean-size 2gb \
  --digitalocean-region sgp1 \
  <HOST_NAME>

This creates a new droplet on digital ocean with HOST_NAME, size=2gb, region=singapore

  • eval $(docker-machine env <HOST_NAME) This changes the env of your machine to be the HOST_NAME. When you run docker xxx it is executed on the HOST_NAME server. The eval command essentially set 4 envs of your machine to the like of the following:
DOCKER_HOST=tcp://192.168.99.100:2376
DOCKER_MACHINE_NAME=default
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/Users/<your_username>/.docker/machine/machines/default

You can check it with $ env | grep DOCKER.

After eval $(), you are inside the HOST_NAME. Now, you can run your container through.

docker run -d \
  -p 80:3000 \
  -e ROOT_URL="http://<your app url>" \
  -e MONGO_URL="mongodb://<your mongo url>" \
  <YOUR_HUB_NAME>

-p host:container means going to port 80 on your Docker host will route to port 3000 on the container -e is env setter.

If you unset it through eval $(docker-machine env -u). Your machine is the one who run docker. docker will look inside your env whether these 4 variables exist.

DOCKER_HOST=tcp://192.168.99.100:2376
DOCKER_MACHINE_NAME=default
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/Users/<your_username>/.docker/machine/machines/default

Why docker is good

Previously

  • You develop your codes using git repo (optional)
  • You build your codes into a zip file
  • You upload the zip to the server
  • You access the app server
  • You unzip it
  • You start the server

With CD

  • You develop your codes using git repo (required)
  • You trigger the build through committing to master
  • The CD server build and zip it
  • The CD server send it to the app server, unzip and start

With Docker

  • You develop your codes using git repo (optional)
  • You run docker push <imagename> to upload a container into an image to the docker hub
  • You run docker pull/run <imagename> to deploy the image into the app server

container ensures you have all the dependencies and the right environments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment