Install Docker on Mac
Follow the instruction here to download the docker dmg on mac and start it.
Start Docker Machine on Mac
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# create a default docker virtual machine $ docker-machine rm default $ docker-machine create --driver virtualbox default # list out all docker vm $ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM default * virtualbox Running tcp://192.168.99.100:2376 dev virtualbox Saved # evaluate docker environment $ eval $(docker-machine env default) # start | stop | restart | rm a vm machine $ docker-machine restart default # ssh into the default vm machine $ docker-machine ssh default # get info of a vm machine $ docker-machine inspect default $ docker-machine ip default # list out docker images $ docker ps $ docker ps -a # docker login is needed to download images from public repository $ docker login Username: [username] Password: [password] Email: [email] WARNING: login credentials saved in /Users/ray/.docker/config.json Login Succeeded |
Build Your Own Image
Use Docker Commit
- Build on top of an image without Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# run an new image instance $ docker run -i -t ubuntu:14.04 /bin/bash > apt-get install curl > curl # it is functioning > exit # the container will be stopped # run an new image instance $ docker run -i -t ubuntu:14.04 /bin/bash > which curl # not working $ docker ps -a //take a note of the container id $ docker start <container id> $ docker exec -it <container id> /bin/bash > which curl # it is working # persist the change $ docker commit <container id> rayhon/curl:1.0 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE rayhon/curl 1.0 374e3ebac29b 31 seconds ago 199.4 MB # confirm it $ docker run -it rayhon/curl:1.0 /bin/bash > which curl # working |
Use Dockerfile
- it provides effective way to build an image
- easily fits into your continuous integration and deployment
Dockerfile
- each RUN will execute the command on the top writable layer and perform commit of the image
- you can aggregate multiple RUN by using && to avoid this overhead
- you can use “apt-get install -y” to answer yes to the questions as you cannot see the prompt.
1 2 3 4 5 6 |
# specify base image FROM ubuntu:14.04 MAINTAINER Ray <ray@ad.net> RUN apt-get install -y vim RUN apt-get install -y curl |
Build an image from the Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ docker build -t rayhon/ubuntu:v1 . Sending build context to Docker daemon 2.048 kB Step 1 : FROM ubuntu:14.04 ---> 97434d46f197 Step 2 : MAINTAINER ray <ray@ad.net> ---> Running in 31625def0d07 ---> eab1edb62ccd Removing intermediate container 31625def0d07 Step 3 : RUN apt-get install -y vim ---> Running in 878f2b1f56c1 ... ... Step 4 : RUN apt-get install -y curl ---> Running in 3f5cab855495 ... Removing intermediate container 3f5cab855495 Successfully built 2fd72bf00304 |
Single command instead of 2
- You will cut the steps above from 4 to 3 if you chain the command together using &&
1 2 3 |
# specify base image FROM ubuntu:14.04 RUN apt-get update && apt-get install -y curl vim |
CMD vs ENTRYPOINT
- CMD defines what command to run if you don’t override thru command option during “docker run”
- ENTRYPOINT can be used instead of CMD but ENTRYPOINT is not overridable. If you pass command option during docker run, ENTRYPOINT command will run before it.
- You can use ENTRYPOINT if you want to take argument from users. Use EXEC form is preferred as shell form cannot accept arguments at runtime.
- EXPOSE will expose the ports from container to ports of host.
- Build the image thru docker file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ cat Dockerfile FROM ubuntu CMD ["echo"] $ docker run <image name> echo hello hello $ cat Dockerfile FROM ubuntu ENTRYPOINT ["echo"] $ docker run <image name> echo hello echo hello |
Build an image with intermediate images
1 2 3 |
# in directory “python”, remove all intermediate images (-rm) and # tag the final results with “docker-demo/python” $ docker build -rm -t docker-demo/python python |
Connect with us