git remote set-url origin https://andrzejsydor:<TOKEN>>@github.com/andrzejsydor/devops.git
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
https://docs.docker.com/engine/reference/commandline/container_run/
cmd | |
---|---|
--help |
|
--rm |
Automatically remove the container when it exits |
--detach , -d |
Run container in background and print container ID |
--interactive , -i |
Keep STDIN open even if not attached |
--tty , -t |
Allocate a pseudo-TTY |
--publish , -p |
Publish a container’s port(s) to the host |
--publish-all , -P |
Publish all exposed ports to random ports |
--volume , -v |
Bind mount a volume |
--name |
Assign a name to the container |
--mount |
Attach a filesystem mount to the container |
--network |
Connect a container to a network |
--restart |
Restart policy to apply when a container exits |
Use the -e
, --env
, and --env-file
flags to set simple (non-array) environment variables in the container you’re running, or overwrite variables that are defined in the Dockerfile of the image you’re running.
docker container run --rm -dit -name myCont alpine
docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list
Manage containers
docker container COMMAND
https://docs.docker.com/engine/reference/commandline/container/
cmd | |
---|---|
ls |
List containers |
inspect |
Display detailed information |
top |
display the running processes of a container |
restart |
restart one or more containers |
Example:
docker ls -a
docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}'
Attach local standard input, output, and error streams to a running container
docker attach [OPTIONS] CONTAINER
https://docs.docker.com/engine/reference/commandline/attach/
Example:
docker attach <CONTAINER>
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
https://docs.docker.com/engine/reference/commandline/exec/
Example:
docker exec -it <CONTAINER> sh
docker exec -it <CONTAINER> sh -c ls
List images
docker images [OPTIONS] [REPOSITORY[:TAG]]
https://docs.docker.com/engine/reference/commandline/images/
Example:
docker images --all
docker images --filter=reference='*bash*'
docker images image*
Manage images
docker image COMMAND
https://docs.docker.com/engine/reference/commandline/image/
cmd |
---|
ls |
pull |
push |
inspect |
import |
Example:
docker image inspect alpine
Fetch the logs of a container
docker logs [OPTIONS] CONTAINER
https://docs.docker.com/engine/reference/commandline/logs/
CMD | Description |
---|---|
--follow , -f |
Follow log output |
--since |
Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) |
--tail |
Number of lines to show from the end of the logs |
--timestamps , -t |
Show timestamps |
Example:
docker logs -f --until=2s
Create a new image from a container’s changes
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
https://docs.docker.com/engine/reference/commandline/commit/
CMD | Description |
---|---|
--author , -a |
Author (e.g., “John Hannibal Smith [email protected]”) |
--change , -c |
Apply Dockerfile instruction to the created image |
--message , -m |
Commit message |
--pause , -p |
Pause container during commit |
Example:
docker commit -c "ENV PROFILE prod" -m 'production profile' <CONTAINER_ID> <IMAGE_ID>
https://docs.docker.com/storage/volumes/
Manage volumes
https://docs.docker.com/engine/reference/commandline/volume/
docker volume COMMAND COMMAND
cmd | |
---|---|
-h |
List all Docker volume commands |
ls |
List current volumes |
create |
Create a volume |
rm |
Delete a volume |
inspect |
Get detailed about a volume |
prune |
Remove all unused local volumes |
https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only
docker container run -v <SOURCE>:<TARGET> <IMAGE>
docker run --rm -v $(pwd)/a.txt:/home/a.txt alpine sh -c 'ls /home'
docker container run --mount type=bind,source=<SOURCE>,target=<TARGET> <IMAGE>
docker volume create <VOLUME_NAME>
docker run -v <VOLUME_NAME> <IMAGE>
https://docs.docker.com/network/
Manage networks
https://docs.docker.com/engine/reference/commandline/network/
docker network COMMAND
Command | Description |
---|---|
docker network connect | Connect a container to a network |
docker network create | Create a network |
docker network disconnect | Disconnect a container from a network |
docker network inspect | Display detailed information on one or more networks |
docker network ls | List networks |
docker network prune | Remove all unused networks |
docker network rm | Remove one or more networks |
https://docs.docker.com/engine/reference/commandline/run/#connect-a-container-to-a-network---network
When you start a container use the --network
flag to connect it to a network.
docker network create <NETWORK>
docker network ls
docker run --network <NETWORK> <IMAGE>
docker network inspect <NETWORK>
docker rm <NETWORK>
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
https://docs.docker.com/engine/reference/commandline/tag/
Example:
docker tag image:latest image:2
docker image build [OPTIONS] PATH | URL | -
https://docs.docker.com/engine/reference/commandline/image_build/
cmd | ||
---|---|---|
--tag , -t |
Name and optionally a tag in the ‘name:tag’ format | |
--file , -f |
Name of the Dockerfile (Default is ‘PATH/Dockerfile’) | |
--label |
Set metadata for an image | |
--rm |
true |
Remove intermediate containers after a successful build |
--force-rm |
Always remove intermediate containers | |
--ulimit |
Ulimit options |
docker image build -t <NAME>:<TAG> .
docker image build -t <NAME>:<TAG> -f Dockerfile .
docker image build -t : - < tar.gz
docker image build -t <name>:<tag>
docker image build --tag <name>:<tag>
git log -1 --pretty=%H
docker image tag <SOURCE_IMAGE>:<TAG> <TARGET_IMAGE>:<TAG>
The ENV
instruction sets the environment variable to the value
. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.
ENV <key> <value>
ENV <key>=<value> ...
https://docs.docker.com/engine/reference/builder/#env
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy
The ARG
instruction defines a variable that users can pass at build-time to the builder with the docker build
command using the --build-arg =
flag
ARG <name>[=<default value>]
https://docs.docker.com/engine/reference/builder/#arg
FROM busybox
USER ${user:-some_user}
ARG user
USER $user
docker network ls
docker network inspect <NAME>
docker network create <NAME>
docker network rm <NAME>
docker network prune
docker network connect <NETWORK> <CONTAINER>
docker container run --name <NAME> -it --network <NETWORK> <IMAGE> <CMD>
docker network disconnect <NETWORK> <CONTAINER>
docker container run -name <NAME> -it --network <NETWORK> --ip <IP> <IMAGE> <CMD>
Log in to a Docker registry
https://docs.docker.com/engine/reference/commandline/login/
docker login [OPTIONS] [SERVER]
Log out from a Docker registry
https://docs.docker.com/engine/reference/commandline/logout/
docker logout [SERVER]
docker container top <NAME>
docker container stats <NAME>
Manage Docker
https://docs.docker.com/engine/reference/commandline/system/
docker system COMMAND
Command | Description |
---|---|
docker system df | Show docker disk usage |
docker system events | Get real time events from the server |
docker system info | Display system-wide information |
docker system prune | Remove unused data |
docker system df
docker system df -v
docker system df --format 'table {{.Type}}\t{{.TotalCount}}\t{{.Size}}'
docker image prune
docker image prune -a
docker image ls | sort -k7 -h -r
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer