Thanks to the tutorial Docker for Beginners from KodeKloud.
- Contains only live as long as their entry point is running
- Running e.g.
docker run ubuntuwill exit as soon as ubuntu has finished startingdocker run ubuntu sleep 5executessleep 5and then exits
- Running e.g.
- Find docker's files
- Unix:
/var/lib/docker - Windows
- Run
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -i sh - Navigate to
/var/lib/docker
- Run
- Unix:
- Run a docker image (downloads it if not available)
docker run <user/image>(automatically tags it with:latest)docker run <user/image:version>- Map a port
-p <host-port>:<container-path>
- Run interactively, i.e. allowing the app to capture input from the console
-iand-it, the latter is helpful when linebreaks are different (ie running unix in a windows shell)
- Run container in detached mode
-d
- Map directory/volume on host to container
-v <host-path>:<container-path>-v my_volume:/myvolumewill create the folder/myvolumein the container and create themy_volumeon the host
- Set environment variable in container
-e <ENV_VAR>=<value>- You can find out which environment variables are set by using
docker inspect
- Change the entrypoint
--entrypoint sh
- Pass parameters to the entry point / configure the entry point
- For the
ubuntuimage the entry pointbashbut when you're not running interactively, it'll immediately exit - To pass in parameters to it (which, because it's
bash, will be run), append them:docker run ubuntu sleep 5- To make this permanent, create a new image with the
Dockerfilecontaining:From ubuntu CMD ["sleep", "5"]
- To make this permanent, create a new image with the
- Pass parameters to the entrypoint's program
AndFrom ubuntu ENTRYPOINT ["sleep"] CMD ["5"]docker run ubuntusleeper 7will result insleepbeing called with the parameter7whiledocker run ubuntusleepwill result in the default parameter5forsleep --link <containerid/name>:<alias>- Creates an entry in the hosts file for the host name
<alias>and linking to the ip of<containerid/name>. This is deprecated in favor of user-defined networks! - If
containerid/nameandaliasare the same, one may use--link <alias>
- Creates an entry in the hosts file for the host name
- Performance
--cpus=0.5limits the container's CPU usage to 50% of the host's CPU--memory=100mlimits the memory usable by the container to 100MB
- For the
- Networks
--network=<network>- Bridge
- It's the default
- Contains get their own IP and thus different containers can use the same port, since it's like they're on different machiens
- IPs are usually
172.0.0.2and onwards
- None
- No network available
- Host
- Runs in host network
- IP same as host
- Ports usable only once
- Networks can be created with
docker network create ...
- Attach to the terminal of a running container
docker attach <containerid>- Disconnect with
ctrl+c
- List containers
docker ps- In the
PORTScolumn you will see the published ports.- e.g.
0.0.0.0:3456->3456/tcp, 0.0.0.0:38080->80/tcpwhere 3456 and 80 are the ports on the container and 3456 and 38080 are on the host, forwarding to the container.
- e.g.
- List all
docker ps a
- Stop/Remove a continer
docker stop <containerid> [<containerid2> ..]docker rm <containerid> [<containerid2> ..]
- Images
- List
docker image ls
- Remove
docker image rm <imageid>
- List
- Details of a container
docker inspect <containerid>will yield all information in JSON- To get the IP address
docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerid>
- Logs
docker logs <containerid>
- Building an image
docker build -t <name>[:<tag>] <foldername>- In that foldery there must be a file called
Dockerfile(upper caseD!)
- In that foldery there must be a file called
- See layers in image and their sizes
docker history <imageid>- Example: ubuntu
120MB, python300MB, pip packages5MB, source code500B)
- Example: ubuntu
- The layers are cached, to speed up building of images
- Network
lsList alldocker network create --driver bridge subnet 182.18.0.0/16 custom-isolated-networkcreates a new one with the given IP range and name- To connect to others hosts on the same network, one can use
- their IP, but that's not optimal as it can change etc
- their container name, which is resolved via DNS (the server is at 127.0.0.11)
Name the file docker-compose.yml and run docker-compose up
version: "2" # must be string
services:
web:
image: "mmumshad/simple-webapp"
database:
image: "mongo"
messaging:
image: "redis:alpine"
orchestration:
image: "ansible/galaxy"
myapp:
build: ./myapp # instead of pulling an image from docker, build this folder
ports:
- 5000:80 # map host:5000 to container:80
links:
- database # link to mongodb, no longer required from 2 onwards
depends_on:
- messaging # messaging must start before this one
From version 2 onwards:
- all containers are within
services: - a dedicated network is created so links are no longer required since the container names are resolvable via DNS
From version 3 onwards:
- Docker
swarmis supported
When specifying an image in docker run <image> or a docker-compose.yml file, the format is: registries/username/imagename:tag
registriesif omitted will default todocker.iobut there's alsogcr.iofor google's kubernetes related images and many other.usernameis the name of the registered user on dockerimagenameis optional as well, some users publish only a single image under their name alonetagis optional and will default to:latest
Some registries allow to mark images as private, requiring login credentials
docker login <private-registry-url>will query you for credentialsdocker run <private-registry-url>/<image>then should work if the user used above has access to<image>
There is a docker image called registry that serves a docker registry.
docker run -d -p 5000:5000 --name registry registryto start it- To push an image to the registry
docker image tag my-image localhost:5000/my-imageto tag itdocker push localhost:5000/my-imageand push it
- `docker pull
When on Windows, Docker utilizes Microsoft's Hyper-V to run Linux Containers.
To run Windows containers, go to the task bar, right click on the Docker icon and select Switch to Windows containers