- 1. Installing docker
- 2. Installing docker-compose
- 3. Check your docker installation
- 4. docker Basic operations
- 4.1. Search in public docker index
- 4.2. Pull an image
- 4.3. Listing images
- 4.4. Start a container from an image
- 4.5. Listing the containers
- 4.6. Saving a container state
- 4.7. Looking in to docker metatdata
- 4.8. Attaching to a container
- 4.9. Stop/Start/Restart a conatiner
- 4.10. Looking at the logs of container
- 4.11. Killing a container
- 4.12. Delete all stopped containers
- 4.13. Privileged access inside container
- 4.14. Return low level information about container
- 4.15. Check how much resources are used by the container
- 4.16. Controlling resources used by a container
- 4.17. Copy files/folders from a PATH on the container to a HOSTDIR on the host
- 4.18. Run a command inside a directory while starting a container
- 4.19. List the changed files and directories in a container’s filesystem
- 4.20. History of the image
- 4.21. Networking
- 4.22. Exposing a port while starting a container
- 4.23. Docker volumes
- 4.24. Remove docker image
- 5. Dockerfiles
- 6. Next
- 7. References
-
There are multiple ways to install docker based on your platform, please read the docs https://docs.docker.com/engine/installation/
-
You can also get a VM on cloud provider like Amazon or Digital Ocean to make things easier for you.
$ sudo dnf -y install docker
$ sudo systemctl start docker
$ sudo systemctl enable docker
For other Linux distributions or Windows , OS X, refer: https://www.docker.com/products/overview
-
We will also look at how to dockerize an existing application and use docker-compose to bring multi-container application. To install docker-compose follow docs here: https://docs.docker.com/compose/install/
$ docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 2 Server Version: 1.10.3 Storage Driver: devicemapper Pool Name: docker-252:1-1574682-pool Pool Blocksize: 65.54 kB Base Device Size: 10.74 GB Backing Filesystem: xfs Data file: /dev/loop0 Metadata file: /dev/loop1 Data Space Used: 268.4 MB Data Space Total: 107.4 GB Data Space Available: 40.71 GB Metadata Space Used: 704.5 kB Metadata Space Total: 2.147 GB Metadata Space Available: 2.147 GB Udev Sync Supported: true Deferred Removal Enabled: false Deferred Deletion Enabled: false Deferred Deleted Device Count: 0 Data loop file: /var/lib/docker/devicemapper/devicemapper/data WARNING: Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning. Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Library Version: 1.02.122 (2016-04-09) Execution Driver: native-0.2 Logging Driver: journald Plugins: Volume: local Network: host bridge null Kernel Version: 4.7.3-200.fc24.x86_64 Operating System: Fedora 24 (Cloud Edition) OSType: linux Architecture: x86_64 Number of Docker Hooks: 2 CPUs: 4 Total Memory: 3.859 GiB Name: fedora ID: Y4OB:3DAQ:MMLV:M7RP:7UFB:KE2U:XAR3:KQIN:MDAY:TEDW:CVR6:WOGW WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled Registries: docker.io (secure)
$ docker search fedora INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/fedora Official Docker builds of Fedora 429 [OK] docker.io docker.io/fedora/apache 33 [OK] docker.io docker.io/dockingbay/fedora-rust Trusted build of Rust programming language... 3 [OK] docker.io docker.io/gluster/gluster-fedora Official GlusterFS image [ Fedora 21 + Glu... 3 [OK] docker.io docker.io/startx/fedora Simple container used for all startx based... 2 [OK] docker.io docker.io/dasrick/fedora-nginx NGINX image - port 80, 443 - based on Fedo... 1 [OK] docker.io docker.io/fedora/tools Docker image that has systems administrati... 1 [OK] docker.io docker.io/mattsch/fedora-rpmfusion Base container for Fedora with RPM Fusion ... 1 [OK] docker.io docker.io/neroinc/fedora-apache Plain and simple image with Apache httpd b... 1 [OK] docker.io docker.io/pacur/fedora-22 Pacur Fedora 22 1 [OK]
$ docker pull fedora:24 Trying to pull repository docker.io/library/fedora ... 24: Pulling from docker.io/library/fedora Digest: sha256:64a02df6aac27d1200c2572fe4b9949f1970d05f74d367ce4af994ba5dc3669e Status: Downloaded newer image for docker.io/fedora:24 $ docker pull alpine Using default tag: latest Trying to pull repository docker.io/library/alpine ... latest: Pulling from docker.io/library/alpine 117f30b7ae3d: Pull complete Digest: sha256:02eb5cfe4b721495135728ab4aea87418fd4edbfbf83612130a81191f0b2aae3 Status: Downloaded newer image for docker.io/alpine:latest
Try pulling a tagged image i.e. docker pull fedora:24
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 980e0e4c79ec 2 weeks ago 196.7 MB docker.io/fedora 24 11a5107645d4 5 weeks ago 204.4 MB docker.io/fedora latest 11a5107645d4 5 weeks ago 204.4 MB
$ docker run -it fedora bash [root@1b65159da55c /]# cat /etc/fedora-release Fedora release 24 (Twenty Four)
Open another terrminal and run below command while running the container as mentioned in the previous state.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b65159da55c fedora "bash" 35 seconds ago Up 33 seconds silly_morse
To list all containers (both running and stopped)
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 23e00a59a9b4 fedora "ls" 6 seconds ago Exited (0) 4 seconds ago adoring_knuth 1b65159da55c fedora "bash" About a minute ago Exited (0) 12 seconds ago silly_morse
-
Start a container
-
Modify a file
]$ docker run -i -t fedora /bin/bash [root@f369268c54cd /]# echo "docker 101" > /etc/motd
-
On a different terminal, save the container as an image
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f369268c54cd fedora "/bin/bash" 37 seconds ago Up 35 seconds amazing_turing $ docker commit -a "Lalatendu Mohanty" -m "PyCon 2016" f369268c54cd pycon:motd sha256:ce9d49c4ff59b756621dd18479a80fbb16eb25595949bc7ad0c1f22430c21397 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE pycon motd ce9d49c4ff59 16 seconds ago 204.4 MB docker.io/centos latest 980e0e4c79ec 2 weeks ago 196.7 MB docker.io/fedora 24 11a5107645d4 5 weeks ago 204.4 MB docker.io/fedora latest 11a5107645d4 5 weeks ago 204.4 MB
$ sudo cat /var/lib/docker/image/devicemapper/repositories.json | python -mjson.tool { "Repositories": { "docker.io/centos": { "docker.io/centos:latest": "sha256:980e0e4c79ec933406e467a296ce3b86685e6b42eed2f873745e6a91d718e37a", "docker.io/centos@sha256:2ae0d2c881c7123870114fb9cc7afabd1e31f9888dac8286884f6cf59373ed9b": "sha256:980e0e4c79ec933406e467a296ce3b86685e6b42eed2f873745e6a91d718e37a" }, "docker.io/fedora": { "docker.io/fedora:24": "sha256:11a5107645d4ecb36e75d933576f5cdb52358bef385eac2c2d2a91af44ad4ad7", "docker.io/fedora:latest": "sha256:11a5107645d4ecb36e75d933576f5cdb52358bef385eac2c2d2a91af44ad4ad7", "docker.io/fedora@sha256:64a02df6aac27d1200c2572fe4b9949f1970d05f74d367ce4af994ba5dc3669e": "sha256:11a5107645d4ecb36e75d933576f5cdb52358bef385eac2c2d2a91af44ad4ad7" }, "pycon": { "pycon:motd": "sha256:ce9d49c4ff59b756621dd18479a80fbb16eb25595949bc7ad0c1f22430c21397" } } }
$ ID=$(sudo docker run -d fedora /bin/sh -c "while true; do echo PyCon 2016 ; sleep 1; done") $ docker attach $ID PyCon 2016 PyCon 2016 [SNIP]
$ docker run -t -i fedora /bin/bash [root@50559bf9ab0a /]# mount -t tmpfs none /mnt mount: permission denied [root@50559bf9ab0a /]# exit
To get privilaged access, please run below command
$ docker run --privileged -t -i fedora /bin/bash
$ docker inspect $ID $ docker inspect --format='{{.NetworkSettings.IPAddress}}' $ID
$ docker update -m 512MB <ID> P:S: You can get the container ID from "docker ps" command output
-
You can specify resources while running the container too. Check the docker run command usage.
$ docker run --help
$ docker cp $ID:/etc/motd /tmp/
$ docker run -t -i -w /etc fedora ls
Note : if the path does not exist, it will get created
docker diff $ID
-
A Add
-
D Delete
-
C Change
Bind a port to host interface
-
Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine.
$ docker run -d -i -t -p 127.0.0.1:8080:80 fedora bash
-
Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine.
$ docker run -d -i -t -p 127.0.0.1::8080 fedora bash
-
Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine.
docker run -d -i -t -p 80:8080 fedora bash
-
Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces of the host machine.
docker run -d -i -t -p 8080 fedora bash
$ ID=$(docker run --expose=22 -d -i -t pycon:sshd /bin/bash)
For further study, refer: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/
-
Any uncommitted data or changes in containers get lost as soon as containers are deleted
-
Data volumes persist even if the container itself is deleted.
$ docker run -i -t --name demo -v /data docker.io/fedora /bin/bash Run below command in another terminal $ docker inspect
$ docker run -i -t -v /var/logs:/logs_from_host:ro fedora bash $ ls logs_from_host/
-
Further study: https://docs.docker.com/engine/tutorials/dockervolumes/*
$ mkdir /tmp/pycon; cd /tmp/pycon $ echo "FROM fedora" >> Dockerfile $ echo "MAINTAINER Lalatendu" >> Dockerfile $ docker build -t pycon/fedora . $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE pycon/fedora latest 8ab29ba8abf2 5 seconds ago 204.4 MB pycon motd f6b39867b3e9 18 minutes ago 204.4 MB docker.io/fedora 23 5a813a9e051e 31 hours ago 214.4 MB
-
FROM <image> | <image>:<tag>
Set the base image
-
MAINTAINER <name>
Set the author
-
RUN <cmd> | ["executable", "param1", "param2"]
Executes any commands in a new layer on top of the current image and commit the results
-
CMD ["executable","param1","param2"] | ["param1","param2"] | command param1 param2
Provides defaults for an executing container
-
EXPOSE <port> [<port> …]
Open up specified network ports at runtime
-
ENV <key> <value>
This sets the environment variable <key> to the value <value>
-
ADD <src> <dest>
Copy new files from source and add them to the container's filesystem at path
-
ENTYRPOINT ["executable", "param1", "param2"] | command param1 param2
Helps to configure a container that you can run as an executable.
-
VOLUME ["/data"]
Creates a mount point with the specified name and mark it as holding externally mounted volumes from native host or other containers.
-
USER
Sets the username or UID to use when running the image.
-
WORKDIR
Sets the working directory
-
ONBUILD [INSTRUCTION]
Adds to the image a "trigger" instruction to be executed at a later time, when the image is used as the base for another build.