Docker is a technology to run software already bundled with all their dependencies together by one command. The software is distributed in images and once you run it a container is created.
Contains file-system layers with libraries and applications. Images are hosted on hub.docker.com or any other instance of Docker Hub.
You can get one (e.g. nginx image) by calling
docker get nginx. Maybe you will need to
docker login first.
Once downloaded, you can do with images ...
- list available images
- remove an image
docker rmi nginxonce its containers stopped and removed
- run it as a container
docker run nginxBEWARE: this creates a new container every time
- build a new image upon this one by adding file-system layers with your own apps and libs
Running an image - a.k.a. creating a container
is the part where you use the most parameters (external mountpoint, port publishing etc.). Once container is defined start/stop is simple operation without parameters! Available parameters when running an image as a container
--name <name>important if you don't want a random name like "stupid_trump"
-p 8080:8080port to publish (make available from host) - syntax -p
-e VAR_NAME="var value"set an env var inside the container
-v <local-folder>:<container-folder>binds a container-folder to local-folder so you can see and share the final data from the host. This is actually recommended to store user data outside the appplication container. You can either use this
--volumes-fromto use a dedicated data container. Why? Because when you would update the version of your application container you would loose all data with it!
-ddetach container - useful for servers
-rmremove container when it exists - useful for stateless applications
Example of one of my
docker run -d \ --name multishop.postgres \ -e POSTGRES_PASSWORD=my-password \ -e PGDATA=/var/lib/postgresql/data/pgdata \ -p 5432:5432 \ -v `pwd`/data:/var/lib/postgresql/data/pgdata \ # the second part is obviously inside the container "multishop/postgres:9.6" # image to be run
Re-publishing an image
Images are "named" by
tags. Tag contains even source of the image. Therefore if I download an image from local docker hub A
docker pull hub-a.mycompany.com/orgunit/my-image:latest the image appears in
docker images REPOSITORY TAG IMAGE ID CREATED SIZE hub-a.mycompany.com/orgunit/my-image 8.0.7 23ef52fadd22 4 weeks ago 1.94GB busybox latest 6d5fcfe5ff17 3 months ago 1.22MB
Then you add a new tag to the image
docker tag hub-a.mycompany.com/orgunit/my-image:8.0.7 hub-b.mycompany.com/orgunit/my-image:8.0.7
and you will be rewarded by a new tag on the same image (see equal IMAGE_IDs)
docker images REPOSITORY TAG IMAGE ID CREATED SIZE hub-a.mycompany.com/orgunit/my-image 8.0.7 23ef52fadd22 4 weeks ago 1.94GB hub-b.mycompany.com/orgunit/my-image 8.0.7 23ef52fadd22 4 weeks ago 1.94GB busybox latest 6d5fcfe5ff17 3 months ago 1.22MB
Now you can push the image to your other repository
docker push hub-b.mycompany.com/orgunit/my-image:8.0.7
Is a running process that comes from an image. It creates one extra FS layer containing your modifications and data. Container can be stopped and restarted.
- create a running container from an image
docker run [bazilion-of-options] nginx
- stop a container
docker stop nginx1
- (re)start a container
docker start nginx1
- list running containers
- list running and stopped containers
docker ps -a
Examples at https://docs.docker.com/engine/reference/commandline/run/