Nuttapat Koonarangsri
Pan Teparak
Last edit : 14 May 2019 @1.44 am
[TOC]
Check it out yourself here
VM || Docker ??
- Install Docker CE and Docker Compose
- See if it works
docker version
,docker-compose version
- More verification
docker run hello-world
A class of container. Think it as OS or base technology you want to use.
docker images
or docker image ls
Find online images : Docker Hub
Image name: <image name> : <tag/version>
ex: python:3.7-alpine
A ‘running’ Docker image. Multiple containers can use the same image
docker container ls
docker network ls
See it like external disk attached to the container
docker volumes
or docker volumes ls
A definition of Docker image
Path : d1
Dockerfile
FROM python:3.7-alpine # select base image, alpine is cheap
WORKDIR /app # initial path for working dir, create if not exists
RUN pwd # run some script
ADD . . # copy all files from d1/ (host) to /app (container)
RUN ls
CMD python app.py
Build and run it
# Build the image according to Dockerfile, with optional tag (image name)
docker build -t d1 . # notes the "."
# Run the built image with required arguments
docker run d1
One more
Path : d2
Dockerfile
FROM python:3.7-alpine
WORKDIR /app
RUN pwd
ADD . .
RUN ls
CMD ["python", "app.py"]
Build and run it
docker build -t d2 .
# now run it as usual
docker run d2 1 2 # expected "Result: 3"
make change in Dockerfile
FROM python:3.7-alpine
WORKDIR /app
RUN pwd
ADD . .
RUN ls
ENTRYPOINT ["python", "app.py"]
now build and re-run it.
Connecting from outside world
Path: d3
build and run it : docker build -t d3 . && docker run d3
- now try go to http://localhost:5000/hello/your-name
Try run with docker run -p 5000:5000 d3
or docker run --port 5000:5000 d3
now try again
- Try adding
/blah
route that just return stringblah
to screen. - build the
Dockerfile
again - see if it works
Now, What can we optimise here ?
From observations, we know that
- it install
req.txt
every time you changeapp.py
but we don’t want that to run every time
Solution:
FROM python:3.7-alpine
WORKDIR /app
ADD req.txt .
RUN pip install -r req.txt
ADD . .
CMD gunicorn -b 0.0.0.0:5000 app:app
Now try adding one more route, build, and see the change.
Notes:
you can run Docker in detached/daemon mode by adding
-d
flagtry:
docker run -d d3
and it’ll go background !
Step 1: Starting a container: docker run -d --name mysql_sample -p 3307:3306 -e MYSQL_ROOT_PASSWORD=milanomilano -e MYSQL_DATABASE=mydb mysql
**Step 2: ** Get into it: docker exec -it mysql_sample mysql -uroot -p
, enter password
mysql> CREATE TABLE `mydb`.`user` (
`firstname` varchar(255) NULL,
`lastname` varchar(255) NULL,
`student_id` int(0) NULL
);
mysql > use mydb;
mysql > show tables;
mysql > exit; # or ctrl + d
Now you have a table user
in your database. Now try to remove the container it.
docker stop mysql_sample
docker rm mysql_sample
## OR
docker rm -f mysql_sample
**Step 3: ** start over from Step 1 but with -v ./mysql_data:/var/lib/mysql
now do Step 2 and try to start the Step 1 again, see if your data is still there.
Notes:
You can specify 2 types of volumes:
./dir
will use the current directory and create the folder for it,vol_1
will refer todocker volume
. For second approach, you can dodocker volume create mysql_data
and-v mysql_data:/var/lib/mysql
. Now withdocker volumes
you’ll seemysql_data
path : d3
Goal: Want to see the change for Flask app right away.
We can dogunicorn …… —reload
.
- Run
gunicorn
locally, make some changes, see what happen - build dockerfile
- run it with
-d
flag - try changing some routes
- see if it works
- how to fix ??
SOLUTION
docker run --name d3_vol -v “$(pwd):/app” -d d3
-
watch the log
docker logs --follow (or -f) --tail 5 d3_vol
-
make change to
app.py
, see the log -
try see the new change.
What if we have multiple Dockerfile
or services that want to run together