Docker builds are isolated with respect to 8 aspects:
# List all docker images
docker images
# List all running containers
docker ps
# last run container
docker ps -l
# Run container (with examples)
docker run [image:tag] /bin/echo 'Hello from your Docker container'
docker run -d ubuntu:latest /bin/bash -c "while true; do echo DOCKERMAN; sleep 1; done"
docker run -t -i [image:tag] /bin/bash
docker run -d -p 8080:80 tutum/apache-php
# Stop container execution
docker stop [containerid]
# View container logs
docker logs [containerid]
# Commit container to an image
docker commit [idofcontainer] [nameofnewimage]
# Remove an image
docker rmi [image-id]
# Remove container
docker rm [container-id]
-d
- run container as daemon.
-t
- TTY mode
-i
- docker image
-p
- port
changes in an image are not saved automatically, so you would have to commit the changes to a new or
an existing image via docker commit
command
docker run -t -i centos6:withapache /bin/bash
service httpd start
curl http://localhost:80
docker inspect [given-name-of-instance] | grep IPAddress | cut -d ":" -f2 | cut -d "\" -f2 = 1.2.3.4
docker attach [given-name] //gives us the command prompt inside the container, already running instance
docker run centos6:withapache
docker ps
Nothing runs because the image does not have any bootup process by itself, starts/stops
The -d
command runs the image as daemon
if there are dependent images, you cannot remove the parent image, it will orphan the dependent images
cat repositories-devicemapper | python -mjson.tool
reference file for repositories/images that exist
containers directory - containers that have yet to be committed and have dependencies on parent images
for any image in containers directory - cat config.json | python -mjson.tool
/sbin/service httpd start
docker commit [container-instance] [container-name-new]
docker run -i -t -d centos:apacherunning /bin/bash
hub.docker.com - free account (1 private repo only)
docker push [image-name]
To add content to a container, start an instance of an image then move files using SCP
In the Dockerfile, we can use the ADD
command:
FROM redhat:centos6
MAINTAINTER Dmitriy Dubson <ddubson@example.com>
ADD testfile.html /var/www/html/testfile.html
Create a volume for any running container:
docker run -i -t -v /myapp test:html /bin/bash
-v
= volume
We can create an empty directory on the local filesystem and mount that volume to the docker container
docker run -t -i -v /root/myvolume:/var/volume test:html /bin/bash
where myvolume
is a folder on the local system
docker run -v /Users/<path>:/<container-path> ...
ip link add r10 type bridge
ip addr add 10.10.100.1/24 dev br10- class C range (10.10.100)
ip link set br10 up
docker.io -d -b br10 &
docker run -t -i centos:centos6 /bin/bash
/etc/rc.local ->
auto lo
iface lo inet loopback
auto br0
iface br10 inet static
address 10.10.100.1
netmask 255.255.255.0
bridge_ports dummy0
bridge_stp off
bridge_fd 0
Labs:
https://gist.github.com/ddubson/2217565de88de6d708d8
Interactive Shell Control
docker run -t -i —name MYCONTAINER <= name a container when running
docker exec -t -i MYCONTAINER /usr/bin/top <= attaching to a process in a container
docker ps -a => history of all containers that ever ran
docker ps -a | wc -l (count number of lines, number of containers that are stopped)
Delete everything that is 7 days ago
Docker ps -a | grep '7 days ago' | awk '{print $1}' | xargs docker rm
Set a static route
docker route add -net [ip] netmask 255.255.255.0 gw [gatewayip]
docker run -d -i -t -v /data --name DATA1 ubuntu:latest /bin/bash
docker run -d -i -t --volumes-from DATA1 --name DATA2 ubuntu:latest /bin/bash
docker run -t -i ubuntu:latest /bin/bash
docker ps
and see what the container name isdocker commit [name] [new-image-name]
docker run -t -i ubuntu:htop /bin/bash
Check if cmd is there.
Useful Docker CLI Commands
docker cp [name-of-container]:/etc/yum.conf tmp
docker diff [name-of-container]
docker events
docker events --since '2014-12-05'
docker history centos:latest
docker exec -I -t MyTestContainer1 /usr/bin/yum -y update
docker -D info
docker kill [container-name]
docker [export|save] OurWeb3 > OurWeb.tar
docker [load] < OurWeb.tar
docker load -i OurWeb.tar
docker images -t
(can see all the layers of containers that were built by Dockerfile)