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 login
login to docker hubdocker ps
list current running containersdocker ps -a
list containers ran before
docker images
list imagesdocker pull image-name
pull image from hub
image-name:version
specify version e.g.ubuntu:12.04
,ubuntu:latest
docker push USERNAME/image-name
push image to hubdocker run image-name [command]
executes commands in container
docker pull
if image not exists-it
iterative mode; will not exit container-d
detached mode; detach running container from terminal-P
publish all the exposed container ports to random ports on the Docker host-p
specify port number e.g.-p 8888:80
link 8888 on container to 80 on host-e
pass environment variables- e.g.
-e AUTHOR="name"
- e.g.
--name
specify container name
docker stop container-name
stop running containerdocker rm container1-name container2-name ...
remove containers
-f
remove running containers
docker port container-name
port of running containerdocker search image-name
search 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]
-t
optional 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 -d
start all containers indocker-compose.yml
-d
run in daemon mode in background
Then build & push to hub.