(git) repo -> (docker) image -> container (app)
For all of the following commands, you have to first navigate to the directory that hosts either the Dockerfil
or the docker-compose.yml
files.
docker build .
# OR to tag an image
docker build -t author/repo:tag .
docker images
# OR
docker image ls
docker rmi $IMAGE_IDENTIFIER
# OR
docker image rm $IMAGE_IDENTIFIER
# OR to prune/remove unused images
docker image prune
Where $IMAGE_IDENTIFIER
is either Image ID
or image tag, i.e. author/repo:tag
.
This will essentially CREATE a new container off of an image. You can create multiple containers from a single image.
docker run $IMAGE_IDENTIFIER
# OR to name a container
docker run --name app $IMAGE_IDENTIFIER
# OR to forward the port to host
docker run --name app -p 4000:4000 $IMAGE_IDENTIFIER
# OR to create a volume from a host directory
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir $IMAGE_IDENTIFIER
# OR to automatically remove the container after being stopped
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir --rm $IMAGE_IDENTIFIER
# OR to run the container in detached/daemon (background) mode
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir -d $IMAGE_IDENTIFIER
# To list running containers
docker ps
# OR to include stopped containers
docker ps -a
docker logs $CONTAINER_IDENTIFIER
# OR to follow logs
docker logs $CONTAINER_IDENTIFIER --follow
# OR for short
docker logs $CONTAINER_IDENTIFIER -f
Where $CONTAINER_IDENTIFIER
is either Container ID
or randomly generated container name, i.e. scary_ship
.
Stopped containers are not entirely removed and they can be started again later, but their initialization commands are already executed.
docker stop $CONTAINER_IDENTIFIER
Note that contrary to docker run
, in this command you are NOT creating a new image and that you are working with $CONTAINER_IDENTIFIER
instead of $IMAGE_IDENTIFIER
. Also note that docker start
always starts the container in detached mode.
docker start $CONTAINER_IDENTIFIER
docker rm $CONTAINER_IDENTIFIER
docker compose up
# OR to override the default .env file with another one
docker compose --env-file .development.env up
# OR to run in the detached/daemon (background) mode
docker compose up -d
docker comopse logs
# OR to follow logs
docker compose logs --follow
# OR for short
docker compose logs -f
docker compose images
docker compose ls
# OR to list stopped services as well
docker compose ls -a
docker compose down
# OR to remove volumes as well
docker compose down -v
docker compose rm
# For starting an interactive shell in Ubuntu flavors
docker exec -it $CONTAINER_IDENTIFIER /bin/bash
# OR for Alpine flavors
docker exec -it $CONTAINER_IDENTIFIER /bin/sh
# OR for shorter version
docker exec -it $CONTAINER_IDENTIFIER sh
# OR to run a specific command
docker exec -it $CONTAINER_IDENTIFIER psql
# List volumes:
docker volume ls
# Inspect a volume:
docker inspect $VOLUME_IDENTIFIER
# Remove a particular volume:
docker volume rm $VOLUME_IDENTIFIER
# Prune/remove all unused volumes:
docker volume prune
# List networks:
docker network ls
# Inspect a network:
docker inspect $NETWORK_IDENTIFIER