Docker Beginner
Content
- Basics
- Terminologies
- Commands
- Docker Images
- Image Types
- Create Images
- Docker Compose
docker-compose.yml- Run Application
Basics
Terminologies
- Images: the file system and configuration of an application used to create containers
docker inspect image-name
- Containers: running instances of Docker images
- Run the actual applications; includes an application and all of its dependencies
- Share the kernel with other containers
- Run as an isolated process in user space on the host OS
- A container created by
docker run
- Docker daemon: the background service running on the host that manages building, running and distributing Docker containers
- Docker client: the command line tool that allows the user to interact with the Docker daemon
- Docker Hub: a registry of Docker images
Commands
docker loginlogin to docker hubdocker pslist current running containersdocker ps -alist containers ran before
docker imageslist imagesdocker pull image-namepull image from hub
image-name:versionspecify version e.g.ubuntu:12.04,ubuntu:latest
docker push USERNAME/image-namepush image to hubdocker run image-name [command]executes commands in container
docker pullif image not exists-ititerative mode; will not exit container-ddetached mode; detach running container from terminal-Ppublish all the exposed container ports to random ports on the Docker host-pspecify port number e.g.-p 8888:80link 8888 on container to 80 on host-epass environment variables- e.g.
-e AUTHOR="name"
- e.g.
--namespecify container name
docker stop container-namestop running containerdocker rm container1-name container2-name ...remove containers
-fremove running containers
docker port container-nameport of running containerdocker search image-namesearch for images
Docker Images
Image Types
- Base images v.s. child images
- Base image: no parent images, usually images with an OS
- Child image: build on base images and add additional functionality
- Official images v.s. user images
- Official image: no prefix
- User image:
user/image-name; based on base image
Create Images
Steps
- Create an app
- Write a Dockerfile
- Build the image
- Run your image
- Dockerfile commands summary
Dockerfile
List of commands the Docker daemon calls while creating an image.
- Base Docker image to run from
- Location of your project code
- Dependencies
- Commands to run at start-up
# specify base image
FROM alpine:latest # username/imagename:version
# copying files & installing dependencies
RUN apk add --update py-pip # install Python pip package to the alpine linux distribution # RUN add new layers
# install required Python packages & copy files
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
# specify port number
EXPOSE 5000
# run application; one CMD per Dockerfile/Image
CMD ["python", "/usr/src/app/app.py"] # which command to run by default when started
Build Images
docker build -t <USERNAME>/appname [path-to-dockerfile]
-toptional tag name
Docker Compose
Define & run multi-container apps.
docker-compose.yml
Describe containers & volumes you want.
version: "2"
services:
vote:
build: ./vote
command: python app.py
volumes:
- ./vote:/app
ports:
- "5000:80"
networks:
- front-tier
- back-tier
result:
build: ./result
command: nodemon --debug server.js
volumes:
- ./result:/app
ports:
- "5001:80"
- "5858:5858"
networks:
- front-tier
- back-tier
worker:
build: ./worker
networks:
- back-tier
redis:
image: redis:alpine
container_name: redis
ports: ["6379"]
networks:
- back-tier
db:
image: postgres:9.4
container_name: db
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- back-tier
volumes:
db-data:
networks:
front-tier:
back-tier:
- Networks: containers can communicate with others in the same network
Run Application
docker-compose up -dstart all containers indocker-compose.yml
-drun in daemon mode in background
Then build & push to hub.