Demonstration on Docker containers
Resources
Please install Docker on your laptop! See this for Windows and MacOS. For Linux users you should easily find a documentation online according to your distribution!
Check your installation
- Open a terminal
- try
docker --help
Pull an Ubuntu basic image from Docker Hub
-
take a look at the image on Docker Hub
-
docker pull ubuntuYou’ve just pull an image from Docker Hub! -
docker image lsYou can see the Ubuntu image on your list of images.
Create a Dockerfile
A Dockerfile is a file containing a set of instruction to build a new Docker image. Common Dockerfile instructions start with RUN, ENV, FROM, MAINTAINER, ADD, and CMD, among others. They are equivalent to system commands. See the documentation for more details.
-
create a new Dockerfile
touch your-path/Dockerfile -
open it with your favorite text editor (emacs, vi, sublimetext etc.)
-
copy/paste the following content in the Dockerfile
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get install sl -y
- The instruction
FROM ubuntu:latestmeans that the new Docker image will be built by using ubuntu:latest as a basic layer; - the other instructions run commands on the basic image before the new layers and image are created, in this example the packages
sl(remember the train?) is installed.
Build a new Docker image and run it
-
docker build -f Dockerfile -t demo-ubuntu .This command builds the new image, calleddemo-ubuntu, described in the DockerfileDockerfile. -
docker run -i -t demo-ubuntu /usr/games/slHave you seen the train? Yes ! It means your new image have been successfully created and that you have been able to run a command on a container that runs this image.
Get a bash on your container
-
docker run -i -t demo-ubuntu /bin/bashYou are know using a bash terminal on your container. -
apt-get install pythonPython is not installed in the basic Ubuntu image. Let’s install it directly from the bash terminal on your container. -
python --helpOk Python have been installed successfully on the container! -
exit
WTF?
docker run -i -t demo-ubuntu python --helpWhat? You’ve just installed Python in your container but it is not working anymore? What is happening?
Do you remember the philosophy of Docker about application packaging? Do you remember the difference with LXC? You’ve actually created another container from the image demo-ubuntu with this command, you have not run the command on the one created before.
-
docker ps -aNow you can see the container you’ve created so far. Docker has given a name to each container you’ve created (last column). -
docker restart [name]You can restart the previous container. -
docker attach [name]And you can attach to this container to get a bash on it. -
python --helpIt should work! But still this illustrates the ephemeral nature of Docker containers. -
exit -
docker exec [name] lsThis executes a commandlson the container [name]. -
docker rm [name]You’ve just removed the container [name].
Data volume
Let’s create another image with an associated volume. Data volumes is the cleanest way to handle persistent data within Docker containers.
- Create another Dockerfile
totowith the following content:
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get install sl -y
RUN apt-get -y install python
-
docker build -f toto -t demo-ubuntu2 . -
docker run -i -t demo-ubuntu2 python --helpPython is installed in this container as the new Dockerfile included it -
docker run -it -v /data --name container2 demo-ubuntu2You could also use separatelydocker volume create [name]to create a new volume. -
lsYou should see the directorydatamounted in your container. -
touch data/test.orgYou’ve created a file in data. -
exit docker inspect container2You should see the volume details:"Mounts": [ { "Type": "volume", "Name": "a4a840fe25c23c855fcf7f546c4a45b09b1580a39d0203df4630d1d1dd9281b3", "Source": "/var/lib/docker/volumes/a4a840fe25c23c855fcf7f546c4a45b09b1580a39d0203df4630d1d1dd9281b3/_data", "Destination": "/data", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],-
docker retsart container2 -
docker attach container2 -
ls data/ -
exit -
docker rm container2You’ve deleted the container2. -
docker volume lsThe volume is still visible in the list of volumes. If you add-vto the removal of your container, the associated volume would be removed as well. -
docker run -it -v [volumne-name]:/app --name container3 demo-ubuntuThe volume [volume-name] is mounted on the new container3 and placed in the directoryapp. ls app/You should find you previous filetest.org.