đź’ˇ When referencing a container, we can just type the first few letters of the ID
📝 | Image = the packaged artifact that can be moved around -> not running |
---|---|
📝 | Container = application running in its environment |
docker pull nginx:alpine
docker build -t image-name .
docker images
docker rmi image_name
docker rmi $(docker images -a -q) --force
Exposes port 80 in the container on port 8085 on host
,-d
to detach: prevent the terminal to switch to the container logs => it will only give the container id and stay in current terminal and run the container in background
, container name is optional (otherwise it makes up a name)
docker run -d --name container_name -p 8085:80 image_name
docker run image_name ls /working_dir
Anything that will be written to the mentioned folder will be persisted in the same folder hierarchy on the host; hierarchy will be created if needed
docker run -v /path/to/folder image_name
What’s written to /path/to/folder
in the container will be persisted in $(pwd)
on host (pwd = Print Working Directory on Mac/Linux).
$(pwd)
is the place from where the container is run.
Windows PowerShell version of the variable is ${PWD}
docker run -v $(pwd):/path/to/folder image_name
docker run -rm -it -v $(pwd):/src python:3 python /src/hello.py
docker run -rm -it -v $(pwd):/src bash
You can/should use sh
(or PowerShell
for Windows systems containers) instead of bash
depending on what the container supports
docker exec -it <container_name or container_id> bash
docker logs container_name
docker ps
docker ps -a
docker stop <container_name OR container_name>
docker rm <container_name OR container_id>
docker rm -f container_name
By default, it does not include volumes, to do so add the flag --volumes
docker system prune
docker system prune -a
There are (3) default networks created by Docker
docker network ls
There are other options for the driver type, bridge is the simplest to implement
docker network create --driver bridge network_name
--net
is a shorthand for --network
Containers running in a same network can communicate with each other
docker run -d --net=network_name --name=container_name image_name
See network config and containers inside that network
docker network inspect <network_id or network_name>
docker network rm network_name
Run multi-container applications (the file can define multiple services, networks, volumes…) and avoid retyping the same sequence of CLI commands over and over again.
Advantages:
When the file name is docker-compose.yml
, you don’t need to specify it in the command. Other, you’ll need to add the flag -f /path/docker-my-app.yaml
before the command.
Use the -d
flag for detached.
Newer versions of Docker allow to use the command docker compose
as well as docker-compose
Needed for custom images
docker-compose build
Default
docker-compose up
With specific file and detached
docker-compose -f /path/docker-postgres.yaml up -d
docker-compose start service_name
docker-composer ps
Doesn’t remove the container
docker-composer stop service_name
up
commanddocker-compose down
Accessed with ${ARG_NAME}
export ARG_NAME=arg_value
docker-compose build
Will push all images in the docker-compose file, taking into account the environment variables we set up
docker-compose push
⚠️ depends_on
just starts those services first but doesn’t check that the service is actually running => doesn’t suppress need to add some check of the status and retry logic in the application
version: "3.x"
services:
[list of services: apps, databases...]
service_xyz:
container_name: custom_name_for_service_xyz
image: image_name
build: (where to get the docker file, context...= how to build => for custom image builds)
context:
dockerfile: (when the name is not the default "dockerfile")
args: (arguments passed at build time, accessible with $MY_ENV_VAR)
MY_ENV_VAR: some_value
environment:
- MY_OTHER_ENV_VAR: some_value (arguments passed at runtime) => there is another way to pass arguments via a file
networks: (networks the app is part of)
- ...
ports:
- "external:internal"
volumes:
- ...
depends_on:
- service_abc
service_abc:
...
networks:
[list of networks]
network_def:
driver: bridge (driver type)