docker ps
- list only running containers
docker ps -a
- flag -a
is used to list all containers
docker build -t image_name .
docker run -d --name container_name image_name
Use flag -e
or --env
to pass specific variables and replace container
below:
docker run -it -e TEST=123 --env TEST2=456 --rm container /bin/bash
# Write in terminal
echo $TEST
# Output: 123
Use flag --env-file
and replace container
below:
docker run -it --env-file ./path/to/env-file <container-id> /bin/bash
Contents of environment file should look like this:
TEST=123
TEST2=456
Using option --rm
will remove container after it is finished.
Replace container
below:
docker run -it --rm container /bin/bash
Flags:
-i
: specifies interactivet
: enables a terminal typing interface
# Logs in to <container-id>
$ docker exec -it <container-id> /bin/bash
# From local machine's terminal
docker cp container_id:/file/path/within/container /host/path/target
docker logs -f <container>
docker stop $(docker ps -q)
- stop only active
docker stop $(docker ps -aq)
- stop all
When running multiple containers, there could be many containers with status=exited
.
docker rm $(docker ps -q -f status=exited)
docker rmi $(docker images -q)
- image should NOT have reference to container
BE CAREFUL when running this, if you want to keep some data:
docker volume rm $(docker volume ls -qf dangling=true)
User docker-compose
when running multiple containers using docker-compose.yml
:
docker-compose up
Replace container
below:
docker-compose rm container
If the container
is running, you will be informed no stopped containers
for that name, so run:
docker-compose stop container
Confirm the prompt by entering y
, and then re-build running:
docker-compose up container
Replace docker-compose.yml
with the name of the docker file in environment:
docker-compose -f docker-compose.yml down --rmi all
Instead of using localhost
as DB_HOST, use host.docker.internal
.